您的位置:首页 > 移动开发 > Cocos引擎

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

2015-04-12 23:01 274 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: