Use Player commands to enable UI buttons

PiperOrigin-RevId: 384910388
This commit is contained in:
kimvde 2021-07-15 14:08:19 +01:00 committed by Oliver Woodman
parent 08ac778ad6
commit 85f3af8864
4 changed files with 43 additions and 64 deletions

View File

@ -16,6 +16,9 @@
package com.google.android.exoplayer2.ext.mediasession; package com.google.android.exoplayer2.ext.mediasession;
import static androidx.media.utils.MediaConstants.PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID; import static androidx.media.utils.MediaConstants.PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_WINDOW;
import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED;
@ -912,16 +915,16 @@ public final class MediaSessionConnector {
} }
private long buildPlaybackActions(Player player) { private long buildPlaybackActions(Player player) {
boolean enableSeeking = false; boolean enableSeeking = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_WINDOW);
boolean enableRewind = false; boolean enableRewind =
boolean enableFastForward = false; player.isCommandAvailable(COMMAND_SEEK_BACK) && controlDispatcher.isRewindEnabled();
boolean enableFastForward =
player.isCommandAvailable(COMMAND_SEEK_FORWARD) && controlDispatcher.isFastForwardEnabled();
boolean enableSetRating = false; boolean enableSetRating = false;
boolean enableSetCaptioningEnabled = false; boolean enableSetCaptioningEnabled = false;
Timeline timeline = player.getCurrentTimeline(); Timeline timeline = player.getCurrentTimeline();
if (!timeline.isEmpty() && !player.isPlayingAd()) { if (!timeline.isEmpty() && !player.isPlayingAd()) {
enableSeeking = player.isCurrentWindowSeekable();
enableRewind = enableSeeking && controlDispatcher.isRewindEnabled();
enableFastForward = enableSeeking && controlDispatcher.isFastForwardEnabled();
enableSetRating = ratingCallback != null; enableSetRating = ratingCallback != null;
enableSetCaptioningEnabled = captionCallback != null && captionCallback.hasCaptions(player); enableSetCaptioningEnabled = captionCallback != null && captionCallback.hasCaptions(player);
} }

View File

@ -15,9 +15,11 @@
*/ */
package com.google.android.exoplayer2.ui; package com.google.android.exoplayer2.ui;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_WINDOW;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS;
import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED;
@ -869,21 +871,14 @@ public class PlayerControlView extends FrameLayout {
boolean enableFastForward = false; boolean enableFastForward = false;
boolean enableNext = false; boolean enableNext = false;
if (player != null) { if (player != null) {
Timeline timeline = player.getCurrentTimeline(); enableSeeking = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_WINDOW);
if (!timeline.isEmpty() && !player.isPlayingAd()) { enablePrevious = player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS);
boolean isSeekable = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_WINDOW); enableRewind =
timeline.getWindow(player.getCurrentWindowIndex(), window); player.isCommandAvailable(COMMAND_SEEK_BACK) && controlDispatcher.isRewindEnabled();
enableSeeking = isSeekable; enableFastForward =
enablePrevious = player.isCommandAvailable(COMMAND_SEEK_FORWARD)
isSeekable && controlDispatcher.isFastForwardEnabled();
|| !window.isLive() enableNext = player.isCommandAvailable(COMMAND_SEEK_TO_NEXT);
|| player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS_WINDOW);
enableRewind = isSeekable && controlDispatcher.isRewindEnabled();
enableFastForward = isSeekable && controlDispatcher.isFastForwardEnabled();
enableNext =
(window.isLive() && window.isDynamic)
|| player.isCommandAvailable(COMMAND_SEEK_TO_NEXT_WINDOW);
}
} }
updateButton(showPreviousButton, enablePrevious, previousButton); updateButton(showPreviousButton, enablePrevious, previousButton);

View File

