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

Cocos2d-3.x_视频播放(Android和iOS平台)

发布时间:2020-12-14 21:04:16 所属栏目:百科 来源:网络整理
导读:#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "ui/CocosGUI.h"USING_NS_CC;class HelloWorld : public cocos2d::Layer{public: static cocos2d::Scene* createScene(); virtual bool init();// 设置视频播放
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "ui/CocosGUI.h"

USING_NS_CC;


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

    virtual bool init();

	// 设置视频播放时的状态的回调函数,必须加上平台判断的宏定义才能不报错
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	void videoEventCallback(Ref* sender,cocos2d::experimental::ui::VideoPlayer::EventType eventType);
#endif

	// 设置视频播放完成时的回调函数
	void videoPlayOverCallback();

    CREATE_FUNC(HelloWorld);

private:

};

#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
	Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	auto sprite = Sprite::create("HelloWorld.png");
	sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x,visibleSize.height / 2 + origin.y));
	this->addChild(sprite,0);

	// 在Cocos2d-3.x中,增加了可以播放视频的API,但必须注意的是:到目前为止,只能在Android和iOS平台进行播放视频,如果在其他的平台,代码则会编译出错,所以需要加上平台判断的宏定义才能编译通过
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	auto videoPlayer = cocos2d::experimental::ui::VideoPlayer::create();
	videoPlayer->setContentSize(visibleSize);
	videoPlayer->setPosition(visibleSize/2.0);
	videoPlayer->setFileName("video.mp4");
	videoPlayer->addEventListener(CC_CALLBACK_2(HelloWorld::videoEventCallback,this));
	videoPlayer->play();
	this->addChild(videoPlayer);
#endif
    return true;
}

void HelloWorld::videoPlayOverCallback()
{

}

#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:
		this->videoPlayOverCallback();
		break;
	default:
		break;
	}
}

#endif

(编辑:李大同)

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

    推荐文章
      热点阅读