Allow resetting of position when setting source

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129083335
This commit is contained in:
olly 2016-08-02 03:56:34 -07:00 committed by Oliver Woodman
parent 90ceade08f
commit 9a272068b4
5 changed files with 39 additions and 10 deletions

View File

@ -125,6 +125,7 @@ public class PlayerActivity extends Activity implements OnKeyListener, OnTouchLi
private DebugTextViewHelper debugViewHelper;
private boolean playerNeedsSource;
private boolean shouldRestorePosition;
private int playerPeriodIndex;
private long playerPosition;
@ -278,7 +279,9 @@ public class PlayerActivity extends Activity implements OnKeyListener, OnTouchLi
player.setTextOutput(subtitleView);
player.setVideoListener(this);
player.setVideoSurfaceHolder(surfaceView.getHolder());
player.seekTo(playerPeriodIndex, playerPosition);
if (shouldRestorePosition) {
player.seekTo(playerPeriodIndex, playerPosition);
}
player.setPlayWhenReady(true);
mediaController.setMediaPlayer(new PlayerControl(player));
mediaController.setPrevNextListeners(new MediaControllerPrevNextClickListener(player, true),
@ -319,7 +322,7 @@ public class PlayerActivity extends Activity implements OnKeyListener, OnTouchLi
}
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
: new ConcatenatingMediaSource(mediaSources);
player.setMediaSource(mediaSource);
player.setMediaSource(mediaSource, !shouldRestorePosition);
playerNeedsSource = false;
updateButtonVisibilities();
}
@ -368,6 +371,8 @@ public class PlayerActivity extends Activity implements OnKeyListener, OnTouchLi
debugViewHelper = null;
playerPeriodIndex = player.getCurrentPeriodIndex();
playerPosition = player.getCurrentPosition();
Timeline playerTimeline = player.getCurrentTimeline();
shouldRestorePosition = playerTimeline != null && playerTimeline.isFinal();
player.release();
player = null;
trackSelector = null;

View File

@ -246,12 +246,20 @@ public interface ExoPlayer {
*/
int getPlaybackState();
/**
* Sets the {@link MediaSource} to play. Equivalent to {@code setMediaSource(mediaSource, true)}.
*/
void setMediaSource(MediaSource mediaSource);
/**
* Sets the {@link MediaSource} to play.
*
* @param mediaSource The {@link MediaSource} to play.
* @param resetPosition Whether the playback position should be reset to the source's default
* position. If false, playback will start from the position defined by
* {@link #getCurrentPeriodIndex()} and {@link #getCurrentPosition()}.
*/
void setMediaSource(MediaSource mediaSource);
void setMediaSource(MediaSource mediaSource, boolean resetPosition);
/**
* Sets whether playback should proceed when {@link #getPlaybackState()} == {@link #STATE_READY}.

View File

@ -97,8 +97,13 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Override
public void setMediaSource(MediaSource mediaSource) {
setMediaSource(mediaSource, true);
}
@Override
public void setMediaSource(MediaSource mediaSource, boolean resetPosition) {
timeline = null;
internalPlayer.setMediaSource(mediaSource);
internalPlayer.setMediaSource(mediaSource, resetPosition);
}
@Override
@ -140,7 +145,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
maskingPeriodIndex = periodIndex;
maskingPositionMs = seekToDefaultPosition ? 0 : positionMs;
maskingDurationMs = periodChanging ? ExoPlayer.UNKNOWN_TIME : getDuration();
pendingSeekAcks++;
internalPlayer.seekTo(periodIndex, seekToDefaultPosition ? C.UNSET_TIME_US : positionMs * 1000);
if (!seekToDefaultPosition) {

View File

@ -152,8 +152,9 @@ import java.util.ArrayList;
handler = new Handler(internalPlaybackThread.getLooper(), this);
}
public void setMediaSource(MediaSource mediaSource) {
handler.obtainMessage(MSG_SET_MEDIA_SOURCE, mediaSource).sendToTarget();
public void setMediaSource(MediaSource mediaSource, boolean resetPosition) {
handler.obtainMessage(MSG_SET_MEDIA_SOURCE, resetPosition ? 1 : 0, 0, mediaSource)
.sendToTarget();
}
public void setPlayWhenReady(boolean playWhenReady) {
@ -161,7 +162,7 @@ import java.util.ArrayList;
}
public void seekTo(int periodIndex, long positionUs) {
handler.obtainMessage(MSG_SEEK_TO, periodIndex, -1, positionUs).sendToTarget();
handler.obtainMessage(MSG_SEEK_TO, periodIndex, 0, positionUs).sendToTarget();
}
public void stop() {
@ -234,7 +235,7 @@ import java.util.ArrayList;
try {
switch (msg.what) {
case MSG_SET_MEDIA_SOURCE: {
setMediaSourceInternal((MediaSource) msg.obj);
setMediaSourceInternal((MediaSource) msg.obj, msg.arg1 != 0);
return true;
}
case MSG_SET_PLAY_WHEN_READY: {
@ -328,8 +329,14 @@ import java.util.ArrayList;
}
}
private void setMediaSourceInternal(MediaSource mediaSource) {
private void setMediaSourceInternal(MediaSource mediaSource, boolean resetPosition)
throws ExoPlaybackException {
resetInternal();
if (resetPosition) {
playbackInfo = new PlaybackInfo(0);
playbackInfo.startPositionUs = C.UNSET_TIME_US;
playbackInfo.positionUs = C.UNSET_TIME_US;
}
this.mediaSource = mediaSource;
mediaSource.prepareSource(this);
setState(ExoPlayer.STATE_BUFFERING);

View File

@ -329,6 +329,11 @@ public final class SimpleExoPlayer implements ExoPlayer {
player.setMediaSource(mediaSource);
}
@Override
public void setMediaSource(MediaSource mediaSource, boolean resetPosition) {
player.setMediaSource(mediaSource, resetPosition);
}
@Override
public void setPlayWhenReady(boolean playWhenReady) {
player.setPlayWhenReady(playWhenReady);