参考文献:http://blog.csdn.net/candyforever/article/details/8905852
下面简单实现Cocos2d-x一个视频播放的模块,需要播放视频时,不用到处乱改了,一句代码搞定!
一. IOS播放本地视频
对于IOS平台的视频播放,这里直接使用MediaPlayer.framework来播放视频
注意:MediaPlayer.framework播放视频格式有限,可能需要转换为指定的视频格式才能播放!
1.添加MediaPalyer框架到项目中
2.简单写三个类
VideoPlatform,IOSPlayVideo,IOSVideoController
1)VideoPlatform 这个类用来判断播放视频的平台,从而调用各自平台的视频播放接口
VideoPlatform.h
#ifndef __Platform_H_H__ #define __Platform_H_H__ #include "cocos2d.h" using namespace cocos2d; class VideoPlatform { public: //在当前的layer上播放视频,视频完毕或者点击跳过视频会跳转到指定的layer上(默认为空,也就是停留在当前layer上) static void playVideo(const char * filename,CCLayer *layer =NULL); }; #endif // __Platform_H_H__
VideoPlatform.cpp
#include "VideoPlatform.h" #include "../../cocos2dx/platform/CCPlatformConfig.h" #if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID) #include <jni.h> #include "../../cocos2dx/platform/android/jni/JniHelper.h" #include <android/log.h> #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS) #include "IOSPlayVideo.h" #endif void VideoPlatform::playVideo(const char * filename,CCLayer *layer) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //Android视频播放代码 JniMethodInfo minfo; bool isHave = JniHelper::getMethodInfo(minfo,"org/cocos2dx/video/video","playVideo","()V"); if (isHave) { minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID); } #elif(CC_TARGET_PLATFORM==CC_PLATFORM_IOS) //iOS视频播放代码 IOSPlayVideo::playVideoForIOS(filename,layer); #endif }
2) IOSPlayVideo是IOS平台播放视频的接口
IOSPlayVideo.h
- #ifndef__IOSPlayVideo_H_H__
- #define__IOSPlayVideo_H_H__
- #include"cocos2d.h"
- usingnamespacecocos2d;
- classIOSPlayVideo
- {
- public:
- staticvoidplayVideoForIOS(constchar*filename,CCLayer*layer);
- };
- #endif//__IOSPlayVideo_H_H__
IOSPlayVideo.mm