diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoView.java b/android/src/main/java/com/brentvatne/react/ReactVideoView.java index 3e44bfbe..d47806bb 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoView.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoView.java @@ -9,13 +9,14 @@ import java.util.Map; import java.util.HashMap; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.WritableMap; +import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.events.RCTEventEmitter; import com.yqritc.scalablevideoview.ScalableType; import com.yqritc.scalablevideoview.ScalableVideoView; public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnPreparedListener, MediaPlayer - .OnErrorListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener { + .OnErrorListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, LifecycleEventListener { public enum Events { EVENT_LOAD_START("onVideoLoadStart"), @@ -69,6 +70,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP private boolean mMuted = false; private float mVolume = 1.0f; private float mRate = 1.0f; + private boolean mPlayInBackground = false; private boolean mMediaPlayerValid = false; // True if mMediaPlayer is in prepared, started, or paused state. private int mVideoDuration = 0; @@ -79,6 +81,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP mThemedReactContext = themedReactContext; mEventEmitter = themedReactContext.getJSModule(RCTEventEmitter.class); + themedReactContext.addLifecycleEventListener(this); initializeMediaPlayerIfNeeded(); setSurfaceTextureListener(this); @@ -244,6 +247,10 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP // setRateModifier(mRate); } + public void setPlayInBackground(final boolean playInBackground) { + mPlayInBackground = playInBackground; + } + @Override public void onPrepared(MediaPlayer mp) { mMediaPlayerValid = true; @@ -311,4 +318,19 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP super.onAttachedToWindow(); setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset); } + + @Override + public void onHostPause() { + if (mMediaPlayer != null && !mPlayInBackground) { + mMediaPlayer.pause(); + } + } + + @Override + public void onHostResume() { + } + + @Override + public void onHostDestroy() { + } } diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java index 6439f757..5585adf0 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoViewManager.java @@ -30,6 +30,7 @@ public class ReactVideoViewManager extends SimpleViewManager { public static final String PROP_VOLUME = "volume"; public static final String PROP_SEEK = "seek"; public static final String PROP_RATE = "rate"; + public static final String PROP_PLAY_IN_BACKGROUND = "playInBackground"; @Override public String getName() { @@ -106,4 +107,9 @@ public class ReactVideoViewManager extends SimpleViewManager { public void setRate(final ReactVideoView videoView, final float rate) { videoView.setRateModifier(rate); } + + @ReactProp(name = PROP_PLAY_IN_BACKGROUND, defaultBoolean = false) + public void setPlayInBackground(final ReactVideoView videoView, final boolean playInBackground) { + videoView.setPlayInBackground(playInBackground); + } }