Commit c8ec77ef by olly Committed by Oliver Woodman

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
parent fedf8dd5
...@@ -19,6 +19,7 @@ import android.support.annotation.NonNull; ...@@ -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 { ...@@ -308,7 +309,7 @@ public final class CastPlayer implements Player {
} }
@Override @Override
public Exception getPlaybackError() { public ExoPlaybackException getPlaybackError() {
return null; return null;
} }
......
...@@ -339,7 +339,6 @@ public final class MediaSessionConnector { ...@@ -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 { ...@@ -443,7 +442,10 @@ public final class MediaSessionConnector {
*/ */
public void setErrorMessageProvider( public void setErrorMessageProvider(
ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider) { ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider) {
if (this.errorMessageProvider != errorMessageProvider) {
this.errorMessageProvider = errorMessageProvider; this.errorMessageProvider = errorMessageProvider;
updateMediaSessionPlaybackState();
}
} }
/** /**
...@@ -453,10 +455,12 @@ public final class MediaSessionConnector { ...@@ -453,10 +455,12 @@ public final class MediaSessionConnector {
* @param queueNavigator The queue navigator. * @param queueNavigator The queue navigator.
*/ */
public void setQueueNavigator(QueueNavigator queueNavigator) { public void setQueueNavigator(QueueNavigator queueNavigator) {
if (this.queueNavigator != queueNavigator) {
unregisterCommandReceiver(this.queueNavigator); unregisterCommandReceiver(this.queueNavigator);
this.queueNavigator = queueNavigator; this.queueNavigator = queueNavigator;
registerCommandReceiver(queueNavigator); registerCommandReceiver(queueNavigator);
} }
}
/** /**
* Sets the {@link QueueEditor} to handle queue edits sent by the media controller. * Sets the {@link QueueEditor} to handle queue edits sent by the media controller.
...@@ -464,11 +468,13 @@ public final class MediaSessionConnector { ...@@ -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) {
if (this.queueEditor != queueEditor) {
unregisterCommandReceiver(this.queueEditor); unregisterCommandReceiver(this.queueEditor);
this.queueEditor = queueEditor; this.queueEditor = queueEditor;
registerCommandReceiver(queueEditor); registerCommandReceiver(queueEditor);
mediaSession.setFlags(queueEditor == null ? BASE_MEDIA_SESSION_FLAGS mediaSession.setFlags(
: EDITOR_MEDIA_SESSION_FLAGS); queueEditor == null ? BASE_MEDIA_SESSION_FLAGS : EDITOR_MEDIA_SESSION_FLAGS);
}
} }
/** /**
...@@ -477,10 +483,12 @@ public final class MediaSessionConnector { ...@@ -477,10 +483,12 @@ public final class MediaSessionConnector {
* @param ratingCallback The rating callback. * @param ratingCallback The rating callback.
*/ */
public void setRatingCallback(RatingCallback ratingCallback) { public void setRatingCallback(RatingCallback ratingCallback) {
if (this.ratingCallback != ratingCallback) {
unregisterCommandReceiver(this.ratingCallback); unregisterCommandReceiver(this.ratingCallback);
this.ratingCallback = ratingCallback; this.ratingCallback = ratingCallback;
registerCommandReceiver(this.ratingCallback); registerCommandReceiver(this.ratingCallback);
} }
}
private void registerCommandReceiver(CommandReceiver commandReceiver) { private void registerCommandReceiver(CommandReceiver commandReceiver) {
if (commandReceiver != null && commandReceiver.getCommands() != null) { if (commandReceiver != null && commandReceiver.getCommands() != null) {
...@@ -516,17 +524,17 @@ public final class MediaSessionConnector { ...@@ -516,17 +524,17 @@ public final class MediaSessionConnector {
} }
customActionMap = Collections.unmodifiableMap(currentActions); customActionMap = Collections.unmodifiableMap(currentActions);
int sessionPlaybackState = playbackException != null ? PlaybackStateCompat.STATE_ERROR int playbackState = player.getPlaybackState();
ExoPlaybackException playbackError =
playbackState == Player.STATE_IDLE ? player.getPlaybackError() : null;
int sessionPlaybackState =
playbackError != null
? PlaybackStateCompat.STATE_ERROR
: mapPlaybackState(player.getPlaybackState(), player.getPlayWhenReady()); : mapPlaybackState(player.getPlaybackState(), player.getPlayWhenReady());
if (playbackException != null) { if (playbackError != null && errorMessageProvider != null) {
if (errorMessageProvider != null) { Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackError);
Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackException);
builder.setErrorMessage(message.first, message.second); builder.setErrorMessage(message.first, message.second);
} }
if (player.getPlaybackState() != Player.STATE_IDLE) {
playbackException = null;
}
}
long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player) long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player)
: MediaSessionCompat.QueueItem.UNKNOWN_ID; : MediaSessionCompat.QueueItem.UNKNOWN_ID;
Bundle extras = new Bundle(); Bundle extras = new Bundle();
...@@ -702,12 +710,6 @@ public final class MediaSessionConnector { ...@@ -702,12 +710,6 @@ public final class MediaSessionConnector {
} }
@Override @Override
public void onPlayerError(ExoPlaybackException error) {
playbackException = error;
updateMediaSessionPlaybackState();
}
@Override
public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) { public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
if (currentWindowIndex != player.getCurrentWindowIndex()) { if (currentWindowIndex != player.getCurrentWindowIndex()) {
if (queueNavigator != null) { if (queueNavigator != null) {
......
...@@ -468,7 +468,7 @@ public interface Player { ...@@ -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}.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment