Mask repeatMode in CastPlayer

When the user calls setRepeatMode, the state modifications are
observed as immediate, in spite of being sent to the receiver app
as asynchronous operations.

PiperOrigin-RevId: 273754931
This commit is contained in:
aquilescanta 2019-10-09 16:53:44 +01:00 committed by Ian Baker
parent 5775a47010
commit a35a0925ed

View File

@ -440,9 +440,23 @@ public final class CastPlayer extends BasePlayer {
@Override
public void setRepeatMode(@RepeatMode int repeatMode) {
if (remoteMediaClient != null) {
remoteMediaClient.queueSetRepeatMode(getCastRepeatMode(repeatMode), null);
if (remoteMediaClient == null) {
return;
}
// We update the local state and send the message to the receiver app, which will cause the
// operation to be perceived as synchronous by the user. When the operation reports a result,
// the local state will be updated to reflect the state reported by the Cast SDK.
setRepeatModeAndNotifyIfChanged(repeatMode);
flushNotifications();
remoteMediaClient
.queueSetRepeatMode(getCastRepeatMode(repeatMode), /* jsonObject= */ null)
.setResultCallback(
mediaChannelResult -> {
if (remoteMediaClient != null) {
updateRepeatModeAndNotifyIfChanged();
flushNotifications();
}
});
}
@Override
@ -553,7 +567,6 @@ public final class CastPlayer extends BasePlayer {
// There is no session. We leave the state of the player as it is now.
return;
}
boolean wasPlaying = playbackState == Player.STATE_READY && playWhenReady;
updatePlayerStateAndNotifyIfChanged();
boolean isPlaying = playbackState == Player.STATE_READY && playWhenReady;
@ -561,12 +574,7 @@ public final class CastPlayer extends BasePlayer {
notificationsBatch.add(
new ListenerNotificationTask(listener -> listener.onIsPlayingChanged(isPlaying)));
}
@RepeatMode int repeatMode = fetchRepeatMode(remoteMediaClient);
if (this.repeatMode != repeatMode) {
this.repeatMode = repeatMode;
notificationsBatch.add(
new ListenerNotificationTask(listener -> listener.onRepeatModeChanged(this.repeatMode)));
}
updateRepeatModeAndNotifyIfChanged();
updateTimelineAndNotifyIfChanged();
int currentWindowIndex = C.INDEX_UNSET;
@ -599,6 +607,11 @@ public final class CastPlayer extends BasePlayer {
!remoteMediaClient.isPaused(), fetchPlaybackState(remoteMediaClient));
}
@RequiresNonNull("remoteMediaClient")
private void updateRepeatModeAndNotifyIfChanged() {
setRepeatModeAndNotifyIfChanged(fetchRepeatMode(remoteMediaClient));
}
private void updateTimelineAndNotifyIfChanged() {
if (updateTimeline()) {
@Player.TimelineChangeReason
@ -673,6 +686,14 @@ public final class CastPlayer extends BasePlayer {
return false;
}
private void setRepeatModeAndNotifyIfChanged(@Player.RepeatMode int repeatMode) {
if (this.repeatMode != repeatMode) {
this.repeatMode = repeatMode;
notificationsBatch.add(
new ListenerNotificationTask(listener -> listener.onRepeatModeChanged(repeatMode)));
}
}
private void setPlayerStateAndNotifyIfChanged(
boolean playWhenReady, @Player.State int playbackState) {
if (this.playWhenReady != playWhenReady || this.playbackState != playbackState) {