博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Playing Video on iPhone Cocos2D-X
阅读量:6842 次
发布时间:2019-06-26

本文共 4511 字,大约阅读时间需要 15 分钟。

原文地址:http:///www.cocos2d-x.org/boards/7/topics/550Got this working, thought I'd share:Can also be found on my not very often updated blog hereOK, first thing you need to know is that Cocos2d doesn’t play videos, it leaves this up to the iOS SDK on the devices. The following code therefore is Obj C which can be inserted into your projects AppController.mm file or you could create a new mm interface file and put it in there.Add the “AVFoundation.framework” to your projects Frameworks (located the Frameworks folder, right click, select “Add -> Existing Frameworks”).Add the “MediaPlayer.framework” to your projects Frameworks.You will need a video to play and to add it to the resources in the project. Use your own or download my sample video from hereAdd the following code to your AppController.mm////MPMoviePlayerViewController *playerViewController=NULL;int g_iPlayVideoState=0;- (void)PlayVideo:(int)iStateAfterPlay fullscreen:(int)iFullScreen file:(NSString*)strFilennameNoExtension fileExtension:(NSString*)strExtension{    NSLog(@"PlayVideo start");    g_iPlayVideoState = 2;    NSString *url = [[NSBundle mainBundle] pathForResource:strFilennameNoExtension ofType:strExtension];    CGRect rScreen;    rScreen.origin.x = 0;    rScreen.origin.y = 0;    rScreen.size.width = 480;    rScreen.size.height = 320;    //rScreen = CGRect::CGRectMake(0,0, 480, 320);            if( iFullScreen==0 )    {        MPMoviePlayerController *player2 = [[MPMoviePlayerController alloc]                                             initWithContentURL:[NSURL fileURLWithPath:url]];        [[NSNotificationCenter defaultCenter]             addObserver:self            selector:@selector(movieFinishedCallback:)            name:MPMoviePlayerPlaybackDidFinishNotification            object:player2];        //---play partial screen---        player2.view.frame = rScreen;        [self addSubview:player2.view];        player2.shouldAutoplay=TRUE;        //---play movie---        [player2 play];    }       else    {        playerViewController = [[MPMoviePlayerViewController alloc]                                 initWithContentURL:[NSURL fileURLWithPath:url]];        [[NSNotificationCenter defaultCenter]         addObserver:self        selector:@selector(movieFinishedCallback:)        name:MPMoviePlayerPlaybackDidFinishNotification        object:[playerViewController moviePlayer]];        playerViewController.view.frame = rScreen;        cocos2d::CCDirector::sharedDirector()->purgeCachedData();        // Add the view        EAGLView *view = [EAGLView sharedEGLView];        [view addSubview:playerViewController.view];        [view sendSubviewToBack:view];        NSLog(@"pView inserted");        // Add the view - Use these three lines for Cocos 2D X        window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];        [window addSubview: playerViewController.view];        [window makeKeyAndVisible];        //---play movie---        MPMoviePlayerController *player = [playerViewController moviePlayer];        player.scalingMode=MPMovieScalingModeAspectFit;        player.shouldAutoplay=TRUE;        [player play];    }    g_iPlayVideoState = 1;    NSLog(@"PlayVideo done");}////- (void) movieFinishedCallback:(NSNotification*) aNotification {    NSLog(@"movieFinishedCallback");    MPMoviePlayerController *player = [aNotification object];    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];        [player.view removeFromSuperview];     [player autorelease];        g_iPlayVideoState = 0;    NSLog(@"movieFinishedCallback done");}    Call the PlayVideo function from with a line like the following. Call it from your AppController’s didFinishLaunchingWithOptions function just before (or after) the app.run();[self PlayVideo:0 fullscreen:1 file:@"ThreeSheets_Title_480x272" fileExtension:@"m4v"];In Appelegate.cpp, find your applicationDidFinishLaunching() function and after “pDirector->setOpenGLView(m_pMainWnd);” add the line:glClearColor(0.0f, 0.0f, 0.0f, 1.0f);The above gl command sets your Cocos2D-X window to transparent wherever it is black, so you can see through it to the video behind.I have a global variable “g_iPlayVideoState” which you can monitor in your Cocos2D applications code to detect when the video ends, its values are 2=initializing, 1=playing, 0=finished. At the end of play you might want to put the views back to non-transparent with the call “glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

转载地址:http://yjzul.baihongyu.com/

你可能感兴趣的文章
python基础教程
查看>>
linux命令:function脚本编程之函数
查看>>
Linux性能监控之CPU利用率
查看>>
第九节 VMware View 6.0 菜鸟入门 连接服务器的安装和部署
查看>>
spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
查看>>
C# 文件操作详解(二)---------FileInfo类
查看>>
Windows Server 2012系列---文件服务器资源管理器FSRM(2)
查看>>
JPA注解
查看>>
LogMiner详细讲解
查看>>
03.17基本控件的使用
查看>>
ElementaryOS 安装PhpStorm
查看>>
nutch与起点R3集成之笔记(二)
查看>>
ThinkPHP 统计查询
查看>>
厚黑学
查看>>
C++异常处理机制之一
查看>>
CentOS 5.2安装
查看>>
js prototype的理解
查看>>
dubbo学习笔记 第九章dubbo服务调用的安全控制
查看>>
Bela Ban's JGroups Manual Translation Serial III - JGroups API
查看>>
Collection接口
查看>>