From d5fbdb1e53510fd4bf45c7d1e46fd4e0c8e3e575 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 15 Aug 2016 19:55:13 +0800 Subject: [PATCH 1/5] add currentTime and duration in iOS --- native/cocos/ui/UIVideoPlayer-ios.mm | 28 ++++++++++++++++++++++++++++ native/cocos/ui/UIVideoPlayer.h | 7 +++++++ 2 files changed, 35 insertions(+) diff --git a/native/cocos/ui/UIVideoPlayer-ios.mm b/native/cocos/ui/UIVideoPlayer-ios.mm index 01417589f8..fb3dd9e931 100644 --- a/native/cocos/ui/UIVideoPlayer-ios.mm +++ b/native/cocos/ui/UIVideoPlayer-ios.mm @@ -45,6 +45,8 @@ using namespace cocos2d::experimental::ui; - (void) resume; - (void) stop; - (void) seekTo:(float) sec; +- (float) currentTime; +- (float) duration; - (void) setVisible:(BOOL) visible; - (void) setKeepRatioEnabled:(bool) enabled; - (void) setFullScreenEnabled:(bool) enabled; @@ -207,6 +209,22 @@ using namespace cocos2d::experimental::ui; } } +-(float) currentTime +{ + if (self.moviePlayer != NULL) { + return [self.moviePlayer currentPlaybackTime]; + } + return -1; +} + +-(float) duration +{ + if (self.moviePlayer != NULL) { + return [self.moviePlayer duration]; + } + return -1; +} + -(void) setVisible:(BOOL)visible { if (self.moviePlayer != NULL) { @@ -409,6 +427,16 @@ void VideoPlayer::seekTo(float sec) } } +float VideoPlayer::currentTime()const +{ + return [((UIVideoViewWrapperIos*)_videoView) currentTime]; +} + +float VideoPlayer::duration()const +{ + return [((UIVideoViewWrapperIos*)_videoView) duration]; +} + bool VideoPlayer::isPlaying() const { return _isPlaying; diff --git a/native/cocos/ui/UIVideoPlayer.h b/native/cocos/ui/UIVideoPlayer.h index 79ebddaa2e..9f672c0fab 100644 --- a/native/cocos/ui/UIVideoPlayer.h +++ b/native/cocos/ui/UIVideoPlayer.h @@ -122,6 +122,13 @@ namespace experimental{ */ virtual void seekTo(float sec); + /** + * Get the current play time, measure in seconds. + */ + float currentTime()const; + + float duration() const; + /** * Checks whether the VideoPlayer is playing. * From c31ae1fe2c1c10ea903ca09e15f2e6bd8a4856db Mon Sep 17 00:00:00 2001 From: "yanli.huang" Date: Tue, 16 Aug 2016 11:43:11 +0800 Subject: [PATCH 2/5] update cocos2d-console commit :add --env param to cocos package --- native/tools/cocos2d-console | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/tools/cocos2d-console b/native/tools/cocos2d-console index 7164d48421..fdad71adcf 160000 --- a/native/tools/cocos2d-console +++ b/native/tools/cocos2d-console @@ -1 +1 @@ -Subproject commit 7164d484212b95578e9827cae595cb2590d648a7 +Subproject commit fdad71adcf580741398d3ef93ac2f835023ef06c From c6ee3598d39935e41ff5276847440c520d98d9e7 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 16 Aug 2016 12:01:23 +0800 Subject: [PATCH 3/5] add android support --- .../org/cocos2dx/lib/Cocos2dxVideoHelper.java | 66 ++++++++++++++++++- ...sb_cocos2dx_experimental_video_auto_api.js | 20 ++++++ .../jsb_cocos2dx_experimental_video_auto.cpp | 38 +++++++++++ .../jsb_cocos2dx_experimental_video_auto.hpp | 2 + native/cocos/ui/UIVideoPlayer-android.cpp | 10 +++ 5 files changed, 135 insertions(+), 1 deletion(-) diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java index cacebbd246..fda28608c7 100644 --- a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java @@ -26,24 +26,34 @@ package org.cocos2dx.lib; import android.graphics.Rect; import android.os.Handler; +import android.os.Looper; import android.os.Message; +import android.util.Log; import android.util.SparseArray; import android.view.View; import android.widget.FrameLayout; +import android.widget.VideoView; import org.cocos2dx.lib.Cocos2dxVideoView.OnVideoEventListener; import java.lang.ref.WeakReference; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; public class Cocos2dxVideoHelper { static VideoHandler sVideoHandler = null; - private SparseArray mVideoViews = null; + private static SparseArray mVideoViews = null; + private static Handler sHandler = null; + Cocos2dxVideoHelper() { sVideoHandler = new VideoHandler(this); mVideoViews = new SparseArray(); + sHandler = new Handler(Looper.myLooper()); } private static int videoTag = 0; @@ -60,6 +70,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 VideoTaskCurrentTime = 13; + private final static int VideoTaskDuration = 14; final static int KeyEventBack = 1000; static class VideoHandler extends Handler{ @@ -370,6 +382,58 @@ public class Cocos2dxVideoHelper { } } + + public static T callInMainThread(Callable call) throws ExecutionException, InterruptedException { + FutureTask task = new FutureTask(call); + sHandler.post(task); + return task.get(); + } + + public static float getCurrentTime(final int index) { + Callable callable = new Callable() { + @Override + public Float call() throws Exception { + Cocos2dxVideoView viedo = mVideoViews.get(index); + float currentPosition = -1; + if (viedo != null) { + currentPosition = viedo.getCurrentPosition() / 1000.0f; + } + return new Float(currentPosition); + } + }; + + try { + return callInMainThread(callable); + } catch (ExecutionException e) { + return -1; + } catch (InterruptedException e) { + return -1; + } + } + + public static float getDuration(final int index) { + Callable callable = new Callable() { + @Override + public Float call() throws Exception { + Cocos2dxVideoView viedo = mVideoViews.get(index); + float duration = -1; + if (viedo != null) { + duration = viedo.getDuration() / 1000.0f; + } + return new Float(duration); + } + }; + + try { + return callInMainThread(callable); + } catch (ExecutionException e) { + return -1; + } catch (InterruptedException e) { + return -1; + } + } + + public static void setVideoVisible(int index, boolean visible) { Message msg = Message.obtain(); msg.what = VideoTaskSetVisible; diff --git a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_experimental_video_auto_api.js b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_experimental_video_auto_api.js index 1751546076..670496d4b2 100644 --- a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_experimental_video_auto_api.js +++ b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_experimental_video_auto_api.js @@ -46,6 +46,16 @@ bool { }, +/** + * @method currentTime + * @return {float} + */ +currentTime : function ( +) +{ + return 0; +}, + /** * @method stop */ @@ -114,6 +124,16 @@ isFullScreenEnabled : function ( return false; }, +/** + * @method duration + * @return {float} + */ +duration : function ( +) +{ + return 0; +}, + /** * @method isPlaying * @return {bool} diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.cpp index 9b93d5ebe9..d1d629b6ae 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.cpp @@ -117,6 +117,24 @@ bool js_cocos2dx_experimental_video_VideoPlayer_setKeepAspectRatioEnabled(JSCont JS_ReportError(cx, "js_cocos2dx_experimental_video_VideoPlayer_setKeepAspectRatioEnabled : wrong number of arguments: %d, was expecting %d", argc, 1); return false; } +bool js_cocos2dx_experimental_video_VideoPlayer_currentTime(JSContext *cx, uint32_t argc, jsval *vp) +{ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + cocos2d::experimental::ui::VideoPlayer* cobj = (cocos2d::experimental::ui::VideoPlayer *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_experimental_video_VideoPlayer_currentTime : Invalid Native Object"); + if (argc == 0) { + double ret = cobj->currentTime(); + jsval jsret = JSVAL_NULL; + jsret = DOUBLE_TO_JSVAL(ret); + args.rval().set(jsret); + return true; + } + + JS_ReportError(cx, "js_cocos2dx_experimental_video_VideoPlayer_currentTime : wrong number of arguments: %d, was expecting %d", argc, 0); + return false; +} bool js_cocos2dx_experimental_video_VideoPlayer_stop(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); @@ -249,6 +267,24 @@ bool js_cocos2dx_experimental_video_VideoPlayer_isFullScreenEnabled(JSContext *c JS_ReportError(cx, "js_cocos2dx_experimental_video_VideoPlayer_isFullScreenEnabled : wrong number of arguments: %d, was expecting %d", argc, 0); return false; } +bool js_cocos2dx_experimental_video_VideoPlayer_duration(JSContext *cx, uint32_t argc, jsval *vp) +{ + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); + js_proxy_t *proxy = jsb_get_js_proxy(obj); + cocos2d::experimental::ui::VideoPlayer* cobj = (cocos2d::experimental::ui::VideoPlayer *)(proxy ? proxy->ptr : NULL); + JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_experimental_video_VideoPlayer_duration : Invalid Native Object"); + if (argc == 0) { + double ret = cobj->duration(); + jsval jsret = JSVAL_NULL; + jsret = DOUBLE_TO_JSVAL(ret); + args.rval().set(jsret); + return true; + } + + JS_ReportError(cx, "js_cocos2dx_experimental_video_VideoPlayer_duration : wrong number of arguments: %d, was expecting %d", argc, 0); + return false; +} bool js_cocos2dx_experimental_video_VideoPlayer_isPlaying(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); @@ -363,6 +399,7 @@ void js_register_cocos2dx_experimental_video_VideoPlayer(JSContext *cx, JS::Hand JS_FN("getURL", js_cocos2dx_experimental_video_VideoPlayer_getURL, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("play", js_cocos2dx_experimental_video_VideoPlayer_play, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("setKeepAspectRatioEnabled", js_cocos2dx_experimental_video_VideoPlayer_setKeepAspectRatioEnabled, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), + JS_FN("currentTime", js_cocos2dx_experimental_video_VideoPlayer_currentTime, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("stop", js_cocos2dx_experimental_video_VideoPlayer_stop, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("setFullScreenEnabled", js_cocos2dx_experimental_video_VideoPlayer_setFullScreenEnabled, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("setFileName", js_cocos2dx_experimental_video_VideoPlayer_setFileName, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), @@ -370,6 +407,7 @@ void js_register_cocos2dx_experimental_video_VideoPlayer(JSContext *cx, JS::Hand JS_FN("isKeepAspectRatioEnabled", js_cocos2dx_experimental_video_VideoPlayer_isKeepAspectRatioEnabled, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("onPlayEvent", js_cocos2dx_experimental_video_VideoPlayer_onPlayEvent, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("isFullScreenEnabled", js_cocos2dx_experimental_video_VideoPlayer_isFullScreenEnabled, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), + JS_FN("duration", js_cocos2dx_experimental_video_VideoPlayer_duration, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("isPlaying", js_cocos2dx_experimental_video_VideoPlayer_isPlaying, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("seekTo", js_cocos2dx_experimental_video_VideoPlayer_seekTo, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FS_END diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.hpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.hpp index 5e854598b2..0fd62dee36 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.hpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_experimental_video_auto.hpp @@ -17,6 +17,7 @@ bool js_cocos2dx_experimental_video_VideoPlayer_getFileName(JSContext *cx, uint3 bool js_cocos2dx_experimental_video_VideoPlayer_getURL(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_play(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_setKeepAspectRatioEnabled(JSContext *cx, uint32_t argc, jsval *vp); +bool js_cocos2dx_experimental_video_VideoPlayer_currentTime(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_stop(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_setFullScreenEnabled(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_setFileName(JSContext *cx, uint32_t argc, jsval *vp); @@ -24,6 +25,7 @@ bool js_cocos2dx_experimental_video_VideoPlayer_setURL(JSContext *cx, uint32_t a bool js_cocos2dx_experimental_video_VideoPlayer_isKeepAspectRatioEnabled(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_onPlayEvent(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_isFullScreenEnabled(JSContext *cx, uint32_t argc, jsval *vp); +bool js_cocos2dx_experimental_video_VideoPlayer_duration(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_isPlaying(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_seekTo(JSContext *cx, uint32_t argc, jsval *vp); bool js_cocos2dx_experimental_video_VideoPlayer_create(JSContext *cx, uint32_t argc, jsval *vp); diff --git a/native/cocos/ui/UIVideoPlayer-android.cpp b/native/cocos/ui/UIVideoPlayer-android.cpp index fc2cb72a0f..8db8dde37c 100644 --- a/native/cocos/ui/UIVideoPlayer-android.cpp +++ b/native/cocos/ui/UIVideoPlayer-android.cpp @@ -313,5 +313,15 @@ void executeVideoCallback(int index,int event) } } +float VideoPlayer::currentTime() const +{ + return JniHelper::callStaticFloatMethod(videoHelperClassName, "getCurrentTime", _videoPlayerIndex); +} + +float VideoPlayer::duration() const +{ + return JniHelper::callStaticFloatMethod(videoHelperClassName, "getDuration", _videoPlayerIndex); +} + #endif From ca1a3d8870d74348f5236429e13efc2c8183d4c3 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 16 Aug 2016 12:52:38 +0800 Subject: [PATCH 4/5] fix android isPlaying API --- .../org/cocos2dx/lib/Cocos2dxVideoHelper.java | 35 +++++++++++++------ native/cocos/ui/UIVideoPlayer-android.cpp | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java index fda28608c7..782e337778 100644 --- a/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java +++ b/native/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxVideoHelper.java @@ -28,17 +28,14 @@ import android.graphics.Rect; import android.os.Handler; import android.os.Looper; import android.os.Message; -import android.util.Log; import android.util.SparseArray; import android.view.View; import android.widget.FrameLayout; -import android.widget.VideoView; import org.cocos2dx.lib.Cocos2dxVideoView.OnVideoEventListener; import java.lang.ref.WeakReference; import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; @@ -70,8 +67,6 @@ 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 VideoTaskCurrentTime = 13; - private final static int VideoTaskDuration = 14; final static int KeyEventBack = 1000; static class VideoHandler extends Handler{ @@ -393,10 +388,10 @@ public class Cocos2dxVideoHelper { Callable callable = new Callable() { @Override public Float call() throws Exception { - Cocos2dxVideoView viedo = mVideoViews.get(index); + Cocos2dxVideoView video = mVideoViews.get(index); float currentPosition = -1; - if (viedo != null) { - currentPosition = viedo.getCurrentPosition() / 1000.0f; + if (video != null) { + currentPosition = video.getCurrentPosition() / 1000.0f; } return new Float(currentPosition); } @@ -415,10 +410,10 @@ public class Cocos2dxVideoHelper { Callable callable = new Callable() { @Override public Float call() throws Exception { - Cocos2dxVideoView viedo = mVideoViews.get(index); + Cocos2dxVideoView video = mVideoViews.get(index); float duration = -1; - if (viedo != null) { - duration = viedo.getDuration() / 1000.0f; + if (video != null) { + duration = video.getDuration() / 1000.0f; } return new Float(duration); } @@ -433,6 +428,24 @@ public class Cocos2dxVideoHelper { } } + public static boolean isPlaying(final int index) { + Callable callable = new Callable() { + @Override + public Boolean call() throws Exception { + Cocos2dxVideoView video = mVideoViews.get(index); + return video != null && video.isPlaying(); + } + }; + + try { + return callInMainThread(callable); + } catch (ExecutionException e) { + return false; + } catch (InterruptedException e) { + return false; + } + } + public static void setVideoVisible(int index, boolean visible) { Message msg = Message.obtain(); diff --git a/native/cocos/ui/UIVideoPlayer-android.cpp b/native/cocos/ui/UIVideoPlayer-android.cpp index 8db8dde37c..4a8f866a2b 100644 --- a/native/cocos/ui/UIVideoPlayer-android.cpp +++ b/native/cocos/ui/UIVideoPlayer-android.cpp @@ -227,7 +227,7 @@ void VideoPlayer::seekTo(float sec) bool VideoPlayer::isPlaying() const { - return _isPlaying; + return JniHelper::callStaticBooleanMethod(videoHelperClassName, "isPlaying", _videoPlayerIndex); } void VideoPlayer::setVisible(bool visible) From 0df076d5084bd877b774fffeaeda0b9624558bd8 Mon Sep 17 00:00:00 2001 From: "yanli.huang" Date: Mon, 22 Aug 2016 16:29:13 +0800 Subject: [PATCH 5/5] update cocos2d-console commit id : cocos package so change pyc --- native/tools/cocos2d-console | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/tools/cocos2d-console b/native/tools/cocos2d-console index fdad71adcf..fc15e7c22c 160000 --- a/native/tools/cocos2d-console +++ b/native/tools/cocos2d-console @@ -1 +1 @@ -Subproject commit fdad71adcf580741398d3ef93ac2f835023ef06c +Subproject commit fc15e7c22ce3345d47d0978054c3eed012ce5076