ios播放影片
ios
//
// movieplayercontroller.h
// sgcard
//
// Created by Apple on 13-9-10.
//
//
#import <MediaPlayer/MediaPlayer.h>
#import "movieplayer.h"
#import "EAGLView.h"
@interface MoviePlayerContorl : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate>
{
UIWindow *window;
MPMoviePlayerController* movieController;
CMoviePlayer * moviePlayer;
//EAGLView *view;
UIButton * btnJump;
}
- (void) PlayVideo : (CMoviePlayer*)movie : (NSString*)strFileName : (NSString*)strFileType;
- (void) Finished : (id)sender;
- (void) JumpOver : (id)sender;
- (void) play;
// 用于 iPhone 中点击 Home 键后,程序挂起前要调用下面的函数
- (void)applicationSuspend:(NSNotification *)notification;
// 当挂起的程序重新进入运行时,会调用下面的函数
- (void)applicationResumed:(NSNotification *)notification;
@end
//
// movieplayercontroller.cpp
// sgcard
//
// Created by Apple on 13-9-10.
//
//
#import "movieplayercontroller.h"
#import "cocos2d.h"
#import "GameAdapt.h"
//用于获取 GameInfo 中的信息
#import "../../data/gameinfo.h"
usingnamespacesgcard;
usingnamespacecocos2d;
@implementation MoviePlayerContorl
- (void) PlayVideo : (CMoviePlayer*)movie : (NSString*)strFileName : (NSString*)strFileType
{
moviePlayer = movie;
NSString *path = [[NSBundle mainBundle] pathForResource : strFileName ofType : strFileType];
movieController = [[MPMoviePlayerControlleralloc] initWithContentURL:[NSURLfileURLWithPath:path]];
NSNotificationCenter* notification = [NSNotificationCenterdefaultCenter];
[notification addObserver : selfselector:@selector(Finished :) name :MPMoviePlayerPlaybackDidFinishNotificationobject : movieController];
movieController.scalingMode = MPMovieScalingModeAspectFill;
movieController.controlStyle = MPMovieControlModeDefault;
//下面代码设置播放动画的界面大小,由于 iPhone 上的设置大小跟真实尺寸差1倍,而 ipad 是相等的
CCSize size = CGameAdapt::get()->size();
//将 view 和 window 对象生成提前到这里来为了下面的代码需要
//view = [EAGLView sharedEGLView];
//且 window.screen.scale 在 iPhone 中为2, ipad 中为1. 具体关系可能要请教做 ios 屏幕适配的人
window = [[UIWindow alloc] initWithFrame : [[UIScreen mainScreen] bounds]];
float coeffi = CGameAdapt::get()->coefficient();
float x = CGameAdapt::get()->x() / window.screen.scale;
float y = CGameAdapt::get()->y() / window.screen.scale;
float width = ( size.width * coeffi ) / window.screen.scale;
float height = ( size.height * coeffi ) / window.screen.scale;
[movieController.view setFrame:CGRectMake( x, y, width, height )];
[movieController.viewsetBackgroundColor : [UIColorclearColor]];
float bwidth = ( 205 * coeffi ) / window.screen.scale;
float bheight = ( 69 * coeffi ) / window.screen.scale;
float bx = width - bwidth - 10;
float by = height - bheight - 10;
btnJump = [[UIButton alloc]initWithFrame:CGRectMake(x+bx, y+by , bwidth, bheight)];
//btnJump.titleLabel.font=[UIFont boldSystemFontOfSize:15.0];
//[btnJump setTitle:@"play" forState:0];
//[btnJump setBackgroundImage:[UIImage imageNamed:@"yellow_middle_btn.png"] forState:0];
[btnJumpsetBackgroundImage:[UIImageimageNamed:@"UI_button_new_tgjq0000.png"] forState:UIControlStateNormal];
[btnJumpsetBackgroundImage:[UIImageimageNamed:@"UI_button_new_tgjq0000_1.png"] forState:UIControlStateHighlighted];
[btnJumpaddTarget:selfaction:@selector(JumpOver:) forControlEvents:UIControlEventTouchUpInside];
//设置监听函数
//监听是否触发home键挂起程序.
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(applicationSuspend:) name:UIApplicationWillResignActiveNotificationobject:nil];
//监听是否重新进入程序程序.
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(applicationResumed:) name:UIApplicationDidBecomeActiveNotificationobject:nil];
}
- (void) Finished : (id)sender
{
//动画结束后移除监听的设置,以防止第二次绑定监听函数时候出现崩溃
[[NSNotificationCenterdefaultCenter] removeObserver:self];
[movieController.viewremoveFromSuperview];
moviePlayer->close();
[self release];
}
- (void) JumpOver : (id)sender
{
[movieControllerstop];
}
- (void) play
{
//[view addSubview : movieController.view];
//[view sendSubviewToBack : view];
[windowaddSubview : movieController.view];
[windowaddSubview : btnJump];
[windowmakeKeyAndVisible];
[movieControllerplay];
}
- (void)dealloc
{
[movieControllerrelease];
[windowrelease];
[super dealloc];
}
// 用于 iPhone 中点击 Home 键后,程序挂起前要调用下面的函数
- (void)applicationSuspend:(NSNotification *)notification
{
[movieControllerpause];
}
// 当挂起的程序重新进入运行时,会调用下面的函数
- (void)applicationResumed:(NSNotification *)notification
{
//重新播放已暂停的动画
[movieControllerplay];
}
@end