#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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。