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 @Override
public void setRepeatMode(@RepeatMode int repeatMode) { public void setRepeatMode(@RepeatMode int repeatMode) {
if (remoteMediaClient != null) { if (remoteMediaClient == null) {
remoteMediaClient.queueSetRepeatMode(getCastRepeatMode(repeatMode), 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 @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. // There is no session. We leave the state of the player as it is now.
return; return;
} }
boolean wasPlaying = playbackState == Player.STATE_READY && playWhenReady; boolean wasPlaying = playbackState == Player.STATE_READY && playWhenReady;
updatePlayerStateAndNotifyIfChanged(); updatePlayerStateAndNotifyIfChanged();
boolean isPlaying = playbackState == Player.STATE_READY && playWhenReady; boolean isPlaying = playbackState == Player.STATE_READY && playWhenReady;
@ -561,12 +574,7 @@ public final class CastPlayer extends BasePlayer {
notificationsBatch.add( notificationsBatch.add(
new ListenerNotificationTask(listener -> listener.onIsPlayingChanged(isPlaying))); new ListenerNotificationTask(listener -> listener.onIsPlayingChanged(isPlaying)));
} }
@RepeatMode int repeatMode = fetchRepeatMode(remoteMediaClient); updateRepeatModeAndNotifyIfChanged();
if (this.repeatMode != repeatMode) {
this.repeatMode = repeatMode;
notificationsBatch.add(
new ListenerNotificationTask(listener -> listener.onRepeatModeChanged(this.repeatMode)));
}
updateTimelineAndNotifyIfChanged(); updateTimelineAndNotifyIfChanged();
int currentWindowIndex = C.INDEX_UNSET; int currentWindowIndex = C.INDEX_UNSET;
@ -599,6 +607,11 @@ public final class CastPlayer extends BasePlayer {
!remoteMediaClient.isPaused(), fetchPlaybackState(remoteMediaClient)); !remoteMediaClient.isPaused(), fetchPlaybackState(remoteMediaClient));
} }
@RequiresNonNull("remoteMediaClient")
private void updateRepeatModeAndNotifyIfChanged() {
setRepeatModeAndNotifyIfChanged(fetchRepeatMode(remoteMediaClient));
}
private void updateTimelineAndNotifyIfChanged() { private void updateTimelineAndNotifyIfChanged() {
if (updateTimeline()) { if (updateTimeline()) {
@Player.TimelineChangeReason @Player.TimelineChangeReason
@ -673,6 +686,14 @@ public final class CastPlayer extends BasePlayer {
return false; 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( private void setPlayerStateAndNotifyIfChanged(
boolean playWhenReady, @Player.State int playbackState) { boolean playWhenReady, @Player.State int playbackState) {
if (this.playWhenReady != playWhenReady || this.playbackState != playbackState) { if (this.playWhenReady != playWhenReady || this.playbackState != playbackState) {