Allow resetting of position when setting source
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=129083335
This commit is contained in:
parent
90ceade08f
commit
9a272068b4
@ -125,6 +125,7 @@ public class PlayerActivity extends Activity implements OnKeyListener, OnTouchLi
|
|||||||
private DebugTextViewHelper debugViewHelper;
|
private DebugTextViewHelper debugViewHelper;
|
||||||
private boolean playerNeedsSource;
|
private boolean playerNeedsSource;
|
||||||
|
|
||||||
|
private boolean shouldRestorePosition;
|
||||||
private int playerPeriodIndex;
|
private int playerPeriodIndex;
|
||||||
private long playerPosition;
|
private long playerPosition;
|
||||||
|
|
||||||
@ -278,7 +279,9 @@ public class PlayerActivity extends Activity implements OnKeyListener, OnTouchLi
|
|||||||
player.setTextOutput(subtitleView);
|
player.setTextOutput(subtitleView);
|
||||||
player.setVideoListener(this);
|
player.setVideoListener(this);
|
||||||
player.setVideoSurfaceHolder(surfaceView.getHolder());
|
player.setVideoSurfaceHolder(surfaceView.getHolder());
|
||||||
|
if (shouldRestorePosition) {
|
||||||
player.seekTo(playerPeriodIndex, playerPosition);
|
player.seekTo(playerPeriodIndex, playerPosition);
|
||||||
|
}
|
||||||
player.setPlayWhenReady(true);
|
player.setPlayWhenReady(true);
|
||||||
mediaController.setMediaPlayer(new PlayerControl(player));
|
mediaController.setMediaPlayer(new PlayerControl(player));
|
||||||
mediaController.setPrevNextListeners(new MediaControllerPrevNextClickListener(player, true),
|
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]
|
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
|
||||||
: new ConcatenatingMediaSource(mediaSources);
|
: new ConcatenatingMediaSource(mediaSources);
|
||||||
player.setMediaSource(mediaSource);
|
player.setMediaSource(mediaSource, !shouldRestorePosition);
|
||||||
playerNeedsSource = false;
|
playerNeedsSource = false;
|
||||||
updateButtonVisibilities();
|
updateButtonVisibilities();
|
||||||
}
|
}
|
||||||
@ -368,6 +371,8 @@ public class PlayerActivity extends Activity implements OnKeyListener, OnTouchLi
|
|||||||
debugViewHelper = null;
|
debugViewHelper = null;
|
||||||
playerPeriodIndex = player.getCurrentPeriodIndex();
|
playerPeriodIndex = player.getCurrentPeriodIndex();
|
||||||
playerPosition = player.getCurrentPosition();
|
playerPosition = player.getCurrentPosition();
|
||||||
|
Timeline playerTimeline = player.getCurrentTimeline();
|
||||||
|
shouldRestorePosition = playerTimeline != null && playerTimeline.isFinal();
|
||||||
player.release();
|
player.release();
|
||||||
player = null;
|
player = null;
|
||||||
trackSelector = null;
|
trackSelector = null;
|
||||||
|
@ -246,12 +246,20 @@ public interface ExoPlayer {
|
|||||||
*/
|
*/
|
||||||
int getPlaybackState();
|
int getPlaybackState();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the {@link MediaSource} to play. Equivalent to {@code setMediaSource(mediaSource, true)}.
|
||||||
|
*/
|
||||||
|
void setMediaSource(MediaSource mediaSource);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the {@link MediaSource} to play.
|
* Sets the {@link MediaSource} to play.
|
||||||
*
|
*
|
||||||
* @param mediaSource 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}.
|
* Sets whether playback should proceed when {@link #getPlaybackState()} == {@link #STATE_READY}.
|
||||||
|
@ -97,8 +97,13 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setMediaSource(MediaSource mediaSource) {
|
public void setMediaSource(MediaSource mediaSource) {
|
||||||
|
setMediaSource(mediaSource, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMediaSource(MediaSource mediaSource, boolean resetPosition) {
|
||||||
timeline = null;
|
timeline = null;
|
||||||
internalPlayer.setMediaSource(mediaSource);
|
internalPlayer.setMediaSource(mediaSource, resetPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -140,7 +145,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
|||||||
maskingPeriodIndex = periodIndex;
|
maskingPeriodIndex = periodIndex;
|
||||||
maskingPositionMs = seekToDefaultPosition ? 0 : positionMs;
|
maskingPositionMs = seekToDefaultPosition ? 0 : positionMs;
|
||||||
maskingDurationMs = periodChanging ? ExoPlayer.UNKNOWN_TIME : getDuration();
|
maskingDurationMs = periodChanging ? ExoPlayer.UNKNOWN_TIME : getDuration();
|
||||||
|
|
||||||
pendingSeekAcks++;
|
pendingSeekAcks++;
|
||||||
internalPlayer.seekTo(periodIndex, seekToDefaultPosition ? C.UNSET_TIME_US : positionMs * 1000);
|
internalPlayer.seekTo(periodIndex, seekToDefaultPosition ? C.UNSET_TIME_US : positionMs * 1000);
|
||||||
if (!seekToDefaultPosition) {
|
if (!seekToDefaultPosition) {
|
||||||
|
@ -152,8 +152,9 @@ import java.util.ArrayList;
|
|||||||
handler = new Handler(internalPlaybackThread.getLooper(), this);
|
handler = new Handler(internalPlaybackThread.getLooper(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMediaSource(MediaSource mediaSource) {
|
public void setMediaSource(MediaSource mediaSource, boolean resetPosition) {
|
||||||
handler.obtainMessage(MSG_SET_MEDIA_SOURCE, mediaSource).sendToTarget();
|
handler.obtainMessage(MSG_SET_MEDIA_SOURCE, resetPosition ? 1 : 0, 0, mediaSource)
|
||||||
|
.sendToTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPlayWhenReady(boolean playWhenReady) {
|
public void setPlayWhenReady(boolean playWhenReady) {
|
||||||
@ -161,7 +162,7 @@ import java.util.ArrayList;
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void seekTo(int periodIndex, long positionUs) {
|
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() {
|
public void stop() {
|
||||||
@ -234,7 +235,7 @@ import java.util.ArrayList;
|
|||||||
try {
|
try {
|
||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
case MSG_SET_MEDIA_SOURCE: {
|
case MSG_SET_MEDIA_SOURCE: {
|
||||||
setMediaSourceInternal((MediaSource) msg.obj);
|
setMediaSourceInternal((MediaSource) msg.obj, msg.arg1 != 0);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case MSG_SET_PLAY_WHEN_READY: {
|
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();
|
resetInternal();
|
||||||
|
if (resetPosition) {
|
||||||
|
playbackInfo = new PlaybackInfo(0);
|
||||||
|
playbackInfo.startPositionUs = C.UNSET_TIME_US;
|
||||||
|
playbackInfo.positionUs = C.UNSET_TIME_US;
|
||||||
|
}
|
||||||
this.mediaSource = mediaSource;
|
this.mediaSource = mediaSource;
|
||||||
mediaSource.prepareSource(this);
|
mediaSource.prepareSource(this);
|
||||||
setState(ExoPlayer.STATE_BUFFERING);
|
setState(ExoPlayer.STATE_BUFFERING);
|
||||||
|
@ -329,6 +329,11 @@ public final class SimpleExoPlayer implements ExoPlayer {
|
|||||||
player.setMediaSource(mediaSource);
|
player.setMediaSource(mediaSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMediaSource(MediaSource mediaSource, boolean resetPosition) {
|
||||||
|
player.setMediaSource(mediaSource, resetPosition);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPlayWhenReady(boolean playWhenReady) {
|
public void setPlayWhenReady(boolean playWhenReady) {
|
||||||
player.setPlayWhenReady(playWhenReady);
|
player.setPlayWhenReady(playWhenReady);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user