Cocos2d:如何在CCLayer的背景下播放视频 [英] Cocos2d: How to play a video in the background of a CCLayer

查看:221
本文介绍了Cocos2d:如何在CCLayer的背景下播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在背景播放视频,并在正文的文本标签中运行以下代码,正在播放视频,但文字标签不显示!

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

CGSize size = [[CCDirector sharedDirector] winSize];

// MP4
NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@testofType:@m4v]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer respondingToSelector:@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];

//创建和初始化标签
CCLabelTTF * label = [CCLabelTTF labelWithString:@Hello WorldfontName:@Marker FeltfontSize:64];
label.position = ccp(size.width / 2,size.height / 2);
[self addChild:label];

return self;
}


解决方案

/ p>

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

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

[_moviePlayer respondingToSelector:@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


I want the video play in the background, and the text label in the front, run the following code, video is playing, but text label does not show!

-(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;
}

解决方案

I found the answer:

First in AppDelegate.m replace kEAGLColorFormatRGB565 with kEAGLColorFormatRGBA8, Second, as the following code, the last 4 lines is important:

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

这篇关于Cocos2d:如何在CCLayer的背景下播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