Merge pull request #1082 from react-native-community/bugfix/mediaplayer-bg-cant-pause

Preserve Android MediaPlayer paused prop when backgrounding
This commit is contained in:
Hampton Maxwell 2018-06-22 13:04:51 -07:00 committed by GitHub
commit 28b06aac52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -101,8 +101,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
private float mRate = 1.0f; private float mRate = 1.0f;
private float mActiveRate = 1.0f; private float mActiveRate = 1.0f;
private boolean mPlayInBackground = false; private boolean mPlayInBackground = false;
private boolean mActiveStatePauseStatus = false; private boolean mBackgroundPaused = false;
private boolean mActiveStatePauseStatusInitialized = false;
private int mMainVer = 0; private int mMainVer = 0;
private int mPatchVer = 0; private int mPatchVer = 0;
@ -128,7 +127,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
@Override @Override
public void run() { public void run() {
if (mMediaPlayerValid && !isCompleted &&!mPaused) { if (mMediaPlayerValid && !isCompleted && !mPaused && !mBackgroundPaused) {
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
@ -334,11 +333,6 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
mPaused = paused; mPaused = paused;
if ( !mActiveStatePauseStatusInitialized ) {
mActiveStatePauseStatus = mPaused;
mActiveStatePauseStatusInitialized = true;
}
if (!mMediaPlayerValid) { if (!mMediaPlayerValid) {
return; return;
} }
@ -597,25 +591,27 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP
@Override @Override
public void onHostPause() { public void onHostPause() {
if (mMediaPlayer != null && !mPlayInBackground) { if (mMediaPlayerValid && !mPaused && !mPlayInBackground) {
mActiveStatePauseStatus = mPaused; /* Pause the video in background
* Don't update the paused prop, developers should be able to update it on background
// Pause the video in background * so that when you return to the app the video is paused
setPausedModifier(true); */
mBackgroundPaused = true;
mMediaPlayer.pause();
} }
} }
@Override @Override
public void onHostResume() { public void onHostResume() {
if (mMediaPlayer != null && !mPlayInBackground) { mBackgroundPaused = false;
if (mMediaPlayerValid && !mPlayInBackground && !mPaused) {
new Handler().post(new Runnable() { new Handler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
// Restore original state // Restore original state
setPausedModifier(mActiveStatePauseStatus); setPausedModifier(false);
} }
}); });
} }
} }