Skip to content

Commit

Permalink
Updated the VideoView to leave the final frame intact when a video ha…
Browse files Browse the repository at this point in the history
…s played to completion (brianwernick#420)
  • Loading branch information
brianwernick committed Apr 1, 2017
1 parent edea532 commit 9ae4e78
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ interface OnSurfaceSizeChanged {

/**
* Performs the functionality to stop the video in playback
*
* @param clearSurface <code>true</code> if the surface should be cleared
*/
void stopPlayback();
void stopPlayback(boolean clearSurface);

/**
* Prepares the media previously specified for playback. This should only be called after
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public void pause() {
}

@Override
public void stopPlayback() {
delegate.stopPlayback();
public void stopPlayback(boolean clearSurface) {
delegate.stopPlayback(clearSurface);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public void pause() {
}

@Override
public void stopPlayback() {
delegate.stopPlayback();
public void stopPlayback(boolean clearSurface) {
delegate.stopPlayback(clearSurface);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,18 @@ public void pause() {
playRequested = false;
}

public void stopPlayback() {
/**
* Performs the functionality to stop the video in playback
*
* @param clearSurface <code>true</code> if the surface should be cleared
*/
public void stopPlayback(boolean clearSurface) {
exoMediaPlayer.stop();
playRequested = false;
listenerMux.clearSurfaceWhenReady(clearableSurface);

if (clearSurface) {
listenerMux.clearSurfaceWhenReady(clearableSurface);
}
}

public void suspend() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ public boolean restart() {
}

@Override
public void stopPlayback() {
delegate.stopPlayback();
public void stopPlayback(boolean clearSurface) {
delegate.stopPlayback(clearSurface);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public boolean restart() {
}

@Override
public void stopPlayback() {
delegate.stopPlayback();
public void stopPlayback(boolean clearSurface) {
delegate.stopPlayback(clearSurface);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public long getCurrentPosition() {

public void seekTo(long milliseconds) {
if (isReady()) {
mediaPlayer.seekTo((int)milliseconds);
mediaPlayer.seekTo((int) milliseconds);
requestedSeek = 0;
} else {
requestedSeek = milliseconds;
Expand Down Expand Up @@ -168,8 +168,10 @@ public boolean setPlaybackSpeed(float speed) {

/**
* Performs the functionality to stop the video in playback
*
* @param clearSurface <code>true</code> if the surface should be cleared
*/
public void stopPlayback() {
public void stopPlayback(boolean clearSurface) {
currentState = State.IDLE;

if (isReady()) {
Expand All @@ -181,7 +183,9 @@ public void stopPlayback() {
}

playRequested = false;
listenerMux.clearSurfaceWhenReady(clearableSurface);
if (clearSurface) {
listenerMux.clearSurfaceWhenReady(clearableSurface);
}
}

/**
Expand All @@ -202,7 +206,7 @@ public void suspend() {
}

public boolean restart() {
if(currentState != State.COMPLETED) {
if (currentState != State.COMPLETED) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,7 @@ public void pause() {
* If a video is currently in playback then the playback will be stopped
*/
public void stopPlayback() {
audioFocusHelper.abandonFocus();
videoViewImpl.stopPlayback();
setKeepScreenOn(false);

if (videoControls != null) {
videoControls.updatePlaybackState(false);
}
stopPlayback(true);
}

/**
Expand Down Expand Up @@ -768,7 +762,25 @@ protected int getVideoViewApiImplementation(@NonNull Context context, @NonNull A
* procedures from running that we no longer need.
*/
protected void onPlaybackEnded() {
stopPlayback();
stopPlayback(false);
}

/**
* Stops the video currently in playback, making sure to only clear the surface
* when requested. This allows us to leave the last frame of a video intact when
* it plays to completion while still clearing it when the user requests playback
* to stop.
*
* @param clearSurface <code>true</code> if the surface should be cleared
*/
protected void stopPlayback(boolean clearSurface) {
audioFocusHelper.abandonFocus();
videoViewImpl.stopPlayback(clearSurface);
setKeepScreenOn(false);

if (videoControls != null) {
videoControls.updatePlaybackState(false);
}
}

protected class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {
Expand Down

0 comments on commit 9ae4e78

Please sign in to comment.