From 860428af73c36fe1ebba2744ddd310026a321802 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 16 Aug 2016 12:01:23 +0800 Subject: [PATCH] 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