cocos2dx 视频播放器

前端之家收集整理的这篇文章主要介绍了cocos2dx 视频播放器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件

#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

猜你在找的Cocos2d-x相关文章