@ -15,9 +15,10 @@
*/ */
package com.google.android.exoplayer2.ui; package com.google.android.exoplayer2.ui;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS;
import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_MEDIA_METADATA_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_MEDIA_METADATA_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED;
@ -53,7 +54,6 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ControlDispatcher; import com.google.android.exoplayer2.ControlDispatcher;
import com.google.android.exoplayer2.DefaultControlDispatcher; import com.google.android.exoplayer2.DefaultControlDispatcher;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.NotificationUtil; import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
@ -677,7 +677,6 @@ public class PlayerNotificationManager {
private final Map<String, NotificationCompat.Action> customActions; private final Map<String, NotificationCompat.Action> customActions;
private final PendingIntent dismissPendingIntent; private final PendingIntent dismissPendingIntent;
private final int instanceId; private final int instanceId;
private final Timeline.Window window;
@Nullable private NotificationCompat.Builder builder; @Nullable private NotificationCompat.Builder builder;
@Nullable private List<NotificationCompat.Action> builderActions; @Nullable private List<NotificationCompat.Action> builderActions;
@ -730,7 +729,6 @@ public class PlayerNotificationManager {
this.smallIconResourceId = smallIconResourceId; this.smallIconResourceId = smallIconResourceId;
this.groupKey = groupKey; this.groupKey = groupKey;
controlDispatcher = new DefaultControlDispatcher(); controlDispatcher = new DefaultControlDispatcher();
window = new Timeline.Window();
instanceId = instanceIdCounter++; instanceId = instanceIdCounter++;
// This fails the nullness checker because handleMessage() is 'called' while `this` is still // This fails the nullness checker because handleMessage() is 'called' while `this` is still
// @UnderInitialization. No tasks are scheduled on mainHandler before the constructor completes, // @UnderInitialization. No tasks are scheduled on mainHandler before the constructor completes,
@ -1291,24 +1289,12 @@ public class PlayerNotificationManager {
* action name is ignored. * action name is ignored.
*/ */
protected List<String> getActions(Player player) { protected List<String> getActions(Player player) {
boolean enablePrevious = false; boolean enablePrevious = player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS);
boolean enableRewind = false; boolean enableRewind =
boolean enableFastForward = false; player.isCommandAvailable(COMMAND_SEEK_BACK) && controlDispatcher.isRewindEnabled();
boolean enableNext = false; boolean enableFastForward =
Timeline timeline = player.getCurrentTimeline(); player.isCommandAvailable(COMMAND_SEEK_FORWARD) && controlDispatcher.isFastForwardEnabled();
if (!timeline.isEmpty() && !player.isPlayingAd()) { boolean enableNext = player.isCommandAvailable(COMMAND_SEEK_TO_NEXT);
boolean isSeekable = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_WINDOW);
timeline.getWindow(player.getCurrentWindowIndex(), window);
enablePrevious =
isSeekable
|| !window.isLive()
|| player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS_WINDOW);
enableRewind = isSeekable && controlDispatcher.isRewindEnabled();
enableFastForward = isSeekable && controlDispatcher.isFastForwardEnabled();
enableNext =
(window.isLive() && window.isDynamic)
|| player.isCommandAvailable(COMMAND_SEEK_TO_NEXT_WINDOW);
}
List<String> stringActions = new ArrayList<>(); List<String> stringActions = new ArrayList<>();
if (usePreviousAction && enablePrevious) { if (usePreviousAction && enablePrevious) {

View File

@ -15,9 +15,11 @@
*/ */
package com.google.android.exoplayer2.ui; package com.google.android.exoplayer2.ui;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_BACK;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_FORWARD;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_WINDOW;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT;
import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_WINDOW; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS;
import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED;
@ -1131,21 +1133,14 @@ public class StyledPlayerControlView extends FrameLayout {
boolean enableFastForward = false; boolean enableFastForward = false;
boolean enableNext = false; boolean enableNext = false;
if (player != null) { if (player != null) {
Timeline timeline = player.getCurrentTimeline(); enableSeeking = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_WINDOW);
if (!timeline.isEmpty() && !player.isPlayingAd()) { enablePrevious = player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS);
boolean isSeekable = player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_WINDOW); enableRewind =
timeline.getWindow(player.getCurrentWindowIndex(), window); player.isCommandAvailable(COMMAND_SEEK_BACK) && controlDispatcher.isRewindEnabled();
enableSeeking = isSeekable; enableFastForward =
enablePrevious = player.isCommandAvailable(COMMAND_SEEK_FORWARD)
isSeekable && controlDispatcher.isFastForwardEnabled();
|| !window.isLive() enableNext = player.isCommandAvailable(COMMAND_SEEK_TO_NEXT);
|| player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS_WINDOW);
enableRewind = isSeekable && controlDispatcher.isRewindEnabled();
enableFastForward = isSeekable && controlDispatcher.isFastForwardEnabled();
enableNext =
(window.isLive() && window.isDynamic)
|| player.isCommandAvailable(COMMAND_SEEK_TO_NEXT_WINDOW);
}
} }
if (enableRewind) { if (enableRewind) {