From 9ca019b8f808a38fea3a467cff200ff7ebf876ce Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 12 Nov 2018 05:18:03 -0800 Subject: [PATCH] PlaybackNotificationManager should show play button in ENDED state - This brings it in line with PlayerControlView. The play action is displayed instead, and pressing it seeks to the default position of the current window. - Do the same for MediaSessionConnector - Add support for PlaybackPreparer consistent with PlayerControlView Issue: #5072 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=221076008 --- .../mediasession/MediaSessionConnector.java | 9 +++++- .../ui/PlayerNotificationManager.java | 32 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java index 0cb590b65e..27ce380a6d 100644 --- a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java +++ b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java @@ -703,7 +703,7 @@ public final class MediaSessionConnector { case Player.STATE_READY: return playWhenReady ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; case Player.STATE_ENDED: - return PlaybackStateCompat.STATE_PAUSED; + return PlaybackStateCompat.STATE_STOPPED; default: return PlaybackStateCompat.STATE_NONE; } @@ -934,6 +934,13 @@ public final class MediaSessionConnector { @Override public void onPlay() { if (canDispatchPlaybackAction(PlaybackStateCompat.ACTION_PLAY)) { + if (player.getPlaybackState() == Player.STATE_IDLE) { + if (playbackPreparer != null) { + playbackPreparer.onPrepare(); + } + } else if (player.getPlaybackState() == Player.STATE_ENDED) { + controlDispatcher.dispatchSeekTo(player, player.getCurrentWindowIndex(), C.TIME_UNSET); + } controlDispatcher.dispatchSetPlayWhenReady(player, /* playWhenReady= */ true); } } diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java index fff59b09cd..30c19d5f82 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerNotificationManager.java @@ -37,6 +37,7 @@ import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.DefaultControlDispatcher; import com.google.android.exoplayer2.PlaybackParameters; +import com.google.android.exoplayer2.PlaybackPreparer; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.util.Assertions; @@ -302,6 +303,7 @@ public class PlayerNotificationManager { private final Timeline.Window window; @Nullable private Player player; + @Nullable private PlaybackPreparer playbackPreparer; private ControlDispatcher controlDispatcher; private boolean isNotificationStarted; private int currentNotificationTag; @@ -562,6 +564,15 @@ public class PlayerNotificationManager { } } + /** + * Sets the {@link PlaybackPreparer}. + * + * @param playbackPreparer The {@link PlaybackPreparer}. + */ + public void setPlaybackPreparer(@Nullable PlaybackPreparer playbackPreparer) { + this.playbackPreparer = playbackPreparer; + } + /** * Sets the {@link ControlDispatcher}. * @@ -991,7 +1002,7 @@ public class PlayerNotificationManager { stringActions.add(ACTION_REWIND); } if (usePlayPauseActions) { - if (player.getPlayWhenReady()) { + if (isPlaying(player)) { stringActions.add(ACTION_PAUSE); } else { stringActions.add(ACTION_PLAY); @@ -1134,6 +1145,12 @@ public class PlayerNotificationManager { controlDispatcher.dispatchSeekTo(player, windowIndex, positionMs); } + private boolean isPlaying(Player player) { + return player.getPlaybackState() != Player.STATE_ENDED + && player.getPlaybackState() != Player.STATE_IDLE + && player.getPlayWhenReady(); + } + private static PendingIntent createBroadcastIntent( String action, Context context, int instanceId) { Intent intent = new Intent(action).setPackage(context.getPackageName()); @@ -1195,8 +1212,17 @@ public class PlayerNotificationManager { return; } String action = intent.getAction(); - if (ACTION_PLAY.equals(action) || ACTION_PAUSE.equals(action)) { - controlDispatcher.dispatchSetPlayWhenReady(player, ACTION_PLAY.equals(action)); + if (ACTION_PLAY.equals(action)) { + if (player.getPlaybackState() == Player.STATE_IDLE) { + if (playbackPreparer != null) { + playbackPreparer.preparePlayback(); + } + } else if (player.getPlaybackState() == Player.STATE_ENDED) { + controlDispatcher.dispatchSeekTo(player, player.getCurrentWindowIndex(), C.TIME_UNSET); + } + controlDispatcher.dispatchSetPlayWhenReady(player, /* playWhenReady= */ true); + } else if (ACTION_PAUSE.equals(action)) { + controlDispatcher.dispatchSetPlayWhenReady(player, /* playWhenReady= */ false); } else if (ACTION_PREVIOUS.equals(action)) { previous(player); } else if (ACTION_REWIND.equals(action)) {