keep player valid after reaching video end

This commit is contained in:
Marcin Adamczyk 2016-06-29 15:25:28 +02:00
parent 5c422db78d
commit f62453f714

View File

@ -1,22 +1,24 @@
package com.brentvatne.react; package com.brentvatne.react;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.net.Uri;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import java.util.Map;
import java.util.HashMap;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter; import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.yqritc.scalablevideoview.ScalableType; import com.yqritc.scalablevideoview.ScalableType;
import com.yqritc.scalablevideoview.ScalableVideoView; import com.yqritc.scalablevideoview.ScalableVideoView;
import java.util.HashMap;
import java.util.Map;
public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnPreparedListener, MediaPlayer public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnPreparedListener, MediaPlayer
.OnErrorListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnInfoListener, LifecycleEventListener { .OnErrorListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnInfoListener, LifecycleEventListener {
public enum Events { public enum Events {
EVENT_LOAD_START("onVideoLoadStart"), EVENT_LOAD_START("onVideoLoadStart"),
@ -79,10 +81,10 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
private float mRate = 1.0f; private float mRate = 1.0f;
private boolean mPlayInBackground = false; private boolean mPlayInBackground = false;
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, paused or completed state.
private int mVideoDuration = 0; private int mVideoDuration = 0;
private int mVideoBufferedDuration = 0; private int mVideoBufferedDuration = 0;
private boolean isStalled = false; private boolean isCompleted = false;
public ReactVideoView(ThemedReactContext themedReactContext) { public ReactVideoView(ThemedReactContext themedReactContext) {
super(themedReactContext); super(themedReactContext);
@ -98,7 +100,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
@Override @Override
public void run() { public void run() {
if (mMediaPlayerValid) { if (mMediaPlayerValid && !isCompleted) {
WritableMap event = Arguments.createMap(); WritableMap event = Arguments.createMap();
event.putDouble(EVENT_PROP_CURRENT_TIME, mMediaPlayer.getCurrentPosition() / 1000.0); event.putDouble(EVENT_PROP_CURRENT_TIME, mMediaPlayer.getCurrentPosition() / 1000.0);
event.putDouble(EVENT_PROP_PLAYABLE_DURATION, mVideoBufferedDuration / 1000.0); //TODO:mBufferUpdateRunnable event.putDouble(EVENT_PROP_PLAYABLE_DURATION, mVideoBufferedDuration / 1000.0); //TODO:mBufferUpdateRunnable
@ -126,6 +128,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
} }
public void setSrc(final String uriString, final String type, final boolean isNetwork, final boolean isAsset) { public void setSrc(final String uriString, final String type, final boolean isNetwork, final boolean isAsset) {
mSrcUriString = uriString; mSrcUriString = uriString;
mSrcType = type; mSrcType = type;
mSrcIsNetwork = isNetwork; mSrcIsNetwork = isNetwork;
@ -196,6 +199,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
} }
public void setRepeatModifier(final boolean repeat) { public void setRepeatModifier(final boolean repeat) {
mRepeat = repeat; mRepeat = repeat;
if (mMediaPlayerValid) { if (mMediaPlayerValid) {
@ -204,6 +208,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
} }
public void setPausedModifier(final boolean paused) { public void setPausedModifier(final boolean paused) {
mPaused = paused; mPaused = paused;
if (!mMediaPlayerValid) { if (!mMediaPlayerValid) {
@ -258,11 +263,13 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
} }
public void setPlayInBackground(final boolean playInBackground) { public void setPlayInBackground(final boolean playInBackground) {
mPlayInBackground = playInBackground; mPlayInBackground = playInBackground;
} }
@Override @Override
public void onPrepared(MediaPlayer mp) { public void onPrepared(MediaPlayer mp) {
mMediaPlayerValid = true; mMediaPlayerValid = true;
mVideoDuration = mp.getDuration(); mVideoDuration = mp.getDuration();
@ -293,6 +300,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
@Override @Override
public boolean onError(MediaPlayer mp, int what, int extra) { public boolean onError(MediaPlayer mp, int what, int extra) {
WritableMap error = Arguments.createMap(); WritableMap error = Arguments.createMap();
error.putInt(EVENT_PROP_WHAT, what); error.putInt(EVENT_PROP_WHAT, what);
error.putInt(EVENT_PROP_EXTRA, extra); error.putInt(EVENT_PROP_EXTRA, extra);
@ -307,11 +315,9 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
switch (what) { switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START: case MediaPlayer.MEDIA_INFO_BUFFERING_START:
mEventEmitter.receiveEvent(getId(), Events.EVENT_STALLED.toString(), Arguments.createMap()); mEventEmitter.receiveEvent(getId(), Events.EVENT_STALLED.toString(), Arguments.createMap());
isStalled = true;
break; break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END: case MediaPlayer.MEDIA_INFO_BUFFERING_END:
mEventEmitter.receiveEvent(getId(), Events.EVENT_RESUME.toString(), Arguments.createMap()); mEventEmitter.receiveEvent(getId(), Events.EVENT_RESUME.toString(), Arguments.createMap());
isStalled = false;
break; break;
case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START: case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
mEventEmitter.receiveEvent(getId(), Events.EVENT_READY_FOR_DISPLAY.toString(), Arguments.createMap()); mEventEmitter.receiveEvent(getId(), Events.EVENT_READY_FOR_DISPLAY.toString(), Arguments.createMap());
@ -337,35 +343,42 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
mEventEmitter.receiveEvent(getId(), Events.EVENT_SEEK.toString(), event); mEventEmitter.receiveEvent(getId(), Events.EVENT_SEEK.toString(), event);
super.seekTo(msec); super.seekTo(msec);
if (isCompleted && mVideoDuration != 0 && msec < mVideoDuration) {
isCompleted = false;
}
} }
} }
@Override @Override
public void onCompletion(MediaPlayer mp) { public void onCompletion(MediaPlayer mp) {
mMediaPlayerValid = false;
isCompleted = true;
mEventEmitter.receiveEvent(getId(), Events.EVENT_END.toString(), null); mEventEmitter.receiveEvent(getId(), Events.EVENT_END.toString(), null);
} }
@Override @Override
protected void onDetachedFromWindow() { protected void onDetachedFromWindow() {
mMediaPlayerValid = false; mMediaPlayerValid = false;
super.onDetachedFromWindow(); super.onDetachedFromWindow();
} }
@Override @Override
protected void onAttachedToWindow() { protected void onAttachedToWindow() {
super.onAttachedToWindow(); super.onAttachedToWindow();
setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset); setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset);
} }
@Override @Override
public void onHostPause() { public void onHostPause() {
if (mMediaPlayer != null && !mPlayInBackground) { if (mMediaPlayer != null && !mPlayInBackground) {
mMediaPlayer.pause(); mMediaPlayer.pause();
} }
} }
@Override @Override
public void onHostResume() { public void onHostResume() {
} }