Make MediaSessionConnector use getPlaybackError

It's no longer necessary to stash a reference to the
error yourself. This also correctly handles the case
where setPlayer is called with a player that's already
in an error state.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=194828387
This commit is contained in:
olly 2018-04-30 12:39:17 -07:00 committed by Oliver Woodman
parent fedf8dd5c1
commit c8ec77ef96
3 changed files with 34 additions and 31 deletions

View File

@ -19,6 +19,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.Log; import android.util.Log;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
@ -308,7 +309,7 @@ public final class CastPlayer implements Player {
} }
@Override @Override
public Exception getPlaybackError() { public ExoPlaybackException getPlaybackError() {
return null; return null;
} }

View File

@ -339,7 +339,6 @@ public final class MediaSessionConnector {
private QueueNavigator queueNavigator; private QueueNavigator queueNavigator;
private QueueEditor queueEditor; private QueueEditor queueEditor;
private RatingCallback ratingCallback; private RatingCallback ratingCallback;
private ExoPlaybackException playbackException;
/** /**
* Creates an instance. Must be called on the same thread that is used to construct the player * Creates an instance. Must be called on the same thread that is used to construct the player
@ -443,7 +442,10 @@ public final class MediaSessionConnector {
*/ */
public void setErrorMessageProvider( public void setErrorMessageProvider(
ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider) { ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider) {
this.errorMessageProvider = errorMessageProvider; if (this.errorMessageProvider != errorMessageProvider) {
this.errorMessageProvider = errorMessageProvider;
updateMediaSessionPlaybackState();
}
} }
/** /**
@ -453,9 +455,11 @@ public final class MediaSessionConnector {
* @param queueNavigator The queue navigator. * @param queueNavigator The queue navigator.
*/ */
public void setQueueNavigator(QueueNavigator queueNavigator) { public void setQueueNavigator(QueueNavigator queueNavigator) {
unregisterCommandReceiver(this.queueNavigator); if (this.queueNavigator != queueNavigator) {
this.queueNavigator = queueNavigator; unregisterCommandReceiver(this.queueNavigator);
registerCommandReceiver(queueNavigator); this.queueNavigator = queueNavigator;
registerCommandReceiver(queueNavigator);
}
} }
/** /**
@ -464,11 +468,13 @@ public final class MediaSessionConnector {
* @param queueEditor The queue editor. * @param queueEditor The queue editor.
*/ */
public void setQueueEditor(QueueEditor queueEditor) { public void setQueueEditor(QueueEditor queueEditor) {
unregisterCommandReceiver(this.queueEditor); if (this.queueEditor != queueEditor) {
this.queueEditor = queueEditor; unregisterCommandReceiver(this.queueEditor);
registerCommandReceiver(queueEditor); this.queueEditor = queueEditor;
mediaSession.setFlags(queueEditor == null ? BASE_MEDIA_SESSION_FLAGS registerCommandReceiver(queueEditor);
: EDITOR_MEDIA_SESSION_FLAGS); mediaSession.setFlags(
queueEditor == null ? BASE_MEDIA_SESSION_FLAGS : EDITOR_MEDIA_SESSION_FLAGS);
}
} }
/** /**
@ -477,9 +483,11 @@ public final class MediaSessionConnector {
* @param ratingCallback The rating callback. * @param ratingCallback The rating callback.
*/ */
public void setRatingCallback(RatingCallback ratingCallback) { public void setRatingCallback(RatingCallback ratingCallback) {
unregisterCommandReceiver(this.ratingCallback); if (this.ratingCallback != ratingCallback) {
this.ratingCallback = ratingCallback; unregisterCommandReceiver(this.ratingCallback);
registerCommandReceiver(this.ratingCallback); this.ratingCallback = ratingCallback;
registerCommandReceiver(this.ratingCallback);
}
} }
private void registerCommandReceiver(CommandReceiver commandReceiver) { private void registerCommandReceiver(CommandReceiver commandReceiver) {
@ -516,16 +524,16 @@ public final class MediaSessionConnector {
} }
customActionMap = Collections.unmodifiableMap(currentActions); customActionMap = Collections.unmodifiableMap(currentActions);
int sessionPlaybackState = playbackException != null ? PlaybackStateCompat.STATE_ERROR int playbackState = player.getPlaybackState();
: mapPlaybackState(player.getPlaybackState(), player.getPlayWhenReady()); ExoPlaybackException playbackError =
if (playbackException != null) { playbackState == Player.STATE_IDLE ? player.getPlaybackError() : null;
if (errorMessageProvider != null) { int sessionPlaybackState =
Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackException); playbackError != null
builder.setErrorMessage(message.first, message.second); ? PlaybackStateCompat.STATE_ERROR
} : mapPlaybackState(player.getPlaybackState(), player.getPlayWhenReady());
if (player.getPlaybackState() != Player.STATE_IDLE) { if (playbackError != null && errorMessageProvider != null) {
playbackException = null; Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackError);
} builder.setErrorMessage(message.first, message.second);
} }
long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player) long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player)
: MediaSessionCompat.QueueItem.UNKNOWN_ID; : MediaSessionCompat.QueueItem.UNKNOWN_ID;
@ -701,12 +709,6 @@ public final class MediaSessionConnector {
updateMediaSessionPlaybackState(); updateMediaSessionPlaybackState();
} }
@Override
public void onPlayerError(ExoPlaybackException error) {
playbackException = error;
updateMediaSessionPlaybackState();
}
@Override @Override
public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
if (currentWindowIndex != player.getCurrentWindowIndex()) { if (currentWindowIndex != player.getCurrentWindowIndex()) {

View File

@ -468,7 +468,7 @@ public interface Player {
* @return The error, or {@code null}. * @return The error, or {@code null}.
*/ */
@Nullable @Nullable
Exception getPlaybackError(); ExoPlaybackException getPlaybackError();
/** /**
* Sets whether playback should proceed when {@link #getPlaybackState()} == {@link #STATE_READY}. * Sets whether playback should proceed when {@link #getPlaybackState()} == {@link #STATE_READY}.