cocos2dx 3.15 视频播放实现 lua 以及c++ 版本VideoPlayer的使用注意 与详细示例
2017-11-11 21:51
1966 查看
废话不多说,直接上代码
VideoPlayerScene.h#ifndef __VIDEOPLAYERSCENE_H__ #define __VIDEOPLAYERSCENE_H__ #include "ui/UIVideoPlayer.h" #include "ui/CocosGUI.h" #include "cocos2d.h" USING_NS_CC; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) using namespace cocos2d::experimental::ui; #endif class VideoPlayerScene : public Layer { public: VideoPlayerScene(void); ~VideoPlayerScene(void); virtual bool init(); static cocos2d::Scene *createScene(); CREATE_FUNC(VideoPlayerScene); Label* _videoStateLabel; Rect _visibleRect; //cocos2d::experimental::ui::VideoPlayer* m_VideoPlayer; //Node生命周期 void onEnter(); void onExit(); void showVideo(); void videoPlayOverCallback(); void menuCloseCallback(cocos2d::Ref* sender); //退出事件回调 void menuRatioCallback(cocos2d::Ref* sender); void menuResourceVideoCallback(cocos2d::Ref* sender); void menuOnlineVideoCallback(cocos2d::Ref* sender); void menuFullScreenCallback(cocos2d::Ref* sender); //全屏回调 void menuPauseCallback(cocos2d::Ref* sender); //暂停播放回调 void menuResumeCallback(cocos2d::Ref* sender); //返回播放回调 void menuStopCallback(cocos2d::Ref* sender); //停止播放回调 void menuHintCallback(cocos2d::Ref* sender); //隐藏回调 /** * 视频播放状态,只有在android和ios平台有效 */ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) cocos2d::experimental::ui::VideoPlayer* m_VideoPlayer; void videoEventCallback(Ref* sender, cocos2d::experimental::ui::VideoPlayer::EventType eventType); #endif }; #endif //__VIDEOPLAYERSCENE_H__
VideoPlayerScene.cpp
#include "VideoPlayerScene.h" VideoPlayerScene::VideoPlayerScene() { } VideoPlayerScene::~VideoPlayerScene() { } Scene* VideoPlayerScene::createScene() { Scene *scene = Scene::create(); VideoPlayerScene* layer = VideoPlayerScene::create(); scene->addChild(layer); return scene; } bool VideoPlayerScene::init() { if (!Layer::init()) //初始化父类 return false; return true; } void VideoPlayerScene::onEnter(){ Layer::onEnter(); _visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect(); MenuItemFont::setFontSize(16); auto fullSwitch = MenuItemFont::create("FullScreenSwitch", CC_CALLBACK_1(VideoPlayerScene::menuFullScreenCallback, this)); fullSwitch->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); fullSwitch->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 200)); auto pauseItem = MenuItemFont::create("Pause", CC_CALLBACK_1(VideoPlayerScene::menuPauseCallback, this)); pauseItem->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); pauseItem->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 400)); auto resumeItem = MenuItemFont::create("Resume", CC_CALLBACK_1(VideoPlayerScene::menuResumeCallback, this)); resumeItem->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); resumeItem->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 350)); auto stopItem = MenuItemFont::create("Stop", CC_CALLBACK_1(VideoPlayerScene::menuStopCallback, this)); stopItem->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); stopItem->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 300)); auto hintItem = MenuItemFont::create("Hint", CC_CALLBACK_1(VideoPlayerScene::menuHintCallback, this)); hintItem->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); hintItem->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 250)); //------------------------------------------------------------------------------------------------------------------- auto resourceVideo = MenuItemFont::create("Play resource video", CC_CALLBACK_1(VideoPlayerScene::menuResourceVideoCallback, this)); resourceVideo->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); resourceVideo->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 50)); auto onlineVideo = MenuItemFont::create("Play online video", CC_CALLBACK_1(VideoPlayerScene::menuOnlineVideoCallback, this)); onlineVideo->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); onlineVideo->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.siz 4000 e.width - 10, _visibleRect.origin.y + 100)); auto ratioSwitch = MenuItemFont::create("KeepRatioSwitch", CC_CALLBACK_1(VideoPlayerScene::menuRatioCallback, this)); ratioSwitch->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); ratioSwitch->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 150)); auto menu = Menu::create(resourceVideo, onlineVideo, ratioSwitch, fullSwitch, pauseItem, resumeItem, stopItem, hintItem, nullptr); menu->setPosition(Vec2::ZERO); this->addChild(menu,100); Label* label = Label::createWithSystemFont("Test videoPlayer","arial",20); label->setPosition(800,500); this->addChild(label,100); _videoStateLabel = Label::createWithSystemFont("IDLE", "Arial", 16); _videoStateLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); _videoStateLabel->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 500)); this->addChild(_videoStateLabel); showVideo(); } void VideoPlayerScene::onExit(){ Layer::onExit(); } void VideoPlayerScene::showVideo(){ Size size = Director::getInstance()->getWinSize(); auto centerPos = Vec2(_visibleRect.origin.x + _visibleRect.size.width / 2,_visibleRect.origin.y + _visibleRect.size.height /2); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) m_VideoPlayer = cocos2d::experimental::ui::VideoPlayer::create(); m_VideoPlayer->setPosition(Point(size.width / 4, size.height / 2)); m_VideoPlayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE); m_VideoPlayer->setTouchEnabled(false); m_VideoPlayer->setContentSize(Size(size.width/2, size.height)); this->addChild(m_VideoPlayer,10); if (m_VideoPlayer) { m_VideoPlayer->setFileName("oceans.mp4"); m_VideoPlayer->play(); } m_VideoPlayer->addEventListener(CC_CALLBACK_2(VideoPlayerScene::videoEventCallback, this)); #endif } /** * 视频播放完成的回调函数 */ void VideoPlayerScene::videoPlayOverCallback() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->setURL("http://benchmark.cocos2d-x.org/cocosvideo.mp4"); m_VideoPlayer->play(); } #endif } /** * 视频播放的状态 * 注意这里的代码,此处代码只有在android平台和Ios平台有效 */ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) void VideoPlayerScene::videoEventCallback(Ref* sender, cocos2d::experimental::ui::VideoPlayer::EventType eventType){ switch (eventType) { case cocos2d::experimental::ui::VideoPlayer::EventType::PLAYING: _videoStateLabel->setString("PLAYING"); break; case cocos2d::experimental::ui::VideoPlayer::EventType::PAUSED: _videoStateLabel->setString("PAUSED"); break; case cocos2d::experimental::ui::VideoPlayer::EventType::STOPPED: _videoStateLabel->setString("STOPPED"); break; case cocos2d::experimental::ui::VideoPlayer::EventType::COMPLETED: _videoStateLabel->setString("COMPLETED"); videoPlayOverCallback(); break; default: break; } } #endif void VideoPlayerScene::menuCloseCallback(Ref* sender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } //全屏回调 void VideoPlayerScene::menuFullScreenCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->setFullScreenEnabled(!m_VideoPlayer->isFullScreenEnabled()); } #endif } //视图缩放比例回调 void VideoPlayerScene::menuRatioCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->setKeepAspectRatioEnabled(! m_VideoPlayer->isKeepAspectRatioEnabled()); } #endif } //播放本地视频回调 void VideoPlayerScene::menuResourceVideoCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->setFileName("oceans.mp4"); m_VideoPlayer->play(); } #endif } //播放在线视频回调 void VideoPlayerScene::menuOnlineVideoCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->setURL("http://benchmark.cocos2d-x.org/cocosvideo.mp4"); m_VideoPlayer->play(); } #endif } //暂停播放回调 void VideoPlayerScene::menuPauseCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->pause(); } #endif } //返回播放回调 void VideoPlayerScene::menuResumeCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->resume(); } #endif } //停止播放回调 void VideoPlayerScene::menuStopCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->stop(); } #endif } //隐藏播放视图回调 void VideoPlayerScene::menuHintCallback(cocos2d::Ref* sender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) if (m_VideoPlayer) { m_VideoPlayer->setVisible(!m_VideoPlayer->isVisible()); } #endif }
注意事项
1.android.mk 文件必须添加需要交叉编译的cpp文件,以我的为例F:\TestFun\TestFun\proj.android\jni,目录下的android.mk文件LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) $(call import-add-path,$(LOCAL_PATH)/../../cocos2d) $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external) $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos) LOCAL_MODULE := cocos2dcpp_shared LOCAL_MODULE_FILENAME := libcocos2dcpp LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp \ ../../Classes/Scene/VideoPlayerScene.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes # _COCOS_HEADER_ANDROID_BEGIN # _COCOS_HEADER_ANDROID_END LOCAL_STATIC_LIBRARIES := cocos2dx_static # _COCOS_LIB_ANDROID_BEGIN # _COCOS_LIB_ANDROID_END include $(BUILD_SHARED_LIBRARY) $(call import-module,.) # _COCOS_LIB_IMPORT_ANDROID_BEGIN # _COCOS_LIB_IMPORT_ANDROID_END
2.配置好环境包括 sdk ndk ant python路径等,注意和cocos2dx 引擎 对应 例如3.15 android-ndk-r10c
3.VedioPlayer 在win32无效
lua版代码,其中一个坑就是一定要在OnEnter里面创建VideoPlayer
VideoPlayerScene.lua-- 获取目标平台 local targetPlatform = cc.Application:getInstance():getTargetPlatform() local videoPlayer = nil local videoStateLabel = nil local touchStateLabel = nil --声明VideoPlayerScene类 local VideoPlayerScene = class("VideoPlayerScene",function() return cc.Scene:create() end) --静态创建函数 function VideoPlayerScene.create() local scene = VideoPlayerScene.new() scene:addChild(scene:createLayerMenu()) return scene end --构造函数 function VideoPlayerScene:ctor() self.visibleSize = cc.Director:getInstance():getVisibleSize() self.origin = cc.Director:getInstance():getVisibleOrigin() self.winSize = cc.Director:getInstance():getWinSize() self.visibleRect = cc.Director:getInstance():getOpenGLView():getVisibleRect() self.centerPos = cc.p(self.visibleRect.x + self.visibleRect.width / 2,self.visibleRect.y + self.visibleRect.height /2) self.schedulerID = nil local function onNodeEvent(event) if event == "enter" then self:onEnter() end end self:registerScriptHandler(onNodeEvent) end function VideoPlay 1026a erScene:onEnter() cclog("onEnter") local testLabel = cc.Label:createWithTTF("test", "fonts/Marker Felt.ttf", 28) testLabel:setAnchorPoint(cc.p(0.5, 0.5)) testLabel:setPosition(3 * self.winSize.width /4 , self.winSize.height - 350) self:addChild(testLabel) if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_ANDROID == targetPlatform) then local function onVideoEventCallback(sener, eventType) if eventType == ccexp.VideoPlayerEvent.PLAYING then videoStateLabel:setString("PLAYING") elseif eventType == ccexp.VideoPlayerEvent.PAUSED then videoStateLabel:setString("PAUSED") elseif eventType == ccexp.VideoPlayerEvent.STOPPED then videoStateLabel:setString("STOPPED") elseif eventType == ccexp.VideoPlayerEvent.COMPLETED then videoStateLabel:setString("COMPLETED") end end videoPlayer = ccexp.VideoPlayer:create() videoPlayer:setAnchorPoint(cc.p(0,0)) videoPlayer:setContentSize(cc.size(self.winSize.width /2,self.winSize.height)) videoPlayer:addEventListener(onVideoEventCallback) videoPlayer:setPosition(cc.p(0,0)) self:addChild(videoPlayer) if nil ~= videoPlayer then local videoFullPath = cc.FileUtils:getInstance():fullPathForFilename("oceans.mp4") videoPlayer:setFileName(videoFullPath) videoPlayer:play() end end end --播放背景音乐 function VideoPlayerScene:playBgMusic() AudioEngine.playMusic("bg.mp3",true) end -- create menu 创建菜单层 function VideoPlayerScene:createLayerMenu() local layerMenu = cc.Layer:create() local titleLabel = cc.Label:createWithTTF("MyVideoPlayer", "fonts/Marker Felt.ttf", 28) titleLabel:setAnchorPoint(cc.p(0.5, 0.5)) titleLabel:setPosition(3 * self.winSize.width /4 , self.winSize.height - 50) layerMenu:addChild(titleLabel, 1) cc.MenuItemFont:setFontSize(18) videoStateLabel = cc.Label:createWithSystemFont("IDLE","Arial",16) videoStateLabel:setAnchorPoint(cc.p(1, 0.5)) videoStateLabel:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 10,self.visibleRect.y + 200)) layerMenu:addChild(videoStateLabel) touchStateLabel = cc.Label:createWithSystemFont("test","Arial",20) touchStateLabel:setAnchorPoint(cc.p(1, 0.5)) touchStateLabel:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 10,self.visibleRect.y + 150)) layerMenu:addChild(touchStateLabel) local fullSwitch = cc.MenuItemFont:create("FullScreenSwitch") fullSwitch:setAnchorPoint(cc.p(0.0, 0.0)) fullSwitch:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 50)) local pauseItem = cc.MenuItemFont:create("Pause") pauseItem:setAnchorPoint(cc.p(0.0, 0.0)) pauseItem:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 100)) local resumeItem = cc.MenuItemFont:create("Resume") resumeItem:setAnchorPoint(cc.p(0.0, 0.0)) resumeItem:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 150)) local stopItem = cc.MenuItemFont:create("Stop") stopItem:setAnchorPoint(cc.p(0.0, 0.0)) stopItem:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 200)) local hintItem = cc.MenuItemFont:create("Hint") hintItem:setAnchorPoint(cc.p(0.0, 0.0)) hintItem:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 250)) local resourceVideo = cc.MenuItemFont:create("Play resource video") resourceVideo:setAnchorPoint(cc.p(0, 0)) resourceVideo:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 300)) local onlineVideo = cc.MenuItemFont:create("Play online video") onlineVideo:setAnchorPoint(cc.p(0, 0)) onlineVideo:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 350)) local ratioSwitch = cc.MenuItemFont:create("KeepRatioSwitch") ratioSwitch:setAnchorPoint(cc.p(0, 0)) ratioSwitch:setPosition(cc.p(self.visibleRect.x + self.visibleRect.width - 200,self.visibleRect.y + 400)) local menu = cc.Menu:create(fullSwitch, pauseItem, resumeItem, stopItem, hintItem, resourceVideo, onlineVideo, ratioSwitch) menu:setPosition(cc.p(0,0)) layerMenu:addChild(menu) --视频播放器回调 if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_ANDROID == targetPlatform) then --全屏回调 local function menuFullScreenCallback(tag, sender) if nil ~= videoPlayer then videoPlayer:setFullScreenEnabled(not videoPlayer:isFullScreenEnabled()) end touchStateLabel:setString("FullScreen") end --暂停回调 local function menuPauseCallback(tag, sender) if nil ~= videoPlayer then videoPlayer:pause() end touchStateLabel:setString("Pause") end --返回回调 local function menuResumeCallback(tag, sender) if nil ~= videoPlayer then videoPlayer:resume() end touchStateLabel:setString("Resume") end --停止回调 local function menuStopCallback(tag, sender) if nil ~= videoPlayer then videoPlayer:stop() end touchStateLabel:setString("Stop") end --隐藏回调 local function menuHintCallback(tag, sender) if nil ~= videoPlayer then videoPlayer:setVisible(not videoPlayer:isVisible()) end touchStateLabel:setString("Hint") end --播放本地资源回调 local function menuResourceVideoCallback(tag, sender) if nil ~= videoPlayer then local videoFullPath = cc.FileUtils:getInstance():fullPathForFilename("oceans.mp4") videoPlayer:setFileName(videoFullPath) videoPlayer:play() end touchStateLabel:setString("ResourceVideo") end --播放网络资源回调 local function menuOnlineVideoCallback(tag, sender) touchStateLabel:setString("OnlineVideo") if nil ~= videoPlayer then videoPlayer:seturl("http://benchmark.cocos2d-x.org/cocosvideo.mp4") videoPlayer:play() end end --伸缩比例回调 local function menuRatioCallback(tag, sender) if nil ~= videoPlayer then videoPlayer:setKeepAspectRatioEnabled(not videoPlayer:isKeepAspectRatioEnabled()) end touchStateLabel:setString("Ratio") end fullSwitch:registerScriptTapHandler(menuFullScreenCallback) pauseItem:registerScriptTapHandler(menuPauseCallback) resumeItem:registerScriptTapHandler(menuResumeCallback) stopItem:registerScriptTapHandler(menuStopCallback) hintItem:registerScriptTapHandler(menuHintCallback) resourceVideo:registerScriptTapHandler(menuResourceVideoCallback) onlineVideo:registerScriptTapHandler(menuOnlineVideoCallback) ratioSwitch:registerScriptTapHandler(menuRatioCallback) end self:playBgMusic() return layerMenu end return VideoPlayerScene
相关文章推荐
- cocos2dx3.3视频播放的实现VideoPlayer的使用
- cocos2dx 3.3 视频播放的实现-VideoPlayer的使用
- cocos2dx3.3视频播放的实现VideoPlayer的使用
- cocos2dx 3.15 lua和c++版本 将win32项目打包到android平台遇见的坑与处理办法
- quick-cocos2dx使用videoPlayer播放视频
- cocos2dx 视频播放——VideoPlayer的使用
- JCVideoPlayer朋友圈动态多item视频播放以及使用compile 'fm.jiecao:jiecaovideoplayer:5.5.2'版本在项目中出现的各种异常处理
- Ionic3学习笔记(十四)使用 videogular2 实现视频播放以及遇到的一些问题
- CoreLocation 的基本使用 以及定位 指南针的实现 (附加: 系统版本适配的方法,和后台更新用户位置的方法及注意)
- 关于VideoPlayer以及使用VUforia播放视频的见解
- 使用Qt示例代码实现的视频播放。VS项目
- Ionic3学习笔记(十四)使用 videogular2 实现视频播放以及遇到的一些问题
- C++ 二叉树的实现以及指针使用注意事项
- [zz]Visual C#使用DirectX实现视频播放
- 使用C++实现JNI接口需要注意的事项
- Visual C#使用DirectX实现视频播放
- Visual C#使用DirectX实现视频播放
- 使用NET实现视频播放
- 使用vcastr3.swf实现视频播放功能
- Visual C#使用DirectX实现视频播放