Cocos 播放视屏方法

用cocos自带的UIVideoPlayer库就可以直接播放 废话不说直接上代码

#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H
#include “cocostudio/CocoStudio.h”
#include “ui/CocosGUI.h”
#include “cocos2d.h”
USING_NS_CC;
using namespace cocostudio;
using namespace ui;
class HelloWorld : public cocos2d::Layer
{
public:
// there’s no ‘id’ in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);

// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);

public:
void onEnter();
void videoPlayOverCallback();
void showVideo();
/**

  • 视频播放状态,只有在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
    };

#endif // HELLOWORLD_SCENE_H

cpp
#include “HelloWorldScene.h”

USING_NS_CC;

Scene* HelloWorld::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::create();

// ‘layer’ is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;
}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////
// 2. add a menu item with “X” image, which is clicked to quit the program
// you may modify it.

// add a “close” icon to exit the progress. it’s an autorelease object
auto closeItem = MenuItemImage::create(
“CloseNormal.png”,
“CloseSelected.png”,
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width / 2, visibleSize.height / 2));

// create menu, it’s an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setScale(3);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 100);

/////////////////////////////
// 3. add your codes below…

// add a label shows “Hello World”
// create and initialize a label

auto label = Label::createWithTTF(“Hello World”, “fonts/Marker Felt.ttf”, 24);

// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height - label->getContentSize().height));

// add the label as a child to this layer
this->addChild(label, 1);

/*********************************************/

return true;
}
void HelloWorld::onEnter()
{
Layer::onEnter();
showVideo();
}
void HelloWorld::showVideo()
{
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(size / 2);
videoPlayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
videoPlayer->setContentSize(size);
this->addChild(videoPlayer);
if (videoPlayer)
{
videoPlayer->setFileName(“video_2.mp4”);
videoPlayer->play();
}

videoPlayer->addEventListener(CC_CALLBACK_2(HelloWorld::videoEventCallback, this));

#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
}
/*
*播放完成的回调函数
/
void HelloWorld::videoPlayOverCallback()
{
CCLOG("**************************************");
CCLOG("--------------------------------------");
CCLOG("--------------------------------------");
auto layer = LayerColor::create(Color4B(125, 128, 128,128),800,480);
layer->setPosition(640, 360);
this->addChild(layer);
}
/

*视屏播放的状态
*注意这里的代码,此处代码只有在安卓平台个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:
videoPlayOverCallback();
break;
default:
break;
}
}
#endif

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox(“You pressed the close button. Windows Store Apps do not implement a close button.”,“Alert”);
return;
#endif

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}