一直在纠结于在项目中添加视频的播放!
AndroID 要调用JAVA IOS 要第三方库,总觉得很麻烦有木有!!!!
正文::::::
首先 : 在.h 要引入两个头文件
#include "ui/UIVIDeoPlayer.h"#include "ui/CocosGUI.h"
看到 UIVIDeoPlayer 没? 这就是咱播放的家伙,咱进去看看他的庐山真面目
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)#include "ui/UIWidget.h"NS_CC_BEGINnamespace experimental{ namespace ui{ class VIDeoPlayer : public cocos2d::ui::Widget { public: //播放的四个状态 enum class EventType { PLAYING = 0,PAUSED,StopPED,COMPLETED }; typedef std::function<voID(Ref*,VIDeoPlayer::EventType)> ccVIDeoPlayerCallback; CREATE_FUNC(VIDeoPlayer); //Sets local file[support assets' file on androID] as a vIDeo source for VIDeoPlayer virtual voID setfilename(const std::string& vIDeoPath);// 视频的文件名 virtual const std::string& getfilename() const { return _vIDeoURL;} //Sets network link as a vIDeo source for VIDeoPlayer virtual voID setURL(const std::string& _vIDeoURL); //URL 额……都懂的吧 virtual const std::string& getURL() const { return _vIDeoURL;} // 额……也都懂的吧(不懂得童鞋可以翻译一下) virtual voID play(); virtual voID pause(); virtual voID resume(); virtual voID stop(); virtual voID seekTo(float sec); //跳转到视频时长 (输入的时间,不要超过视频的时长) virtual bool isPlaying() const; //获取播放状态 virtual voID setVisible(bool visible) overrIDe;//是否显示 virtual voID setKeepAspectRatioEnabled(bool enable);//保持长宽比 virtual bool isKeepAspectRatioEnabled()const { return _keepAspectRatioEnabled;} virtual voID setFullScreenEnabled(bool enabled);//全屏启用 virtual bool isFullScreenEnabled()const; virtual voID addEventListener(const VIDeoPlayer::ccVIDeoPlayerCallback& callback);//回调函数 virtual voID onPlayEvent(int event); virtual voID draw(Renderer *renderer,const Mat4& transform,uint32_t flags) overrIDe; protected: virtual cocos2d::ui::Widget* createCloneInstance() overrIDe; virtual voID copySpecialPropertIEs(Widget* model) overrIDe; CC_CONSTRUCTOR_ACCESS: VIDeoPlayer(); virtual ~VIDeoPlayer(); protected:#if CC_VIDEOPLAYER_DEBUG_DRAW DrawNode *_deBUGDrawNode;#endif enum class Source { filename = 0,URL }; bool _isPlaying; bool _fullScreenDirty; bool _fullScreenEnabled; bool _keepAspectRatioEnabled; std::string _vIDeoURL; Source _vIDeoSource; int _vIDeoPlayerIndex; ccVIDeoPlayerCallback _eventCallback; voID* _vIDeoVIEw; }; }}NS_CC_END#endif
添加注释的就是常用的几个API !!! 最后直接上我的源码吧!!
先来VIDeoPlayer.h
#ifndef __VIDeoPlayer__#define __VIDeoPlayer__#include "cocos2d.h"#include "ui/UIVIDeoPlayer.h"#include "ui/CocosGUI.h"class VIDeoPlayer : public cocos2d::Layer{public: <span > </span> static cocos2d::Scene* createScene(); <span > </span> virtual bool init();<span > </span>voID vIDeoPlayOverCallBack(); voID showVIDeo();#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) voID vIDeoEventCallback(Ref* sender,cocos2d::experimental::ui::VIDeoPlayer::EventType eventType);#endif // implement the "static create()" method manually CREATE_FUNC(VIDeoPlayer);};
#include "VIDeoPlayer.h"#include "layer/LoadingScene.h"USING_NS_CC;Scene* VIDeoPlayer::createScene(){ auto scene = Scene::create(); auto layer = VIDeoPlayer::create(); scene->addChild(layer); return scene;}bool VIDeoPlayer::init(){ if ( !Layer::init() ) { return false; } this->showVIDeo(); return true;}voID VIDeoPlayer::showVIDeo(){ Size visibleSize = Director::getInstance()->getVisibleSize();#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) auto vIDeoPlayer = cocos2d::experimental::ui::VIDeoPlayer::create(); vIDeoPlayer->setposition(visibleSize/2); vIDeoPlayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE); vIDeoPlayer->setContentSize(visibleSize); this->addChild(vIDeoPlayer); if (vIDeoPlayer) { vIDeoPlayer->setfilename("v0.3gp"); vIDeoPlayer->play(); }else { this->vIDeoPlayOverCallBack(); } <span >vIDeoPlayer->addEventListener(CC_CALLBACK_2(VIDeoPlayer::vIDeoEventCallback,this));</span>#endif#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) this->vIDeoPlayOverCallBack();#endif}voID VIDeoPlayer::vIDeoPlayOverCallBack(){ auto scene = LoadingScene::scene(0.2f,enSceneType_Main); Director::getInstance()->replaceScene(scene);}#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)voID VIDeoPlayer::vIDeoEventCallback(Ref* sender,cocos2d::experimental::ui::VIDeoPlayer::EventType eventType){ switch (eventType) { case cocos2d::experimental::ui::VIDeoPlayer::EventType::PLAYING: break; case cocos2d::experimental::ui::VIDeoPlayer::EventType::PAUSED: break; case cocos2d::experimental::ui::VIDeoPlayer::EventType::StopPED: break; case cocos2d::experimental::ui::VIDeoPlayer::EventType::COMPLETED: this->vIDeoPlayOverCallBack(); break; default: break; }}#endif
其实一共也就这么点东西! 自己写一遍基本上就会了
有一点一定要注意下 : 就是回调函数 标红哪里
我就是习惯了写 addtouchEventListener 惯性的就写成这个了, 在win下还测试不了,打包又总是失败,因为就差几个字母,所以找了半天问题
总结以上是内存溢出为你收集整理的cocos2dx 3.X 播放视频全部内容,希望文章能够帮你解决cocos2dx 3.X 播放视频所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)