Skip to content

Commit 6259d5d

Browse files
benjaprietodrelaptop
authored andcommitted
Ui video player new style looping input options (#19297)
* Implemented looping and disable user input for uiVideoPlayer * Fixes on how to handle sub states * Added UI Video Player Demo for Loop, style and user input enabled options
1 parent 8978825 commit 6259d5d

File tree

8 files changed

+503
-18
lines changed

8 files changed

+503
-18
lines changed

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class Cocos2dxVideoHelper {
6666
private final static int VideoTaskRestart = 10;
6767
private final static int VideoTaskKeepRatio = 11;
6868
private final static int VideoTaskFullScreen = 12;
69+
private final static int VideoTaskSetLooping = 13;
70+
private final static int VideoTaskSetUserInputEnabled = 14;
6971
final static int KeyEventBack = 1000;
7072

7173
static class VideoHandler extends Handler{
@@ -157,11 +159,24 @@ public void handleMessage(Message msg) {
157159
}
158160
break;
159161
}
162+
case VideoTaskSetLooping: {
163+
Cocos2dxVideoHelper helper = mReference.get();
164+
helper._setLooping(msg.arg1, msg.arg2 != 0);
165+
break;
166+
}
167+
168+
case VideoTaskSetUserInputEnabled: {
169+
Cocos2dxVideoHelper helper = mReference.get();
170+
helper._setUserInputEnabled(msg.arg1, msg.arg2 != 0);
171+
break;
172+
}
173+
160174
case KeyEventBack: {
161175
Cocos2dxVideoHelper helper = mReference.get();
162176
helper.onBackKeyEvent();
163177
break;
164-
}
178+
}
179+
165180
default:
166181
break;
167182
}
@@ -257,6 +272,36 @@ private void _setVideoURL(int index, int videoSource, String videoUrl) {
257272
}
258273
}
259274
}
275+
276+
public static void setLooping(int index, boolean looping) {
277+
Message msg = new Message();
278+
msg.what = VideoTaskSetLooping;
279+
msg.arg1 = index;
280+
msg.arg2 = looping ? 1 : 0;
281+
mVideoHandler.sendMessage(msg);
282+
}
283+
284+
private void _setLooping(int index, boolean looping) {
285+
Cocos2dxVideoView videoView = sVideoViews.get(index);
286+
if (videoView != null) {
287+
videoView.setLooping(looping);
288+
}
289+
}
290+
291+
public static void setUserInputEnabled(int index, boolean enableInput) {
292+
Message msg = new Message();
293+
msg.what = VideoTaskSetUserInputEnabled;
294+
msg.arg1 = index;
295+
msg.arg2 = enableInput ? 1 : 0;
296+
mVideoHandler.sendMessage(msg);
297+
}
298+
299+
private void _setUserInputEnabled(int index, boolean enableInput) {
300+
Cocos2dxVideoView videoView = sVideoViews.get(index);
301+
if (videoView != null) {
302+
videoView.setUserInputEnabled(enableInput);
303+
}
304+
}
260305

