Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve Android MediaPlayer paused prop when backgrounding #1082

Merged
merged 1 commit into from
Jun 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -589,25 +583,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