From ffcf627c13e566d9bcf75371434389143ca3afe5 Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 10 Apr 2019 18:33:05 -0700 Subject: [PATCH] Ui video player new style looping input options --- .../org/cocos2dx/lib/Cocos2dxVideoHelper.java | 47 ++++- .../org/cocos2dx/lib/Cocos2dxVideoView.java | 29 ++- cocos/ui/UIVideoPlayer-android.cpp | 54 ++++++ cocos/ui/UIVideoPlayer-ios.mm | 90 +++++++++- cocos/ui/UIVideoPlayer.h | 57 +++++- .../UIVideoPlayerTest/UIVideoPlayerTest.cpp | 169 +++++++++++++++++- .../UIVideoPlayerTest/UIVideoPlayerTest.h | 33 ++++ 7 files changed, 468 insertions(+), 11 deletions(-) diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java index ed7dee96eb..beff0786c6 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java @@ -66,6 +66,8 @@ public class Cocos2dxVideoHelper { private final static int VideoTaskRestart = 10; private final static int VideoTaskKeepRatio = 11; private final static int VideoTaskFullScreen = 12; + private final static int VideoTaskSetLooping = 13; + private final static int VideoTaskSetUserInputEnabled = 14; final static int KeyEventBack = 1000; static class VideoHandler extends Handler{ @@ -157,11 +159,24 @@ public class Cocos2dxVideoHelper { } break; } + case VideoTaskSetLooping: { + Cocos2dxVideoHelper helper = mReference.get(); + helper._setLooping(msg.arg1, msg.arg2 != 0); + break; + } + + case VideoTaskSetUserInputEnabled: { + Cocos2dxVideoHelper helper = mReference.get(); + helper._setUserInputEnabled(msg.arg1, msg.arg2 != 0); + break; + } + case KeyEventBack: { Cocos2dxVideoHelper helper = mReference.get(); helper.onBackKeyEvent(); break; - } + } + default: break; } @@ -257,6 +272,36 @@ public class Cocos2dxVideoHelper { } } } + + public static void setLooping(int index, boolean looping) { + Message msg = new Message(); + msg.what = VideoTaskSetLooping; + msg.arg1 = index; + msg.arg2 = looping ? 1 : 0; + mVideoHandler.sendMessage(msg); + } + + private void _setLooping(int index, boolean looping) { + Cocos2dxVideoView videoView = sVideoViews.get(index); + if (videoView != null) { + videoView.setLooping(looping); + } + } + + public static void setUserInputEnabled(int index, boolean enableInput) { + Message msg = new Message(); + msg.what = VideoTaskSetUserInputEnabled; + msg.arg1 = index; + msg.arg2 = enableInput ? 1 : 0; + mVideoHandler.sendMessage(msg); + } + + private void _setUserInputEnabled(int index, boolean enableInput) { + Cocos2dxVideoView videoView = sVideoViews.get(index); + if (videoView != null) { + videoView.setUserInputEnabled(enableInput); + } + } public static void setVideoRect(int index, int left, int top, int maxWidth, int maxHeight) { Message msg = new Message(); diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoView.java index ddf8712018..93ea3e3b13 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoView.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoView.java @@ -198,7 +198,7 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl @Override public boolean onTouchEvent(MotionEvent event) { - if((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) + if( mUserInputEnabled && ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP)) { if (isPlaying()) { pause(); @@ -211,6 +211,8 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl } private boolean mIsAssetRouse = false; + private boolean mLooping = false; + private boolean mUserInputEnabled = true; private String mVideoFilePath = null; private static final String AssetResourceRoot = "assets/"; @@ -246,6 +248,14 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl requestLayout(); invalidate(); } + + public void setLooping(boolean looping) { + mLooping = looping; + } + + public void setUserInputEnabled(boolean enableInput) { + mUserInputEnabled = enableInput; + } public void stopPlayback() { if (mMediaPlayer != null) { @@ -291,6 +301,7 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl mMediaPlayer.setDisplay(mSurfaceHolder); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setScreenOnWhilePlaying(true); + mMediaPlayer.setLooping(mLooping); // CROWDSTAR //} mDuration = -1; @@ -428,8 +439,13 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl public void onCompletion(MediaPlayer mp) { mCurrentState = STATE_PLAYBACK_COMPLETED; mTargetState = STATE_PLAYBACK_COMPLETED; - - release(true); + + // Do not release the player if we are looping as we still need the + // the player resources to exist + if (!mLooping) { + release(true); + } + if (mOnVideoEventListener != null) { mOnVideoEventListener.onVideoEvent(mViewTag,EVENT_COMPLETED); } @@ -441,7 +457,8 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl private static final int EVENT_PAUSED = 1; private static final int EVENT_STOPPED = 2; private static final int EVENT_COMPLETED = 3; - + private static final int EVENT_ERROR = 4; + public interface OnVideoEventListener { void onVideoEvent(int tag,int event); @@ -454,6 +471,10 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl mCurrentState = STATE_ERROR; mTargetState = STATE_ERROR; + if (mOnVideoEventListener != null) { + mOnVideoEventListener.onVideoEvent(mViewTag, EVENT_ERROR); + } + /* If an error handler has been supplied, use it and finish. */ if (mOnErrorListener != null) { if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) { diff --git a/cocos/ui/UIVideoPlayer-android.cpp b/cocos/ui/UIVideoPlayer-android.cpp index f0ad1199f6..344f7960ad 100644 --- a/cocos/ui/UIVideoPlayer-android.cpp +++ b/cocos/ui/UIVideoPlayer-android.cpp @@ -65,6 +65,26 @@ int createVideoWidgetJNI() return ret; } +void setLoopingJNI(int index, bool looping) +{ + JniMethodInfo t; + if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "setLooping", "(IZ)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, looping); + + t.env->DeleteLocalRef(t.classID); + } +} + +void setUserInputEnabledJNI(int index, bool enableInput) +{ + JniMethodInfo t; + if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "setUserInputEnabled", "(IZ)V")) { + t.env->CallStaticVoidMethod(t.classID, t.methodID, index, enableInput); + + t.env->DeleteLocalRef(t.classID); + } +} + //----------------------------------------------------------------------------------------------------------- using namespace cocos2d::experimental::ui; @@ -77,6 +97,10 @@ VideoPlayer::VideoPlayer() , _keepAspectRatioEnabled(false) , _videoPlayerIndex(-1) , _eventCallback(nullptr) +, _isPlaying(false) +, _isLooping(false) +, _isUserInputEnabled(true) +, _styleType(StyleType::DEFAULT) { _videoPlayerIndex = createVideoWidgetJNI(); s_allVideoPlayers[_videoPlayerIndex] = this; @@ -109,6 +133,23 @@ void VideoPlayer::setURL(const std::string& videoUrl) (int)Source::URL,_videoURL); } +void VideoPlayer::setLooping(bool looping) +{ + _isLooping = looping; + setLoopingJNI(_videoPlayerIndex, _isLooping); +} + +void VideoPlayer::setUserInputEnabled(bool enableInput) +{ + _isUserInputEnabled = enableInput; + setUserInputEnabledJNI(_videoPlayerIndex, enableInput); +} + +void VideoPlayer::setStyle(StyleType style) +{ + _styleType = style; +} + void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) { cocos2d::ui::Widget::draw(renderer,transform,flags); @@ -231,6 +272,16 @@ bool VideoPlayer::isPlaying() const return _isPlaying; } +bool VideoPlayer::isLooping() const +{ + return _isLooping; +} + +bool VideoPlayer::isUserInputEnabled() const +{ + return _isUserInputEnabled; +} + void VideoPlayer::setVisible(bool visible) { cocos2d::ui::Widget::setVisible(visible); @@ -294,6 +345,9 @@ void VideoPlayer::copySpecialProperties(Widget *widget) if (videoPlayer) { _isPlaying = videoPlayer->_isPlaying; + _isLooping = videoPlayer->_isLooping; + _isUserInputEnabled = videoPlayer->_isUserInputEnabled; + _styleType = videoPlayer->_styleType; _fullScreenEnabled = videoPlayer->_fullScreenEnabled; _fullScreenDirty = videoPlayer->_fullScreenDirty; _videoURL = videoPlayer->_videoURL; diff --git a/cocos/ui/UIVideoPlayer-ios.mm b/cocos/ui/UIVideoPlayer-ios.mm index 9b9b0867d3..b6c871298a 100644 --- a/cocos/ui/UIVideoPlayer-ios.mm +++ b/cocos/ui/UIVideoPlayer-ios.mm @@ -51,6 +51,10 @@ using namespace cocos2d::experimental::ui; - (void) setKeepRatioEnabled:(BOOL) enabled; - (void) setFullScreenEnabled:(BOOL) enabled; - (BOOL) isFullScreenEnabled; +- (void) setRepeatMode:(MPMovieRepeatMode)repeatMode; +- (void) setControlStyle:(MPMovieControlStyle)controlStyle; +- (void) setUserInteractionEnabled:(BOOL)userInteractionEnabled; + -(id) init:(void*) videoPlayer; @@ -67,7 +71,9 @@ using namespace cocos2d::experimental::ui; int _width; int _height; bool _keepRatioEnabled; - + MPMovieRepeatMode _repeatMode; + MPMovieControlStyle _controlStyle; + BOOL _userInteractionEnabled; VideoPlayer* _videoPlayer; } @@ -75,6 +81,11 @@ using namespace cocos2d::experimental::ui; { if (self = [super init]) { self.moviePlayer = nullptr; + + _repeatMode = MPMovieRepeatModeNone; + _controlStyle = MPMovieControlStyleEmbedded; + _userInteractionEnabled = YES; + _videoPlayer = (VideoPlayer*)videoPlayer; _keepRatioEnabled = false; } @@ -123,6 +134,31 @@ using namespace cocos2d::experimental::ui; return false; } + +-(void) setControlStyle:(MPMovieControlStyle)controlStyle +{ + _controlStyle = controlStyle; + if (self.moviePlayer != nullptr) { + self.moviePlayer.controlStyle = _controlStyle; + } +} + +-(void) setRepeatMode:(MPMovieRepeatMode)repeatMode +{ + _repeatMode = repeatMode; + if (self.moviePlayer != nullptr) { + self.moviePlayer.repeatMode = _repeatMode; + } +} + +-(void) setUserInteractionEnabled:(BOOL)userInteractionEnabled +{ + _userInteractionEnabled = userInteractionEnabled; + if (self.moviePlayer != nullptr) { + self.moviePlayer.view.userInteractionEnabled = _userInteractionEnabled; + } +} + -(void) setURL:(int)videoSource :(std::string &)videoUrl { if (self.moviePlayer != nullptr) { @@ -143,14 +179,17 @@ using namespace cocos2d::experimental::ui; self.moviePlayer.movieSourceType = MPMovieSourceTypeFile; } self.moviePlayer.allowsAirPlay = false; - self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded; - self.moviePlayer.view.userInteractionEnabled = true; + + self.moviePlayer.controlStyle = _controlStyle; + self.moviePlayer.repeatMode = _repeatMode; auto clearColor = [UIColor clearColor]; self.moviePlayer.backgroundView.backgroundColor = clearColor; self.moviePlayer.view.backgroundColor = clearColor; + self.moviePlayer.view.userInteractionEnabled = _userInteractionEnabled; for (UIView * subView in self.moviePlayer.view.subviews) { subView.backgroundColor = clearColor; + subView.userInteractionEnabled = _userInteractionEnabled; } if (_keepRatioEnabled) { @@ -233,6 +272,8 @@ using namespace cocos2d::experimental::ui; if (self.moviePlayer != NULL) { [self.moviePlayer.view setFrame:CGRectMake(_left, _top, _width, _height)]; [self.moviePlayer play]; + self.moviePlayer.view.userInteractionEnabled = _userInteractionEnabled; + self.moviePlayer.repeatMode = _repeatMode; } } @@ -270,6 +311,9 @@ VideoPlayer::VideoPlayer() , _keepAspectRatioEnabled(false) , _videoPlayerIndex(-1) , _eventCallback(nullptr) +, _isLooping(false) +, _isUserInputEnabled(true) +, _styleType(StyleType::DEFAULT) { _videoView = [[UIVideoViewWrapperIos alloc] init:this]; @@ -301,6 +345,33 @@ void VideoPlayer::setURL(const std::string& videoUrl) [((UIVideoViewWrapperIos*)_videoView) setURL:(int)_videoSource :_videoURL]; } +void VideoPlayer::setLooping(bool looping) +{ + _isLooping = looping; + [((UIVideoViewWrapperIos*)_videoView) setRepeatMode:_isLooping ? MPMovieRepeatModeOne : MPMovieRepeatModeNone]; +} + +void VideoPlayer::setUserInputEnabled(bool enableInput) +{ + _isUserInputEnabled = enableInput; + [((UIVideoViewWrapperIos*)_videoView) setUserInteractionEnabled:enableInput]; +} + +void VideoPlayer::setStyle(StyleType style) +{ + _styleType = style; + + switch (style) { + case StyleType::DEFAULT: + [((UIVideoViewWrapperIos*)_videoView) setControlStyle:MPMovieControlStyleEmbedded]; + break; + + case StyleType::NONE: + [((UIVideoViewWrapperIos*)_videoView) setControlStyle:MPMovieControlStyleNone]; + break; + } +} + void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) { cocos2d::ui::Widget::draw(renderer,transform,flags); @@ -403,6 +474,16 @@ bool VideoPlayer::isPlaying() const return _isPlaying; } +bool VideoPlayer::isLooping() const +{ + return _isLooping; +} + +bool VideoPlayer::isUserInputEnabled() const +{ + return _isUserInputEnabled; +} + void VideoPlayer::setVisible(bool visible) { cocos2d::ui::Widget::setVisible(visible); @@ -462,6 +543,9 @@ void VideoPlayer::copySpecialProperties(Widget *widget) if (videoPlayer) { _isPlaying = videoPlayer->_isPlaying; + _isLooping = videoPlayer->_isLooping; + _isUserInputEnabled = videoPlayer->_isUserInputEnabled; + _styleType = videoPlayer->_styleType; _fullScreenEnabled = videoPlayer->_fullScreenEnabled; _fullScreenDirty = videoPlayer->_fullScreenDirty; _videoURL = videoPlayer->_videoURL; diff --git a/cocos/ui/UIVideoPlayer.h b/cocos/ui/UIVideoPlayer.h index 71bc51b702..4e8be1c9c5 100644 --- a/cocos/ui/UIVideoPlayer.h +++ b/cocos/ui/UIVideoPlayer.h @@ -58,7 +58,19 @@ namespace experimental{ PLAYING = 0, PAUSED, STOPPED, - COMPLETED + COMPLETED, + ERROR + }; + + /** + * Styles of how the the video player is presented + * For now only used on iOS to use either MPMovieControlStyleEmbedded (DEFAULT) or + * MPMovieControlStyleNone (NONE) + */ + enum class StyleType + { + DEFAULT = 0, + NONE }; /** @@ -95,6 +107,27 @@ namespace experimental{ * @return A remoting URL address. */ virtual const std::string& getURL() const { return _videoURL;} + + /** + * @brief Set if playback is done in loop mode + * + * @param looping the video will or not automatically restart at the end + */ + virtual void setLooping(bool looping); + + /** + * Set if the player will enable user input for basic pause and resume of video + * + * @param enableInput If true, input will be handled for basic functionality (pause/resume) + */ + virtual void setUserInputEnabled(bool enableInput); + + /** + * Set the style of the player + * + * @param style The corresponding style + */ + virtual void setStyle(StyleType style); /** * Starts playback. @@ -129,6 +162,22 @@ namespace experimental{ * @return True if currently playing, false otherwise. */ virtual bool isPlaying() const; + + /** + * Checks whether the VideoPlayer is set with looping mode. + * + * @return true if the videoplayer is set to loop, false otherwise. + */ + virtual bool isLooping() const; + + + /** + * Checks whether the VideoPlayer is set to listen user input to resume and pause the video + * + * @return true if the videoplayer user input is set, false otherwise. + */ + virtual bool isUserInputEnabled() const; + /** * Causes the video player to keep aspect ratio or no when displaying the video. @@ -196,16 +245,20 @@ namespace experimental{ }; bool _isPlaying; + bool _isLooping; + bool _isUserInputEnabled; bool _fullScreenDirty; bool _fullScreenEnabled; bool _keepAspectRatioEnabled; + StyleType _styleType; + std::string _videoURL; Source _videoSource; int _videoPlayerIndex; ccVideoPlayerCallback _eventCallback; - + void* _videoView; }; } diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.cpp index cbe48a6d73..2e1ac03a6f 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.cpp @@ -31,6 +31,7 @@ using namespace cocos2d::experimental::ui; VideoPlayerTests::VideoPlayerTests() { ADD_TEST_CASE(VideoPlayerTest); + ADD_TEST_CASE(SimpleVideoPlayerTest); } bool VideoPlayerTest::init() @@ -76,8 +77,12 @@ bool VideoPlayerTest::init() auto ratioSwitch = MenuItemFont::create("KeepRatioSwitch", CC_CALLBACK_1(VideoPlayerTest::menuRatioCallback, this)); ratioSwitch->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); ratioSwitch->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10,_visibleRect.origin.y + 150)); + + auto loopToggle = MenuItemFont::create("LoopToogle", CC_CALLBACK_1(VideoPlayerTest::menuLoopCallback, this)); + loopToggle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); + loopToggle->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10,_visibleRect.origin.y + 170)); - auto menu = Menu::create(resourceVideo,onlineVideo,ratioSwitch,fullSwitch,pauseItem,resumeItem,stopItem,hintItem,nullptr); + auto menu = Menu::create(resourceVideo,onlineVideo,ratioSwitch,loopToggle,fullSwitch,pauseItem,resumeItem,stopItem,hintItem,nullptr); menu->setPosition(Vec2::ZERO); _uiLayer->addChild(menu); @@ -85,6 +90,12 @@ bool VideoPlayerTest::init() _videoStateLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); _videoStateLabel->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10,_visibleRect.origin.y + 200)); _uiLayer->addChild(_videoStateLabel); + + _loopStatusLabel = Label::createWithSystemFont("(1)","Arial",10); + _loopStatusLabel->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT); + _loopStatusLabel->setPosition(Vec2(_visibleRect.origin.x + _visibleRect.size.width - 10,_visibleRect.origin.y + 185)); + _uiLayer->addChild(_loopStatusLabel); + createVideo(); return true; @@ -111,6 +122,16 @@ void VideoPlayerTest::menuRatioCallback(Ref* sender) } } +void VideoPlayerTest::menuLoopCallback(Ref* sender) +{ + if (_videoPlayer) + { + _videoPlayer->setLooping(! _videoPlayer->isLooping()); + _loopStatusLabel->setString(_videoPlayer->isLooping() ? "(OO)" : "(1)"); + } +} + + void VideoPlayerTest::menuResourceVideoCallback(Ref* sender) { if (_videoPlayer) @@ -235,3 +256,149 @@ void VideoPlayerTest::videoEventCallback(Ref* sender, VideoPlayer::EventType eve break; } } + + +// Simple Video Test + +SimpleVideoPlayerTest::SimpleVideoPlayerTest() +{ + _videoPlayer = nullptr; + _style = cocos2d::experimental::ui::VideoPlayer::StyleType::NONE; + _userInputEnabled = true; + + _switchUserInputEnabled = nullptr; + _switchStyle = nullptr; +} + +void SimpleVideoPlayerTest::updateButtonsTexts() +{ + if (_switchUserInputEnabled) + { + std::string str = _userInputEnabled ? "< User Input Enabled >" : "< User Input Disabled >"; + _switchUserInputEnabled->setString(str); + } + + if (_switchStyle) + { + std::string str = " - "; + switch(_style) + { + case cocos2d::experimental::ui::VideoPlayer::StyleType::NONE: + _switchUserInputEnabled->setVisible(false); + str = "< NO Sytle >"; + break; + + case cocos2d::experimental::ui::VideoPlayer::StyleType::DEFAULT: + str = "< Default Style >"; + _switchUserInputEnabled->setVisible(true); + break; + + default: + break; + } + + _switchStyle->setString(str); + } +} + +bool SimpleVideoPlayerTest::init() +{ + if ( !UIScene::init() ) + { + return false; + } + + _visibleRect = Director::getInstance()->getOpenGLView()->getVisibleRect(); + + MenuItemFont::setFontSize(16); + + _switchStyle = MenuItemFont::create("Switch Style", CC_CALLBACK_1(SimpleVideoPlayerTest::switchStyleCallback, this)); + _switchStyle->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); + _switchStyle->setPosition(Vec2(_visibleRect.origin.x + 10,_visibleRect.origin.y + 50)); + + _switchUserInputEnabled = MenuItemFont::create("Enable User Input", CC_CALLBACK_1(SimpleVideoPlayerTest::switchUserInputCallback, this)); + _switchUserInputEnabled->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); + _switchUserInputEnabled->setPosition(Vec2(_visibleRect.origin.x + 10,_visibleRect.origin.y + 100)); + + auto menu = Menu::create(_switchUserInputEnabled,_switchStyle,nullptr); + menu->setPosition(Vec2::ZERO); + _uiLayer->addChild(menu); + + createVideo(); + updateButtonsTexts(); + + return true; +} + +void SimpleVideoPlayerTest::menuCloseCallback(Ref* sender) +{ + Director::getInstance()->end(); + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + exit(0); +#endif +} + + +void SimpleVideoPlayerTest::switchStyleCallback(Ref* sender) +{ + switch (_style) { + case cocos2d::experimental::ui::VideoPlayer::StyleType::NONE: + _style = cocos2d::experimental::ui::VideoPlayer::StyleType::DEFAULT; + break; + + case cocos2d::experimental::ui::VideoPlayer::StyleType::DEFAULT: + _style = cocos2d::experimental::ui::VideoPlayer::StyleType::NONE; + break; + + default: + break; + } + + if (_videoPlayer) + { + _videoPlayer->setStyle(_style); + } + + //createVideo(); + updateButtonsTexts(); +} + +void SimpleVideoPlayerTest::switchUserInputCallback(Ref* sender) +{ + _userInputEnabled = !_userInputEnabled; + if (_videoPlayer) + { + _videoPlayer->setUserInputEnabled(_userInputEnabled); + } + + //createVideo(); + updateButtonsTexts(); +} + + +void SimpleVideoPlayerTest::createVideo() +{ + if (_videoPlayer) + { + _uiLayer->removeChild(_videoPlayer); + } + auto centerPos = Vec2(_visibleRect.origin.x + _visibleRect.size.width / 2,_visibleRect.origin.y + _visibleRect.size.height /2); + + auto widgetSize = _widget->getContentSize(); + + _videoPlayer = VideoPlayer::create(); + _videoPlayer->setPosition(centerPos); + _videoPlayer->setAnchorPoint(Vec2::ANCHOR_MIDDLE); + _videoPlayer->setContentSize(Size(widgetSize.width * 0.4f,widgetSize.height * 0.4f)); + _videoPlayer->setLooping(true); + _videoPlayer->setStyle(_style); + _videoPlayer->setUserInputEnabled(_userInputEnabled); + + _uiLayer->addChild(_videoPlayer); + + // _videoPlayer->addEventListener(CC_CALLBACK_2(SimpleVideoPlayerTest::videoEventCallback, this)); + + _videoPlayer->setFileName("cocosvideo.mp4"); + _videoPlayer->play(); +} diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.h b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.h index 2e0de26095..375a54ed25 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.h +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIVideoPlayerTest/UIVideoPlayerTest.h @@ -47,6 +47,7 @@ public: void menuResumeCallback(cocos2d::Ref* sender); void menuStopCallback(cocos2d::Ref* sender); void menuHintCallback(cocos2d::Ref* sender); + void menuLoopCallback(cocos2d::Ref* sender); void sliderCallback(cocos2d::Ref* sender, cocos2d::ui::Slider::EventType eventType); @@ -59,10 +60,42 @@ private: cocos2d::MenuItemFont* _hintItem; cocos2d::experimental::ui::VideoPlayer* _videoPlayer; cocos2d::Label* _videoStateLabel; + cocos2d::Label* _loopStatusLabel; cocos2d::Rect _visibleRect; cocos2d::Layer* _rootLayer; }; + +class SimpleVideoPlayerTest : public UIScene +{ +public: + CREATE_FUNC(SimpleVideoPlayerTest); + + SimpleVideoPlayerTest(); + + virtual bool init() override; + + void menuCloseCallback(cocos2d::Ref* sender); + void switchStyleCallback(cocos2d::Ref* sender); + void switchUserInputCallback(cocos2d::Ref* sender); + +private: + void createVideo(); + + cocos2d::Rect _visibleRect; + cocos2d::experimental::ui::VideoPlayer* _videoPlayer; + + cocos2d::MenuItemFont* _switchUserInputEnabled; + cocos2d::MenuItemFont* _switchStyle; + + + bool _userInputEnabled; + cocos2d::experimental::ui::VideoPlayer::StyleType _style; + + void updateButtonsTexts(); +}; + + #endif // __tests__VideoPlayerTest__