转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/my183100521/article/details/10215053
我们知道cocos2d-x 没有播放视频,那么就用IOS SDK 来实现,如MPMoviePlayerController,它就是处理播放视频。
格式支持:MOV、MP4、M4V及3GP等格式。
选择项目图标->TARGETS->Build Phases->Link Binary With Libraries ,添加“AVFoundation.framework”及“AVFoundation.framework”
AppController.mm
#import <MediaPlayer/MediaPlayer.h> 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 = 320; rScreen.size.height = 480; //rScreen = CGRect::CGRectMake(0,0, 480, 320); id anid; 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; // player2.view.frame = CGRectMake(0,0, m_iScreenWidth, m_iScreenHeight); anid = [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; playerViewController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;//设置全屏播放 cocos2d::CCDirector::sharedDirector()->purgeCachedData(); //是否设置横屏 CGAffineTransform landscapeTransform; landscapeTransform = CGAffineTransformMakeRotation(90 * M_PI / 180); landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80); [playerViewController.view setTransform:landscapeTransform]; EAGLView *view = [EAGLView sharedEGLView]; [view addSubview:playerViewController.view]; //[viewController.view addSubview:moviePlayer.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.controlStyle = MPMovieControlStyleNone;//隐藏进度条和按钮 [player setScalingMode:MPMovieScalingModeAspectFill];//播放全屏 player.shouldAutoplay=TRUE; [player play]; } g_iPlayVideoState = 1; NSLog(@"PlayVideo done"); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //当点击Done按键或者播放完毕 - (void) movieFinishedCallback:(NSNotification*) aNotification { NSLog(@"movieFinishedCallback"); MPMoviePlayerController *player = [aNotification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player]; [player.view removeFromSuperview]; g_iPlayVideoState = 0; [window setHidden:true]; [playerViewController release]; NSLog(@"movieFinishedCallback done"); }新建类为Video,为了实现C++与OC混编,把*.cpp改为mm
Video.h
#ifndef __playVideo__Video__ #define __playVideo__Video__ #include "cocos2d.h" using namespace cocos2d; class Video{ public: void init(int iStateAfterPlay,int iFullScreen,const char* strFilennameNoExtension,const char* strExtension); int backInformation(); static Video *create(void); }; #endif /* defined(__playVideo__Video__) */Video.mm
#include "Video.h" #include "AppController.h" using namespace std; #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #import <MediaPlayer/MediaPlayer.h> extern int g_iPlayVideoState; Video *Video::create(void) { Video *pRet = new Video(); if (pRet) { return pRet; } else { CC_SAFE_DELETE(pRet); return NULL; } } void Video::init(int iStateAfterPlay, int iFullScreen, const char *strFilennameNoExtension, const char *strExtension) { AppController *myAppController; myAppController = [AppController alloc]; myAppController = [myAppController init]; NSString * strFilename = [NSString stringWithUTF8String:strFilennameNoExtension]; NSString * strExtend = [NSString stringWithUTF8String:strExtension]; [myAppController PlayVideo:iStateAfterPlay fullscreen:iFullScreen file:strFilename fileExtension:strExtend]; } int Video::backInformation() { int information = -1; information = g_iPlayVideoState; return information; } #endif
HelloWorldScene.cpp
Video *myVideo = Video::create(); myVideo->init(0, 1, "MiniVideo640x360", "m4v");