[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.
This commit is contained in:
Baris Sencan 2015-11-13 16:24:13 -08:00
parent 9f73f22c62
commit a52f1d5dcf
2 changed files with 40 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package com.brentvatne.react;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.Handler; import android.os.Handler;
import android.util.Log;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.ThemedReactContext;
@ -54,7 +55,8 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
private boolean mRepeat = false; private boolean mRepeat = false;
private boolean mPaused = false; private boolean mPaused = false;
private boolean mMuted = 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 boolean mMediaPlayerValid = false; // True if mMediaPlayer is in prepared, started, or paused state.
private int mVideoDuration = 0; private int mVideoDuration = 0;
@ -73,16 +75,12 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
public void run() { public void run() {
if (mMediaPlayerValid) { if (mMediaPlayerValid) {
try {
WritableMap event = Arguments.createMap(); WritableMap event = Arguments.createMap();
// TODO: Other event properties. // TODO: Other event properties.
event.putDouble(EVENT_PROP_CURRENT_TIME, mMediaPlayer.getCurrentPosition() / 1000.0); event.putDouble(EVENT_PROP_CURRENT_TIME, mMediaPlayer.getCurrentPosition() / 1000.0);
event.putDouble(EVENT_PROP_DURATION, mVideoDuration / 1000.0); event.putDouble(EVENT_PROP_DURATION, mVideoDuration / 1000.0);
event.putDouble(EVENT_PROP_PLAYABLE_DURATION, mVideoDuration/ 1000.0); event.putDouble(EVENT_PROP_PLAYABLE_DURATION, mVideoDuration / 1000.0); // TODO
mEventEmitter.receiveEvent(getId(), Events.EVENT_PROGRESS.toString(), event); mEventEmitter.receiveEvent(getId(), Events.EVENT_PROGRESS.toString(), event);
} catch (Exception e) {
// Do nothing.
}
} }
mProgressUpdateHandler.postDelayed(mProgressUpdateRunnable, 250); mProgressUpdateHandler.postDelayed(mProgressUpdateRunnable, 250);
} }
@ -179,11 +177,21 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
setMutedModifier(mMuted); 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() { public void applyModifiers() {
setResizeModeModifier(mResizeMode); setResizeModeModifier(mResizeMode);
setRepeatModifier(mRepeat); setRepeatModifier(mRepeat);
setPausedModifier(mPaused); setPausedModifier(mPaused);
setMutedModifier(mMuted); setMutedModifier(mMuted);
// setRateModifier(mRate);
} }
@Override @Override
@ -213,6 +221,8 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
@Override @Override
public void seekTo(int msec) { public void seekTo(int msec) {
if (mMediaPlayerValid) {
WritableMap event = Arguments.createMap(); WritableMap event = Arguments.createMap();
event.putDouble(EVENT_PROP_CURRENT_TIME, getCurrentPosition() / 1000.0); event.putDouble(EVENT_PROP_CURRENT_TIME, getCurrentPosition() / 1000.0);
event.putDouble(EVENT_PROP_SEEK_TIME, msec / 1000.0); event.putDouble(EVENT_PROP_SEEK_TIME, msec / 1000.0);
@ -220,6 +230,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
super.seekTo(msec); super.seekTo(msec);
} }
}
@Override @Override
public void onCompletion(MediaPlayer mp) { public void onCompletion(MediaPlayer mp) {

View File

@ -27,6 +27,8 @@ public class ReactVideoViewManager extends SimpleViewManager<ReactVideoView> {
public static final String PROP_PAUSED = "paused"; public static final String PROP_PAUSED = "paused";
public static final String PROP_MUTED = "muted"; public static final String PROP_MUTED = "muted";
public static final String PROP_VOLUME = "volume"; public static final String PROP_VOLUME = "volume";
public static final String PROP_SEEK = "seek";
public static final String PROP_RATE = "rate";
@Override @Override
public String getName() { public String getName() {
@ -110,4 +112,14 @@ public class ReactVideoViewManager extends SimpleViewManager<ReactVideoView> {
public void setVolume(final ReactVideoView videoView, final float volume) { public void setVolume(final ReactVideoView videoView, final float volume) {
videoView.setVolumeModifier(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);
}
} }