Skip to content

Commit

Permalink
PlaybackNotificationManager should show play button in ENDED state
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ojw28 committed Nov 14, 2018
1 parent 51461d7 commit 9ca019b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ private int mapPlaybackState(int exoPlayerPlaybackState, boolean playWhenReady)
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;
}
Expand Down Expand Up @@ -934,6 +934,13 @@ private class MediaSessionCallback extends MediaSessionCompat.Callback {
@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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand Down Expand Up @@ -302,6 +303,7 @@ public void onBitmap(final Bitmap bitmap) {
private final Timeline.Window window;

@Nullable private Player player;
@Nullable private PlaybackPreparer playbackPreparer;
private ControlDispatcher controlDispatcher;
private boolean isNotificationStarted;
private int currentNotificationTag;
Expand Down Expand Up @@ -562,6 +564,15 @@ public final void setPlayer(@Nullable Player player) {
}
}

/**
* Sets the {@link PlaybackPreparer}.
*
* @param playbackPreparer The {@link PlaybackPreparer}.
*/
public void setPlaybackPreparer(@Nullable PlaybackPreparer playbackPreparer) {
this.playbackPreparer = playbackPreparer;
}

/**
* Sets the {@link ControlDispatcher}.
*
Expand Down Expand Up @@ -991,7 +1002,7 @@ protected List<String> getActions(Player player) {
stringActions.add(ACTION_REWIND);
}
if (usePlayPauseActions) {
if (player.getPlayWhenReady()) {
if (isPlaying(player)) {
stringActions.add(ACTION_PAUSE);
} else {
stringActions.add(ACTION_PLAY);
Expand Down Expand Up @@ -1134,6 +1145,12 @@ private void seekTo(Player player, int windowIndex, long positionMs) {
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());
Expand Down Expand Up @@ -1195,8 +1212,17 @@ public void onReceive(Context context, Intent intent) {
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)) {
Expand Down

0 comments on commit 9ca019b

Please sign in to comment.