2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-04-28 12:48:29 +08:00
|
|
|
Copyright (c) 2022 Bytedance Inc.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-02-24 18:45:25 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
#include "UIVideoPlayerTest.h"
|
2014-05-08 18:14:32 +08:00
|
|
|
|
|
|
|
USING_NS_CC;
|
2015-04-09 12:23:47 +08:00
|
|
|
using namespace cocos2d::ui;
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
VideoPlayerTests::VideoPlayerTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(VideoPlayerTest);
|
2019-04-11 09:33:05 +08:00
|
|
|
ADD_TEST_CASE(SimpleVideoPlayerTest);
|
2015-04-03 14:31:03 +08:00
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
bool VideoPlayerTest::init()
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!UIScene::init())
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-08 18:14:32 +08:00
|
|
|
_visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect();
|
|
|
|
|
|
|
|
MenuItemFont::setFontSize(16);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto fullSwitch =
|
|
|
|
MenuItemFont::create("FullScreenSwitch", CC_CALLBACK_1(VideoPlayerTest::menuFullScreenCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
fullSwitch->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
fullSwitch->setPosition(Vec2(_visibleRect.origin.x + 10, _visibleRect.origin.y + 50));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
auto pauseItem = MenuItemFont::create("Pause", CC_CALLBACK_1(VideoPlayerTest::menuPauseCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
pauseItem->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
pauseItem->setPosition(Vec2(_visibleRect.origin.x + 10, _visibleRect.origin.y + 100));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
auto resumeItem = MenuItemFont::create("Resume", CC_CALLBACK_1(VideoPlayerTest::menuResumeCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
resumeItem->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
resumeItem->setPosition(Vec2(_visibleRect.origin.x + 10, _visibleRect.origin.y + 150));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
auto stopItem = MenuItemFont::create("Stop", CC_CALLBACK_1(VideoPlayerTest::menuStopCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
stopItem->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
stopItem->setPosition(Vec2(_visibleRect.origin.x + 10, _visibleRect.origin.y + 200));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
auto hintItem = MenuItemFont::create("Hint", CC_CALLBACK_1(VideoPlayerTest::menuHintCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
hintItem->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
hintItem->setPosition(Vec2(_visibleRect.origin.x + 10, _visibleRect.origin.y + 250));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------
|
2021-12-28 16:06:23 +08:00
|
|
|
auto resourceVideo =
|
|
|
|
MenuItemFont::create("Play resource video", CC_CALLBACK_1(VideoPlayerTest::menuResourceVideoCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
resourceVideo->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
2021-12-28 16:06:23 +08:00
|
|
|
resourceVideo->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 50));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto onlineVideo =
|
|
|
|
MenuItemFont::create("Play online video", CC_CALLBACK_1(VideoPlayerTest::menuOnlineVideoCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
onlineVideo->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
2021-12-28 16:06:23 +08:00
|
|
|
onlineVideo->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 100));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
auto ratioSwitch = MenuItemFont::create("KeepRatioSwitch", CC_CALLBACK_1(VideoPlayerTest::menuRatioCallback, this));
|
2014-05-15 01:07:09 +08:00
|
|
|
ratioSwitch->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
2021-12-28 16:06:23 +08:00
|
|
|
ratioSwitch->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 150));
|
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
auto loopToggle = MenuItemFont::create("LoopToogle", CC_CALLBACK_1(VideoPlayerTest::menuLoopCallback, this));
|
|
|
|
loopToggle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
2021-12-28 16:06:23 +08:00
|
|
|
loopToggle->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 170));
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = Menu::create(resourceVideo, onlineVideo, ratioSwitch, loopToggle, fullSwitch, pauseItem, resumeItem,
|
|
|
|
stopItem, hintItem, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setPosition(Vec2::ZERO);
|
2014-05-08 18:14:32 +08:00
|
|
|
_uiLayer->addChild(menu);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoStateLabel = Label::createWithSystemFont("IDLE", "Arial", 16);
|
2014-05-15 01:07:09 +08:00
|
|
|
_videoStateLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoStateLabel->setPosition(
|
|
|
|
Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 200));
|
2014-05-08 18:14:32 +08:00
|
|
|
_uiLayer->addChild(_videoStateLabel);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
_loopStatusLabel = Label::createWithSystemFont("(1)", "Arial", 10);
|
2019-04-11 09:33:05 +08:00
|
|
|
_loopStatusLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
2021-12-28 16:06:23 +08:00
|
|
|
_loopStatusLabel->setPosition(
|
|
|
|
Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10, _visibleRect.origin.y + 185));
|
2019-04-11 09:33:05 +08:00
|
|
|
_uiLayer->addChild(_loopStatusLabel);
|
2014-05-08 18:14:32 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-28 12:48:29 +08:00
|
|
|
void VideoPlayerTest::onEnter() {
|
|
|
|
UIScene::onEnter();
|
|
|
|
|
|
|
|
createVideo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoPlayerTest::onExit()
|
|
|
|
{
|
|
|
|
if (_videoPlayer)
|
|
|
|
_videoPlayer->removeFromParent();
|
|
|
|
|
|
|
|
UIScene::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuCloseCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
|
|
|
Director::getInstance()->end();
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuFullScreenCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer->setFullScreenEnabled(!_videoPlayer->isFullScreenEnabled());
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuRatioCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer->setKeepAspectRatioEnabled(!_videoPlayer->isKeepAspectRatioEnabled());
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
void VideoPlayerTest::menuLoopCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
if (_videoPlayer)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer->setLooping(!_videoPlayer->isLooping());
|
2019-04-11 09:33:05 +08:00
|
|
|
_loopStatusLabel->setString(_videoPlayer->isLooping() ? "(OO)" : "(1)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuResourceVideoCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-14 11:18:11 +08:00
|
|
|
_videoPlayer->setFileName("cocosvideo.mp4");
|
|
|
|
_videoPlayer->play();
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuOnlineVideoCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2015-04-16 16:27:09 +08:00
|
|
|
_videoPlayer->setURL("http://benchmark.cocos2d-x.org/cocosvideo.mp4");
|
2014-05-14 11:18:11 +08:00
|
|
|
_videoPlayer->play();
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuPauseCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-14 11:18:11 +08:00
|
|
|
_videoPlayer->pause();
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuResumeCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-14 11:18:11 +08:00
|
|
|
_videoPlayer->resume();
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuStopCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-14 11:18:11 +08:00
|
|
|
_videoPlayer->stop();
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::menuHintCallback(Ref* sender)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-09 11:30:07 +08:00
|
|
|
if (_videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer->setVisible(!_videoPlayer->isVisible());
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::createVideo()
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2022-04-28 12:48:29 +08:00
|
|
|
if (_videoPlayer)
|
|
|
|
return;
|
2021-12-28 16:06:23 +08:00
|
|
|
auto centerPos =
|
|
|
|
Vec2(_visibleRect.origin.x + _visibleRect.size.width / 2, _visibleRect.origin.y + _visibleRect.size.height / 2);
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2014-06-20 11:18:53 +08:00
|
|
|
auto widgetSize = _widget->getContentSize();
|
2014-05-08 18:14:32 +08:00
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
_videoPlayer = VideoPlayer::create();
|
|
|
|
_videoPlayer->setPosition(centerPos);
|
2014-05-15 01:07:09 +08:00
|
|
|
_videoPlayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer->setContentSize(Size(widgetSize.width * 0.4f, widgetSize.height * 0.4f));
|
2014-05-09 11:30:07 +08:00
|
|
|
_uiLayer->addChild(_videoPlayer);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-05-12 11:35:57 +08:00
|
|
|
_videoPlayer->addEventListener(CC_CALLBACK_2(VideoPlayerTest::videoEventCallback, this));
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
|
2014-05-09 11:30:07 +08:00
|
|
|
void VideoPlayerTest::createSlider()
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto centerPos =
|
|
|
|
Vec2(_visibleRect.origin.x + _visibleRect.size.width / 2, _visibleRect.origin.y + _visibleRect.size.height / 2);
|
2014-05-08 18:14:32 +08:00
|
|
|
|
|
|
|
auto hSlider = ui::Slider::create();
|
|
|
|
hSlider->setTouchEnabled(true);
|
|
|
|
hSlider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
hSlider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
hSlider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
hSlider->setPosition(Vec2(centerPos.x, _visibleRect.origin.y + _visibleRect.size.height * 0.15f));
|
2014-05-08 18:14:32 +08:00
|
|
|
hSlider->setPercent(50);
|
2014-05-12 11:17:06 +08:00
|
|
|
hSlider->addEventListener(CC_CALLBACK_2(VideoPlayerTest::sliderCallback, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
_uiLayer->addChild(hSlider, 0, 1);
|
2014-05-08 18:14:32 +08:00
|
|
|
|
|
|
|
auto vSlider = ui::Slider::create();
|
|
|
|
vSlider->setTouchEnabled(true);
|
|
|
|
vSlider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
vSlider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
vSlider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
vSlider->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width * 0.15f, centerPos.y));
|
2014-05-08 18:14:32 +08:00
|
|
|
vSlider->setRotation(90);
|
|
|
|
vSlider->setPercent(50);
|
2014-05-12 11:17:06 +08:00
|
|
|
vSlider->addEventListener(CC_CALLBACK_2(VideoPlayerTest::sliderCallback, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
_uiLayer->addChild(vSlider, 0, 2);
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void VideoPlayerTest::sliderCallback(Ref* sender, ui::Slider::EventType eventType)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2014-05-12 11:17:06 +08:00
|
|
|
if (eventType == Slider::EventType::ON_PERCENTAGE_CHANGED && _videoPlayer)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
Slider* hSlider = (Slider*)this->getChildByTag(1);
|
|
|
|
Slider* vSlider = (Slider*)this->getChildByTag(2);
|
2014-05-08 18:14:32 +08:00
|
|
|
|
|
|
|
auto newPosX = _visibleRect.origin.x + _visibleRect.size.width / 2 + hSlider->getPercent() - 50;
|
|
|
|
auto newPosY = _visibleRect.origin.y + _visibleRect.size.height / 2 + 50 - vSlider->getPercent();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer->setPosition(Vec2(newPosX, newPosY));
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 11:35:57 +08:00
|
|
|
void VideoPlayerTest::videoEventCallback(Ref* sender, VideoPlayer::EventType eventType)
|
2014-05-08 18:14:32 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
switch (eventType)
|
|
|
|
{
|
|
|
|
case VideoPlayer::EventType::PLAYING:
|
|
|
|
_videoStateLabel->setString("PLAYING");
|
|
|
|
break;
|
|
|
|
case VideoPlayer::EventType::PAUSED:
|
|
|
|
_videoStateLabel->setString("PAUSED");
|
|
|
|
break;
|
|
|
|
case VideoPlayer::EventType::STOPPED:
|
|
|
|
_videoStateLabel->setString("STOPPED");
|
|
|
|
break;
|
|
|
|
case VideoPlayer::EventType::COMPLETED:
|
|
|
|
_videoStateLabel->setString("COMPLETED");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-05-08 18:14:32 +08:00
|
|
|
}
|
2014-06-19 19:45:24 +08:00
|
|
|
}
|
2019-04-11 09:33:05 +08:00
|
|
|
|
|
|
|
// Simple Video Test
|
|
|
|
|
|
|
|
SimpleVideoPlayerTest::SimpleVideoPlayerTest()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer = nullptr;
|
|
|
|
_style = cocos2d::ui::VideoPlayer::StyleType::NONE;
|
2019-04-11 09:33:05 +08:00
|
|
|
_userInputEnabled = true;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
_switchUserInputEnabled = nullptr;
|
2021-12-28 16:06:23 +08:00
|
|
|
_switchStyle = nullptr;
|
2019-04-11 09:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleVideoPlayerTest::updateButtonsTexts()
|
|
|
|
{
|
|
|
|
if (_switchUserInputEnabled)
|
|
|
|
{
|
|
|
|
std::string str = _userInputEnabled ? "< User Input Enabled >" : "< User Input Disabled >";
|
|
|
|
_switchUserInputEnabled->setString(str);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
if (_switchStyle)
|
|
|
|
{
|
|
|
|
std::string str = " - ";
|
2021-12-28 16:06:23 +08:00
|
|
|
switch (_style)
|
2019-04-11 09:33:05 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case cocos2d::ui::VideoPlayer::StyleType::NONE:
|
|
|
|
_switchUserInputEnabled->setVisible(false);
|
|
|
|
str = "< NO Sytle >";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::VideoPlayer::StyleType::DEFAULT:
|
|
|
|
str = "< Default Style >";
|
|
|
|
_switchUserInputEnabled->setVisible(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-04-11 09:33:05 +08:00
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
_switchStyle->setString(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleVideoPlayerTest::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!UIScene::init())
|
2019-04-11 09:33:05 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
_visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
MenuItemFont::setFontSize(16);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
_switchStyle =
|
|
|
|
MenuItemFont::create("Switch Style", CC_CALLBACK_1(SimpleVideoPlayerTest::switchStyleCallback, this));
|
2019-04-11 09:33:05 +08:00
|
|
|
_switchStyle->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
_switchStyle->setPosition(Vec2(_visibleRect.origin.x + 10, _visibleRect.origin.y + 50));
|
|
|
|
|
|
|
|
_switchUserInputEnabled =
|
|
|
|
MenuItemFont::create("Enable User Input", CC_CALLBACK_1(SimpleVideoPlayerTest::switchUserInputCallback, this));
|
2019-04-11 09:33:05 +08:00
|
|
|
_switchUserInputEnabled->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
_switchUserInputEnabled->setPosition(Vec2(_visibleRect.origin.x + 10, _visibleRect.origin.y + 100));
|
|
|
|
|
|
|
|
auto menu = Menu::create(_switchUserInputEnabled, _switchStyle, nullptr);
|
2019-04-11 09:33:05 +08:00
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
_uiLayer->addChild(menu);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
updateButtonsTexts();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-28 12:48:29 +08:00
|
|
|
void SimpleVideoPlayerTest::onEnter()
|
|
|
|
{
|
|
|
|
UIScene::onEnter();
|
|
|
|
|
|
|
|
createVideo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleVideoPlayerTest::onExit()
|
|
|
|
{
|
|
|
|
if (_videoPlayer)
|
|
|
|
_videoPlayer->removeFromParent();
|
|
|
|
|
|
|
|
UIScene::onExit();
|
|
|
|
}
|
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
void SimpleVideoPlayerTest::menuCloseCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
Director::getInstance()->end();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
exit(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleVideoPlayerTest::switchStyleCallback(Ref* sender)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
switch (_style)
|
|
|
|
{
|
|
|
|
case cocos2d::ui::VideoPlayer::StyleType::NONE:
|
|
|
|
_style = cocos2d::ui::VideoPlayer::StyleType::DEFAULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::VideoPlayer::StyleType::DEFAULT:
|
|
|
|
_style = cocos2d::ui::VideoPlayer::StyleType::NONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-04-11 09:33:05 +08:00
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
if (_videoPlayer)
|
|
|
|
{
|
|
|
|
_videoPlayer->setStyle(_style);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
updateButtonsTexts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleVideoPlayerTest::switchUserInputCallback(Ref* sender)
|
|
|
|
{
|
|
|
|
_userInputEnabled = !_userInputEnabled;
|
|
|
|
if (_videoPlayer)
|
|
|
|
{
|
|
|
|
_videoPlayer->setUserInputEnabled(_userInputEnabled);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
updateButtonsTexts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleVideoPlayerTest::createVideo()
|
|
|
|
{
|
|
|
|
if (_videoPlayer)
|
|
|
|
{
|
|
|
|
_uiLayer->removeChild(_videoPlayer);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
auto centerPos =
|
|
|
|
Vec2(_visibleRect.origin.x + _visibleRect.size.width / 2, _visibleRect.origin.y + _visibleRect.size.height / 2);
|
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
auto widgetSize = _widget->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
_videoPlayer = VideoPlayer::create();
|
|
|
|
_videoPlayer->setPosition(centerPos);
|
|
|
|
_videoPlayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
2021-12-28 16:06:23 +08:00
|
|
|
_videoPlayer->setContentSize(Size(widgetSize.width * 0.4f, widgetSize.height * 0.4f));
|
2019-04-11 09:33:05 +08:00
|
|
|
_videoPlayer->setLooping(true);
|
|
|
|
_videoPlayer->setStyle(_style);
|
|
|
|
_videoPlayer->setUserInputEnabled(_userInputEnabled);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
_uiLayer->addChild(_videoPlayer);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
// _videoPlayer->addEventListener(CC_CALLBACK_2(SimpleVideoPlayerTest::videoEventCallback, this));
|
|
|
|
|
2019-04-11 09:33:05 +08:00
|
|
|
_videoPlayer->setFileName("cocosvideo.mp4");
|
|
|
|
_videoPlayer->play();
|
|
|
|
}
|