mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Use live providers in CompositionPlayer to remove workaround
When CompositionPlayer added repeat modes in d0afb96c40
, it
changed the logic in SimpleBasePlayer to workaround the specific
scenario in CompositionPlayer that didn't seem to work correctly.
The workaround is not really generically applicable though. The
source of the original problem was that the position values of
all state objects were connected to the "live" source of the
current player so that the repetition case couldn't be detected
(the position before and after the seek looked the same). The
solution is to use the newly added LivePositionSuppliers and
disconnect them before seeking.
PiperOrigin-RevId: 713659638
This commit is contained in:
parent
9608ae4e3d
commit
b9d12837b4
@ -4064,14 +4064,10 @@ public abstract class SimpleBasePlayer extends BasePlayer {
|
|||||||
}
|
}
|
||||||
// Only mark changes within the current item as a transition if we are repeating automatically
|
// Only mark changes within the current item as a transition if we are repeating automatically
|
||||||
// or via a seek to next/previous.
|
// or via a seek to next/previous.
|
||||||
if (positionDiscontinuityReason == DISCONTINUITY_REASON_AUTO_TRANSITION) {
|
if (positionDiscontinuityReason == DISCONTINUITY_REASON_AUTO_TRANSITION
|
||||||
if ((getContentPositionMsInternal(previousState, window)
|
&& getContentPositionMsInternal(previousState, window)
|
||||||
> getContentPositionMsInternal(newState, window))
|
> getContentPositionMsInternal(newState, window)) {
|
||||||
|| (newState.hasPositionDiscontinuity
|
return MEDIA_ITEM_TRANSITION_REASON_REPEAT;
|
||||||
&& newState.discontinuityPositionMs == C.TIME_UNSET
|
|
||||||
&& isRepeatingCurrentItem)) {
|
|
||||||
return MEDIA_ITEM_TRANSITION_REASON_REPEAT;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (positionDiscontinuityReason == DISCONTINUITY_REASON_SEEK && isRepeatingCurrentItem) {
|
if (positionDiscontinuityReason == DISCONTINUITY_REASON_SEEK && isRepeatingCurrentItem) {
|
||||||
return MEDIA_ITEM_TRANSITION_REASON_SEEK;
|
return MEDIA_ITEM_TRANSITION_REASON_SEEK;
|
||||||
|
@ -258,6 +258,7 @@ public final class CompositionPlayer extends SimpleBasePlayer
|
|||||||
COMMAND_STOP,
|
COMMAND_STOP,
|
||||||
COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM,
|
COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM,
|
||||||
COMMAND_SEEK_TO_NEXT_MEDIA_ITEM,
|
COMMAND_SEEK_TO_NEXT_MEDIA_ITEM,
|
||||||
|
COMMAND_SEEK_TO_DEFAULT_POSITION,
|
||||||
COMMAND_SEEK_BACK,
|
COMMAND_SEEK_BACK,
|
||||||
COMMAND_SEEK_FORWARD,
|
COMMAND_SEEK_FORWARD,
|
||||||
COMMAND_GET_CURRENT_MEDIA_ITEM,
|
COMMAND_GET_CURRENT_MEDIA_ITEM,
|
||||||
@ -307,7 +308,12 @@ public final class CompositionPlayer extends SimpleBasePlayer
|
|||||||
@Nullable private SurfaceHolder surfaceHolder;
|
@Nullable private SurfaceHolder surfaceHolder;
|
||||||
@Nullable private Surface displaySurface;
|
@Nullable private Surface displaySurface;
|
||||||
private boolean repeatingCompositionSeekInProgress;
|
private boolean repeatingCompositionSeekInProgress;
|
||||||
|
private LivePositionSupplier positionSupplier;
|
||||||
|
private LivePositionSupplier bufferedPositionSupplier;
|
||||||
|
private LivePositionSupplier totalBufferedDurationSupplier;
|
||||||
|
|
||||||
|
// "this" reference for position suppliers.
|
||||||
|
@SuppressWarnings("initialization:methodref.receiver.bound.invalid")
|
||||||
private CompositionPlayer(Builder builder) {
|
private CompositionPlayer(Builder builder) {
|
||||||
super(checkNotNull(builder.looper), builder.clock);
|
super(checkNotNull(builder.looper), builder.clock);
|
||||||
context = builder.context;
|
context = builder.context;
|
||||||
@ -322,6 +328,9 @@ public final class CompositionPlayer extends SimpleBasePlayer
|
|||||||
compositionDurationUs = C.TIME_UNSET;
|
compositionDurationUs = C.TIME_UNSET;
|
||||||
playbackState = STATE_IDLE;
|
playbackState = STATE_IDLE;
|
||||||
volume = 1.0f;
|
volume = 1.0f;
|
||||||
|
positionSupplier = new LivePositionSupplier(this::getContentPositionMs);
|
||||||
|
bufferedPositionSupplier = new LivePositionSupplier(this::getBufferedPositionMs);
|
||||||
|
totalBufferedDurationSupplier = new LivePositionSupplier(this::getTotalBufferedDurationMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -446,9 +455,9 @@ public final class CompositionPlayer extends SimpleBasePlayer
|
|||||||
.setPlayWhenReady(playWhenReady, playWhenReadyChangeReason)
|
.setPlayWhenReady(playWhenReady, playWhenReadyChangeReason)
|
||||||
.setRepeatMode(repeatMode)
|
.setRepeatMode(repeatMode)
|
||||||
.setVolume(volume)
|
.setVolume(volume)
|
||||||
.setContentPositionMs(this::getContentPositionMs)
|
.setContentPositionMs(positionSupplier)
|
||||||
.setContentBufferedPositionMs(this::getBufferedPositionMs)
|
.setContentBufferedPositionMs(bufferedPositionSupplier)
|
||||||
.setTotalBufferedDurationMs(this::getTotalBufferedDurationMs)
|
.setTotalBufferedDurationMs(totalBufferedDurationSupplier)
|
||||||
.setNewlyRenderedFirstFrame(getRenderedFirstFrameAndReset());
|
.setNewlyRenderedFirstFrame(getRenderedFirstFrameAndReset());
|
||||||
if (repeatingCompositionSeekInProgress) {
|
if (repeatingCompositionSeekInProgress) {
|
||||||
state.setPositionDiscontinuity(DISCONTINUITY_REASON_AUTO_TRANSITION, C.TIME_UNSET);
|
state.setPositionDiscontinuity(DISCONTINUITY_REASON_AUTO_TRANSITION, C.TIME_UNSET);
|
||||||
@ -565,6 +574,7 @@ public final class CompositionPlayer extends SimpleBasePlayer
|
|||||||
@Override
|
@Override
|
||||||
protected ListenableFuture<?> handleSeek(
|
protected ListenableFuture<?> handleSeek(
|
||||||
int mediaItemIndex, long positionMs, @Player.Command int seekCommand) {
|
int mediaItemIndex, long positionMs, @Player.Command int seekCommand) {
|
||||||
|
resetLivePositionSuppliers();
|
||||||
CompositionPlayerInternal compositionPlayerInternal =
|
CompositionPlayerInternal compositionPlayerInternal =
|
||||||
checkStateNotNull(this.compositionPlayerInternal);
|
checkStateNotNull(this.compositionPlayerInternal);
|
||||||
compositionPlayerInternal.startSeek(positionMs);
|
compositionPlayerInternal.startSeek(positionMs);
|
||||||
@ -964,11 +974,7 @@ public final class CompositionPlayer extends SimpleBasePlayer
|
|||||||
|
|
||||||
private void repeatCompositionPlayback() {
|
private void repeatCompositionPlayback() {
|
||||||
repeatingCompositionSeekInProgress = true;
|
repeatingCompositionSeekInProgress = true;
|
||||||
seekTo(
|
seekToDefaultPosition();
|
||||||
getCurrentMediaItemIndex(),
|
|
||||||
/* positionMs= */ C.TIME_UNSET,
|
|
||||||
Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM,
|
|
||||||
/* isRepeatingCurrentItem= */ true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ImmutableList<MediaItemData> createPlaylist() {
|
private ImmutableList<MediaItemData> createPlaylist() {
|
||||||
@ -980,6 +986,15 @@ public final class CompositionPlayer extends SimpleBasePlayer
|
|||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void resetLivePositionSuppliers() {
|
||||||
|
positionSupplier.disconnect(getContentPositionMs());
|
||||||
|
bufferedPositionSupplier.disconnect(getBufferedPositionMs());
|
||||||
|
totalBufferedDurationSupplier.disconnect(getTotalBufferedDurationMs());
|
||||||
|
positionSupplier = new LivePositionSupplier(this::getContentPositionMs);
|
||||||
|
bufferedPositionSupplier = new LivePositionSupplier(this::getBufferedPositionMs);
|
||||||
|
totalBufferedDurationSupplier = new LivePositionSupplier(this::getTotalBufferedDurationMs);
|
||||||
|
}
|
||||||
|
|
||||||
private static long getCompositionDurationUs(Composition composition) {
|
private static long getCompositionDurationUs(Composition composition) {
|
||||||
checkState(!composition.sequences.isEmpty());
|
checkState(!composition.sequences.isEmpty());
|
||||||
return getSequenceDurationUs(composition.sequences.get(0));
|
return getSequenceDurationUs(composition.sequences.get(0));
|
||||||
|
@ -237,6 +237,7 @@ public class CompositionPlayerTest {
|
|||||||
Player.COMMAND_STOP,
|
Player.COMMAND_STOP,
|
||||||
Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM,
|
Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM,
|
||||||
Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM,
|
Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM,
|
||||||
|
Player.COMMAND_SEEK_TO_DEFAULT_POSITION,
|
||||||
Player.COMMAND_SEEK_BACK,
|
Player.COMMAND_SEEK_BACK,
|
||||||
Player.COMMAND_SEEK_FORWARD,
|
Player.COMMAND_SEEK_FORWARD,
|
||||||
Player.COMMAND_GET_CURRENT_MEDIA_ITEM,
|
Player.COMMAND_GET_CURRENT_MEDIA_ITEM,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user