-(id)init
{
//步骤一:初始化
NSString * filePath = [[NSBundle mainBundle]
pathForResource:movString ofType:@"mov"];
NSURL * url = [NSURL fileURLWithPath:filePath];
self.player1 = [[MPMoviePlayerController alloc] initWithContentURL:url];
//步骤一:注册一个回调消息,来接受视频完成
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//步骤一:UI控件添加到cocos2d的层之上
[[[CCDirector sharedDirector] view] addSubview:_player1.view];
_player1.view.frame = [[CCDirector sharedDirector] view].bounds;
_player1.controlStyle = MPMovieControlStyleNone;//设置为无边框模式
[_player1 prepareToPlay];
/////防止启动时的闪屏///////
_player1.view.alpha = 0;
[self performSelector:@selector(delayPlayMov1) withObject:nil afterDelay:0.2];
//背景画面加载,时机要注意
CCSprite * aSprite = [CCSprite spriteWithFile:bgString];
aSprite.position = ccp([SingleTonTool defaultMemory].DeviceWidth/4,[SingleTonTool defaultMemory].DeviceHeight/4);
[self addChild:aSprite];
}
//加载好以后显示视频,就不会闪
-(void)delayPlayMov1
{
_player1.view.alpha = 1.0;
}
///回调函数的处理
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
NSLog(@"moviePlayBackDidFinish: called");
switch (self.movieNum)//播放到第几段视频,做不同处理
{
case 0:
{
// Remove from view
_player1.view.alpha = 0.0;//直接remove会闪屏幕,所以先虚化后再找机会(屏幕比较黑的时候移除)
[_player1 stop];
[self performSelector:@selector(removeMov1) withObject:nil afterDelay:1.0];
}
break;
case 1:
{
;
}
break;
default:
break;
}
self.movieNum++;
}
-(void)removeMov1//延迟执行的视频移除
{
[_player1.view removeFromSuperview];
[_player1 release];
_player1 = nil;
}