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

cocos2dx 视频播放器

发布时间:2020-12-14 16:57:28 所属栏目:百科 来源:网络整理
导读:头文件: #ifndef __HELLOWORLD_SCENE_H__ # define __HELLOWORLD_SCENE_H__ #include string #include "cocos2d.h" //务必引入以下2个.h文件 #include "ui/UIVideoPlayer.h" #include "ui/CocosGUI.h" using namespace std;USING_NS_CC;class HelloWorld :

头文件:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include <string>
#include "cocos2d.h"
//务必引入以下2个.h文件
#include "ui/UIVideoPlayer.h"
#include "ui/CocosGUI.h"

using namespace std;

USING_NS_CC;
class HelloWorld : public Layer
{
public:
    static Scene* createScene();

    virtual bool init();

    void onEnter();

    void videoPlayOverCallback();

    void showVideo(string fileName);
    /** * 视频播放状态,只有在android和ios平台有效 */
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    void videoEventCallback(Ref* sender,cocos2d::experimental::ui::VideoPlayer::EventType eventType);
#endif

    CREATE_FUNC(HelloWorld);
};
#endif 

源文件:

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}
bool HelloWorld::init()
{
    if (!Layer::init())
    {
        return false;
    }
    return true;
}

void HelloWorld::onEnter(){
    Layer::onEnter();
    showVideo(string("004.mp4"));
}

void HelloWorld::showVideo(string fileName){
    Size size = 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(Point(size.width / 2,size.height / 2));
    videoPlayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    videoPlayer->setContentSize(Size(size.width,size.height));
    this->addChild(videoPlayer);
    if (videoPlayer)
    {
        videoPlayer->setFileName(fileName);
        videoPlayer->play();
    }
    videoPlayer->addEventListener(CC_CALLBACK_2(HelloWorld::videoEventCallback,this));
#endif
}

/** * 视频播放完成的回调函数 */
void HelloWorld::videoPlayOverCallback()
{
}
/** * 视频播放的状态 * 注意这里的代码,此处代码只有在android平台和Ios平台有效 */
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
void HelloWorld::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:
        break;
    default:
        break;
    }
}
#endif

(编辑:李大同)

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

    推荐文章
      热点阅读