Skip to content

Commit

Permalink
Merge pull request #1082 from react-native-community/bugfix/mediaplay…
Browse files Browse the repository at this point in the history
…er-bg-cant-pause

Preserve Android MediaPlayer paused prop when backgrounding
  • Loading branch information
cobarx authored Jun 22, 2018
2 parents 1bf0e06 + c1558f1 commit 28b06aa
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions android/src/main/java/com/brentvatne/react/ReactVideoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ public String toString() {
private float mRate = 1.0f;
private float mActiveRate = 1.0f;
private boolean mPlayInBackground = false;
private boolean mActiveStatePauseStatus = false;
private boolean mActiveStatePauseStatusInitialized = false;
private boolean mBackgroundPaused = false;

private int mMainVer = 0;
private int mPatchVer = 0;
Expand All @@ -128,7 +127,7 @@ public ReactVideoView(ThemedReactContext themedReactContext) {
@Override
public void run() {

if (mMediaPlayerValid && !isCompleted &&!mPaused) {
if (mMediaPlayerValid && !isCompleted && !mPaused && !mBackgroundPaused) {
WritableMap event = Arguments.createMap();
event.putDouble(EVENT_PROP_CURRENT_TIME, mMediaPlayer.getCurrentPosition() / 1000.0);
event.putDouble(EVENT_PROP_PLAYABLE_DURATION, mVideoBufferedDuration / 1000.0); //TODO:mBufferUpdateRunnable
Expand Down Expand Up @@ -334,11 +333,6 @@ public void setPausedModifier(final boolean paused) {

mPaused = paused;

if ( !mActiveStatePauseStatusInitialized ) {
mActiveStatePauseStatus = mPaused;
mActiveStatePauseStatusInitialized = true;
}

if (!mMediaPlayerValid) {
return;
}
Expand Down Expand Up @@ -597,25 +591,27 @@ protected void onAttachedToWindow() {

@Override
public void onHostPause() {
if (mMediaPlayer != null && !mPlayInBackground) {
mActiveStatePauseStatus = mPaused;

// Pause the video in background
setPausedModifier(true);
if (mMediaPlayerValid && !mPaused && !mPlayInBackground) {
/* Pause the video in background
* Don't update the paused prop, developers should be able to update it on background
* so that when you return to the app the video is paused
*/
mBackgroundPaused = true;
mMediaPlayer.pause();
}
}

@Override
public void onHostResume() {
if (mMediaPlayer != null && !mPlayInBackground) {
mBackgroundPaused = false;
if (mMediaPlayerValid && !mPlayInBackground && !mPaused) {
new Handler().post(new Runnable() {
@Override
public void run() {
// Restore original state
setPausedModifier(mActiveStatePauseStatus);
setPausedModifier(false);
}
});

}
}

Expand Down

0 comments on commit 28b06aa

Please sign in to comment.