加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 3.X 播放视频

发布时间:2020-12-14 21:21:27 所属栏目:百科 来源:网络整理
导读:一直在纠结于在项目中添加视频的播放! Android 要调用JAVA IOS 要第三方库,总觉得很麻烦有木有!!!! 正文:::::: 首先 : 在.h 要引入两个头文件 #include "ui/UIVideoPlayer.h"#include "ui/CocosGUI.h" 看到 UIVideoPlayer 没? 这就是咱播放的

一直在纠结于在项目中添加视频的播放!

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_BEGIN
namespace 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 style="white-space:pre">	</span> static cocos2d::Scene* createScene();

   <span style="white-space:pre">	</span> virtual bool init();

<span style="white-space:pre">	</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);
};


接下来.cpp

#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 style="color:#ff0000;">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下还测试不了,打包又总是失败,因为就差几个字母,所以找了半天问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读