Merge pull request #277 from zilongshanren/improve-videoplayer-v1.2

Improve videoplayer v1.2
This commit is contained in:
子龙山人
2016-08-16 16:40:34 +08:00
committed by GitHub
7 changed files with 184 additions and 2 deletions
@@ -26,6 +26,7 @@ package org.cocos2dx.lib;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.SparseArray;
import android.view.View;
@@ -34,16 +35,22 @@ import android.widget.FrameLayout;
import org.cocos2dx.lib.Cocos2dxVideoView.OnVideoEventListener;
import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Cocos2dxVideoHelper {
static VideoHandler sVideoHandler = null;
private SparseArray<Cocos2dxVideoView> mVideoViews = null;
private static SparseArray<Cocos2dxVideoView> mVideoViews = null;
private static Handler sHandler = null;
Cocos2dxVideoHelper()
{
sVideoHandler = new VideoHandler(this);
mVideoViews = new SparseArray<Cocos2dxVideoView>();
sHandler = new Handler(Looper.myLooper());
}
private static int videoTag = 0;
@@ -370,6 +377,76 @@ public class Cocos2dxVideoHelper {
}
}
public static <T> T callInMainThread(Callable<T> call) throws ExecutionException, InterruptedException {
FutureTask<T> task = new FutureTask<T>(call);
sHandler.post(task);
return task.get();
}
public static float getCurrentTime(final int index) {
Callable<Float> callable = new Callable<Float>() {
@Override
public Float call() throws Exception {
Cocos2dxVideoView video = mVideoViews.get(index);
float currentPosition = -1;
if (video != null) {
currentPosition = video.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<Float> callable = new Callable<Float>() {
@Override
public Float call() throws Exception {
Cocos2dxVideoView video = mVideoViews.get(index);
float duration = -1;
if (video != null) {
duration = video.getDuration() / 1000.0f;
}
return new Float(duration);
}
};
try {
return callInMainThread(callable);
} catch (ExecutionException e) {
return -1;
} catch (InterruptedException e) {
return -1;
}
}
public static boolean isPlaying(final int index) {
Callable<Boolean> callable = new Callable<Boolean>() {
@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();
msg.what = VideoTaskSetVisible;
@@ -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}
@@ -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
@@ -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);
+11 -1
View File
@@ -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)
@@ -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
+28
View File
@@ -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;
+7
View File
@@ -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.
*