From a52f1d5dcfc13e9ee5e460d703ec80b080b075ae Mon Sep 17 00:00:00 2001 From: Baris Sencan Date: Fri, 13 Nov 2015 16:24:13 -0800 Subject: [PATCH] [Android] Implement seek and rate properties rate won't be supported on Android for a while as the native MediaPlayer class doesn't support it either. --- .../com/brentvatne/react/ReactVideoView.java | 45 ++++++++++++------- .../react/ReactVideoViewManager.java | 12 +++++ 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoView.java b/android/src/main/java/com/brentvatne/react/ReactVideoView.java index 21bc4591..a3b16eaf 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoView.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoView.java @@ -2,6 +2,7 @@ package com.brentvatne.react; import android.media.MediaPlayer; import android.os.Handler; +import android.util.Log; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.WritableMap; import com.facebook.react.uimanager.ThemedReactContext; @@ -54,7 +55,8 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP private boolean mRepeat = false; private boolean mPaused = false; private boolean mMuted = false; - private float mVolume = 1; + private float mVolume = 1.0f; + private float mRate = 1.0f; private boolean mMediaPlayerValid = false; // True if mMediaPlayer is in prepared, started, or paused state. private int mVideoDuration = 0; @@ -73,16 +75,12 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP public void run() { if (mMediaPlayerValid) { - try { - WritableMap event = Arguments.createMap(); - // TODO: Other event properties. - event.putDouble(EVENT_PROP_CURRENT_TIME, mMediaPlayer.getCurrentPosition() / 1000.0); - event.putDouble(EVENT_PROP_DURATION, mVideoDuration / 1000.0); - event.putDouble(EVENT_PROP_PLAYABLE_DURATION, mVideoDuration/ 1000.0); - mEventEmitter.receiveEvent(getId(), Events.EVENT_PROGRESS.toString(), event); - } catch (Exception e) { - // Do nothing. - } + WritableMap event = Arguments.createMap(); + // TODO: Other event properties. + event.putDouble(EVENT_PROP_CURRENT_TIME, mMediaPlayer.getCurrentPosition() / 1000.0); + event.putDouble(EVENT_PROP_DURATION, mVideoDuration / 1000.0); + event.putDouble(EVENT_PROP_PLAYABLE_DURATION, mVideoDuration / 1000.0); // TODO + mEventEmitter.receiveEvent(getId(), Events.EVENT_PROGRESS.toString(), event); } mProgressUpdateHandler.postDelayed(mProgressUpdateRunnable, 250); } @@ -179,11 +177,21 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP setMutedModifier(mMuted); } + public void setRateModifier(final float rate) { + mRate = rate; + + if (mMediaPlayerValid) { + // TODO: Implement this. + Log.e(ReactVideoViewManager.REACT_CLASS, "Setting playback rate is not yet supported on Android"); + } + } + public void applyModifiers() { setResizeModeModifier(mResizeMode); setRepeatModifier(mRepeat); setPausedModifier(mPaused); setMutedModifier(mMuted); +// setRateModifier(mRate); } @Override @@ -213,12 +221,15 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP @Override public void seekTo(int msec) { - WritableMap event = Arguments.createMap(); - event.putDouble(EVENT_PROP_CURRENT_TIME, getCurrentPosition() / 1000.0); - event.putDouble(EVENT_PROP_SEEK_TIME, msec / 1000.0); - mEventEmitter.receiveEvent(getId(), Events.EVENT_SEEK.toString(), event); - super.seekTo(msec); + if (mMediaPlayerValid) { + WritableMap event = Arguments.createMap(); + event.putDouble(EVENT_PROP_CURRENT_TIME, getCurrentPosition() / 1000.0); + event.putDouble(EVENT_PROP_SEEK_TIME, msec / 1000.0); + mEventEmitter.receiveEvent(getId(), Events.EVENT_SEEK.toString(), event); + + super.seekTo(msec); + } } @Override @@ -236,7 +247,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); - + try { setSrc(mUriString, mIsNetwork); } catch (Exception e) { diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java index 9645e777..aa5131ad 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java @@ -27,6 +27,8 @@ public class ReactVideoViewManager extends SimpleViewManager { public static final String PROP_PAUSED = "paused"; public static final String PROP_MUTED = "muted"; public static final String PROP_VOLUME = "volume"; + public static final String PROP_SEEK = "seek"; + public static final String PROP_RATE = "rate"; @Override public String getName() { @@ -110,4 +112,14 @@ public class ReactVideoViewManager extends SimpleViewManager { public void setVolume(final ReactVideoView videoView, final float volume) { videoView.setVolumeModifier(volume); } + + @ReactProp(name = PROP_SEEK) + public void setSeek(final ReactVideoView videoView, final float seek) { + videoView.seekTo(Math.round(seek * 1000.0f)); + } + + @ReactProp(name = PROP_RATE) + public void setRate(final ReactVideoView videoView, final float rate) { + videoView.setRateModifier(rate); + } }