Cocos2d:如何在CCLayer的后台播放视频

时间:2012-08-17 05:44:39

标签: cocos2d-iphone mpmovieplayercontroller ccvideoplayer

我希望在后台播放视频,并在前面输入文字标签,运行以下代码,播放视频,但文字标签不显示!

-(id) init {
    if(!(self=[super init])) {
        return nil;
    }

    CGSize size = [[CCDirector sharedDirector] winSize];

    // MP4
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"]];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [moviePlayer respondsToSelector:@selector(setFullscreen:animated:)];
    moviePlayer.controlStyle = MPMovieControlStyleNone;
    moviePlayer.shouldAutoplay = YES;
    moviePlayer.repeatMode = MPMovieRepeatModeOne;
    moviePlayer.view.frame = CGRectMake(0, 0, size.height, size.width);
    [viewController.view  addSubview:moviePlayer.view];
    [viewController.view  sendSubviewToBack:moviePlayer.view];

    // create and initialize a Label
    CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
    label.position =  ccp( size.width /2 , size.height/2 );
    [self addChild: label];

    return self;
}

2 个答案:

答案 0 :(得分:3)

我找到了答案:

首先在AppDelegate.m中用kEAGLColorFormatRGBA8替换kEAGLColorFormatRGB565,其次,如下面的代码,最后4行很重要:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlayBackDidFinish)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:_moviePlayer];

[_moviePlayer respondsToSelector:@selector(setFullscreen:animated:)];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
_moviePlayer.view.frame = CGRectMake(0, 0, 300, 300);

UIView* glView = [CCDirector sharedDirector].openGLView; // attention
[glView.superview insertSubview:_moviePlayer.view atIndex:0]; // attention
glView.opaque = NO; // attention
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // attention

答案 1 :(得分:1)

CCVideo播放器是在cocos2D项目中播放视频的最佳选择之一

[CCVideoPlayer setDelegate: self];
[CCVideoPlayer playMovieWithFile: @"bait.m4v"];