Merge pull request #259 from whitecurrant/master

android_missing_events
This commit is contained in:
Stanisław Chmiela 2016-06-29 15:56:00 +02:00 committed by GitHub
commit b6c9a57ff2

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, LifecycleEventListener { .OnErrorListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnInfoListener, LifecycleEventListener {
public enum Events { public enum Events {
EVENT_LOAD_START("onVideoLoadStart"), EVENT_LOAD_START("onVideoLoadStart"),
@ -24,7 +26,10 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
EVENT_ERROR("onVideoError"), EVENT_ERROR("onVideoError"),
EVENT_PROGRESS("onVideoProgress"), EVENT_PROGRESS("onVideoProgress"),
EVENT_SEEK("onVideoSeek"), EVENT_SEEK("onVideoSeek"),
EVENT_END("onVideoEnd"); EVENT_END("onVideoEnd"),
EVENT_STALLED("onPlaybackStalled"),
EVENT_RESUME("onPlaybackResume"),
EVENT_READY_FOR_DISPLAY("onReadyForDisplay");
private final String mName; private final String mName;
@ -76,9 +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 isCompleted = false;
public ReactVideoView(ThemedReactContext themedReactContext) { public ReactVideoView(ThemedReactContext themedReactContext) {
super(themedReactContext); super(themedReactContext);
@ -94,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
@ -116,10 +122,13 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this); mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnInfoListener(this);
} }
} }
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;
@ -190,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) {
@ -198,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) {
@ -252,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();
@ -287,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);
@ -296,6 +310,24 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
return true; return true;
} }
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
mEventEmitter.receiveEvent(getId(), Events.EVENT_STALLED.toString(), Arguments.createMap());
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
mEventEmitter.receiveEvent(getId(), Events.EVENT_RESUME.toString(), Arguments.createMap());
break;
case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
mEventEmitter.receiveEvent(getId(), Events.EVENT_READY_FOR_DISPLAY.toString(), Arguments.createMap());
break;
default:
}
return false;
}
@Override @Override
public void onBufferingUpdate(MediaPlayer mp, int percent) { public void onBufferingUpdate(MediaPlayer mp, int percent) {
mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0); mVideoBufferedDuration = (int) Math.round((double) (mVideoDuration * percent) / 100.0);
@ -311,29 +343,36 @@ 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();
} }