cocos2d-x 学习日志(5)之cocos2d-x播放视频

转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/my183100521/article/details/10215053

一、简介

我们知道cocos2d-x 没有播放视频,那么就用IOS SDK 来实现,如MPMoviePlayerController,它就是处理播放视频。

格式支持:MOV、MP4、M4V及3GP等格式。


二、具体实现


1.添加库文件:

选择项目图标->TARGETS->Build Phases->Link Binary With Libraries ,添加“AVFoundation.framework”及“AVFoundation.framework”


2.添加代码

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

3.测试代码

HelloWorldScene.cpp

    Video *myVideo = Video::create();
    myVideo->init(0, 1, "MiniVideo640x360", "m4v");

三、效果图





引用博文:http://www.gmtdev.com/blog/2011/02/08/playing-a-video-on-iphoneipadipod-under-cocos2d-x/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值