261306
public static void setVideoRect(int index, int left, int top, int maxWidth, int maxHeight) {
262307
Message msg = new Message();

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoView.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private void initVideoView() {
198198

199199
@Override
200200
public boolean onTouchEvent(MotionEvent event) {
201-
if((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP)
201+
if( mUserInputEnabled && ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP))
202202
{
203203
if (isPlaying()) {
204204
pause();
@@ -211,6 +211,8 @@ public boolean onTouchEvent(MotionEvent event) {
211211
}
212212

213213
private boolean mIsAssetRouse = false;
214+
private boolean mLooping = false;
215+
private boolean mUserInputEnabled = true;
214216
private String mVideoFilePath = null;
215217
private static final String AssetResourceRoot = "assets/";
216218

@@ -246,6 +248,14 @@ private void setVideoURI(Uri uri, Map<String, String> headers) {
246248
requestLayout();
247249
invalidate();
248250
}
251+
252+
public void setLooping(boolean looping) {
253+
mLooping = looping;
254+
}
255+
256+
public void setUserInputEnabled(boolean enableInput) {
257+
mUserInputEnabled = enableInput;
258+
}
249259

250260
public void stopPlayback() {
251261
if (mMediaPlayer != null) {
@@ -291,6 +301,7 @@ private void openVideo() {
291301
mMediaPlayer.setDisplay(mSurfaceHolder);
292302
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
293303
mMediaPlayer.setScreenOnWhilePlaying(true);
304+
mMediaPlayer.setLooping(mLooping); // CROWDSTAR
294305
//}
295306

296307
mDuration = -1;
@@ -425,8 +436,13 @@ public void onPrepared(MediaPlayer mp) {
425436
public void onCompletion(MediaPlayer mp) {
426437
mCurrentState = STATE_PLAYBACK_COMPLETED;
427438
mTargetState = STATE_PLAYBACK_COMPLETED;
428-
429-
release(true);
439+
440+
// Do not release the player if we are looping as we still need the
441+
// the player resources to exist
442+
if (!mLooping) {
443+
release(true);
444+
}
445+
430446
if (mOnVideoEventListener != null) {
431447
mOnVideoEventListener.onVideoEvent(mViewTag,EVENT_COMPLETED);
432448
}
@@ -438,7 +454,8 @@ public void onCompletion(MediaPlayer mp) {
438454
private static final int EVENT_PAUSED = 1;
439455
private static final int EVENT_STOPPED = 2;
440456
private static final int EVENT_COMPLETED = 3;
441-
457+
private static final int EVENT_ERROR = 4;
458+
442459
public interface OnVideoEventListener
443460
{
444461
void onVideoEvent(int tag,int event);
@@ -451,6 +468,10 @@ public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
451468
mCurrentState = STATE_ERROR;
452469
mTargetState = STATE_ERROR;
453470

471+
if (mOnVideoEventListener != null) {
472+
mOnVideoEventListener.onVideoEvent(mViewTag, EVENT_ERROR);
473+
}
474+
454475
/* If an error handler has been supplied, use it and finish. */
455476
if (mOnErrorListener != null) {
456477
if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {

cocos/ui/UIVideoPlayer-android.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ int createVideoWidgetJNI()
6565
return ret;
6666
}
6767

68+
void setLoopingJNI(int index, bool looping)
69+
{
70+
JniMethodInfo t;
71+
if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "setLooping", "(IZ)V")) {
72+
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, looping);
73+
74+
t.env->DeleteLocalRef(t.classID);
75+
}
76+
}
77+
78+
void setUserInputEnabledJNI(int index, bool enableInput)
79+
{
80+
JniMethodInfo t;
81+
if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "setUserInputEnabled", "(IZ)V")) {
82+
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, enableInput);
83+
84+
t.env->DeleteLocalRef(t.classID);
85+
}
86+
}
87+
6888
//-----------------------------------------------------------------------------------------------------------
6989

7090
using namespace cocos2d::experimental::ui;
@@ -77,6 +97,10 @@ VideoPlayer::VideoPlayer()
7797
, _keepAspectRatioEnabled(false)
7898
, _videoPlayerIndex(-1)
7999
, _eventCallback(nullptr)
100+
, _isPlaying(false)
101+
, _isLooping(false)
102+
, _isUserInputEnabled(true)
103+
, _styleType(StyleType::DEFAULT)
80104
{
81105
_videoPlayerIndex = createVideoWidgetJNI();
82106
s_allVideoPlayers[_videoPlayerIndex] = this;
@@ -109,6 +133,23 @@ void VideoPlayer::setURL(const std::string& videoUrl)
109133
(int)Source::URL,_videoURL);
110134
}
111135

136+
void VideoPlayer::setLooping(bool looping)
137+
{
138+
_isLooping = looping;
139+
setLoopingJNI(_videoPlayerIndex, _isLooping);
140+
}
141+
142+
void VideoPlayer::setUserInputEnabled(bool enableInput)
143+
{
144+
_isUserInputEnabled = enableInput;
145+
setUserInputEnabledJNI(_videoPlayerIndex, enableInput);
146+
}
147+
148+
void VideoPlayer::setStyle(StyleType style)
149+
{
150+
_styleType = style;
151+
}
152+
112153
void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
113154
{
114155
cocos2d::ui::Widget::draw(renderer,transform,flags);
@@ -231,6 +272,16 @@ bool VideoPlayer::isPlaying() const
231272
return _isPlaying;
232273
}
233274

275+
bool VideoPlayer::isLooping() const
276+
{
277+
return _isLooping;
278+
}
279+
280+
bool VideoPlayer::isUserInputEnabled() const
281+
{
282+
return _isUserInputEnabled;
283+
}
284+
234285
void VideoPlayer::setVisible(bool visible)
235286
{
236287
cocos2d::ui::Widget::setVisible(visible);
@@ -294,6 +345,9 @@ void VideoPlayer::copySpecialProperties(Widget *widget)
294345
if (videoPlayer)
295346
{
296347
_isPlaying = videoPlayer->_isPlaying;
348+
_isLooping = videoPlayer->_isLooping;
349+
_isUserInputEnabled = videoPlayer->_isUserInputEnabled;
350+
_styleType = videoPlayer->_styleType;
297351
_fullScreenEnabled = videoPlayer->_fullScreenEnabled;
298352
_fullScreenDirty = videoPlayer->_fullScreenDirty;
299353
_videoURL = videoPlayer->_videoURL;

0 commit comments

Comments
 (0)