From adcaf4b027aa8736be9a74a2b2b1a7e6b8539eba Mon Sep 17 00:00:00 2001 From: kimvde Date: Tue, 16 Mar 2021 10:15:24 +0000 Subject: [PATCH] Add available command to seek to any media item PiperOrigin-RevId: 363144142 --- .../exoplayer2/ext/cast/CastPlayer.java | 1 + .../exoplayer2/ext/cast/CastPlayerTest.java | 2 ++ .../google/android/exoplayer2/BasePlayer.java | 1 + .../com/google/android/exoplayer2/Player.java | 25 +++++++++++-------- .../android/exoplayer2/ExoPlayerImpl.java | 6 ++++- .../android/exoplayer2/ExoPlayerTest.java | 4 +++ 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java index 74999635e9..09aef31b71 100644 --- a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java +++ b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java @@ -79,6 +79,7 @@ public final class CastPlayer extends BasePlayer { new int[] { COMMAND_PLAY_PAUSE, COMMAND_PREPARE_STOP_RELEASE, + COMMAND_SEEK_TO_MEDIA_ITEM, COMMAND_SET_REPEAT_MODE, COMMAND_GET_CURRENT_MEDIA_ITEM, COMMAND_GET_MEDIA_ITEMS, diff --git a/extensions/cast/src/test/java/com/google/android/exoplayer2/ext/cast/CastPlayerTest.java b/extensions/cast/src/test/java/com/google/android/exoplayer2/ext/cast/CastPlayerTest.java index 065e72a1cd..11b217d4cf 100644 --- a/extensions/cast/src/test/java/com/google/android/exoplayer2/ext/cast/CastPlayerTest.java +++ b/extensions/cast/src/test/java/com/google/android/exoplayer2/ext/cast/CastPlayerTest.java @@ -22,6 +22,7 @@ import static com.google.android.exoplayer2.Player.COMMAND_GET_MEDIA_ITEMS_METAD import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE_STOP_RELEASE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; +import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SET_REPEAT_MODE; @@ -596,6 +597,7 @@ public class CastPlayerTest { assertThat(castPlayer.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)).isTrue(); assertThat(castPlayer.isCommandAvailable(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM)).isTrue(); assertThat(castPlayer.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM)).isFalse(); + assertThat(castPlayer.isCommandAvailable(COMMAND_SEEK_TO_MEDIA_ITEM)).isTrue(); assertThat(castPlayer.isCommandAvailable(COMMAND_SET_SPEED_AND_PITCH)).isFalse(); assertThat(castPlayer.isCommandAvailable(COMMAND_SET_SHUFFLE_MODE)).isFalse(); assertThat(castPlayer.isCommandAvailable(COMMAND_SET_REPEAT_MODE)).isTrue(); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/BasePlayer.java b/library/common/src/main/java/com/google/android/exoplayer2/BasePlayer.java index e43cebfe34..a47b89ca25 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/BasePlayer.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/BasePlayer.java @@ -326,6 +326,7 @@ public abstract class BasePlayer implements Player { .addIf(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, isCurrentWindowSeekable() && !isPlayingAd()) .addIf(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, hasNext() && !isPlayingAd()) .addIf(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, hasPrevious() && !isPlayingAd()) + .addIf(COMMAND_SEEK_TO_MEDIA_ITEM, !isPlayingAd()) .build(); } } diff --git a/library/common/src/main/java/com/google/android/exoplayer2/Player.java b/library/common/src/main/java/com/google/android/exoplayer2/Player.java index 7d420e3305..7f5b6e8385 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/Player.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/Player.java @@ -1031,10 +1031,10 @@ public interface Player { * Commands that can be executed on a {@code Player}. One of {@link #COMMAND_PLAY_PAUSE}, {@link * #COMMAND_PREPARE_STOP_RELEASE}, {@link #COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM}, {@link * #COMMAND_SEEK_TO_NEXT_MEDIA_ITEM}, {@link #COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM}, {@link - * #COMMAND_SET_SPEED_AND_PITCH}, {@link #COMMAND_SET_SHUFFLE_MODE}, {@link - * #COMMAND_SET_REPEAT_MODE}, {@link #COMMAND_GET_CURRENT_MEDIA_ITEM}, {@link - * #COMMAND_GET_MEDIA_ITEMS}, {@link #COMMAND_GET_MEDIA_ITEMS_METADATA} or {@link - * #COMMAND_CHANGE_MEDIA_ITEMS}. + * #COMMAND_SEEK_TO_MEDIA_ITEM}, {@link #COMMAND_SET_SPEED_AND_PITCH}, {@link + * #COMMAND_SET_SHUFFLE_MODE}, {@link #COMMAND_SET_REPEAT_MODE}, {@link + * #COMMAND_GET_CURRENT_MEDIA_ITEM}, {@link #COMMAND_GET_MEDIA_ITEMS}, {@link + * #COMMAND_GET_MEDIA_ITEMS_METADATA} or {@link #COMMAND_CHANGE_MEDIA_ITEMS}. */ @Documented @Retention(RetentionPolicy.SOURCE) @@ -1044,6 +1044,7 @@ public interface Player { COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM, COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM, + COMMAND_SEEK_TO_MEDIA_ITEM, COMMAND_SET_SPEED_AND_PITCH, COMMAND_SET_SHUFFLE_MODE, COMMAND_SET_REPEAT_MODE, @@ -1063,20 +1064,22 @@ public interface Player { int COMMAND_SEEK_TO_NEXT_MEDIA_ITEM = 4; /** Command to seek to the previous {@link MediaItem} in the playlist. */ int COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM = 5; + /** Command to seek to a {@link MediaItem} in the playlist. */ + int COMMAND_SEEK_TO_MEDIA_ITEM = 6; /** Command to set the playback speed and pitch. */ - int COMMAND_SET_SPEED_AND_PITCH = 6; + int COMMAND_SET_SPEED_AND_PITCH = 7; /** Command to enable shuffling. */ - int COMMAND_SET_SHUFFLE_MODE = 7; + int COMMAND_SET_SHUFFLE_MODE = 8; /** Command to set the repeat mode. */ - int COMMAND_SET_REPEAT_MODE = 8; + int COMMAND_SET_REPEAT_MODE = 9; /** Command to get the current {@link MediaItem}. */ - int COMMAND_GET_CURRENT_MEDIA_ITEM = 9; + int COMMAND_GET_CURRENT_MEDIA_ITEM = 10; /** Command to get the {@link MediaItem MediaItems} in the playlist. */ - int COMMAND_GET_MEDIA_ITEMS = 10; + int COMMAND_GET_MEDIA_ITEMS = 11; /** Command to get the {@link MediaItem MediaItems} metadata. */ - int COMMAND_GET_MEDIA_ITEMS_METADATA = 11; + int COMMAND_GET_MEDIA_ITEMS_METADATA = 12; /** Command to change the {@link MediaItem MediaItems} in the playlist. */ - int COMMAND_CHANGE_MEDIA_ITEMS = 12; + int COMMAND_CHANGE_MEDIA_ITEMS = 13; /** Returns the component of this player for audio output, or null if audio is not supported. */ @Nullable diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java index eefcd11d68..d70821296a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java @@ -192,7 +192,11 @@ import java.util.concurrent.CopyOnWriteArraySet; new ExoTrackSelection[renderers.length], /* info= */ null); period = new Timeline.Period(); - availableCommands = new Commands.Builder().addAll(PERMANENT_AVAILABLE_COMMANDS).build(); + availableCommands = + new Commands.Builder() + .addAll(PERMANENT_AVAILABLE_COMMANDS) + .add(COMMAND_SEEK_TO_MEDIA_ITEM) + .build(); maskingWindowIndex = C.INDEX_UNSET; playbackInfoUpdateHandler = clock.createHandler(applicationLooper, /* callback= */ null); playbackInfoUpdateListener = diff --git a/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java index 962e17b45d..8c02bb507e 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/ExoPlayerTest.java @@ -22,6 +22,7 @@ import static com.google.android.exoplayer2.Player.COMMAND_GET_MEDIA_ITEMS_METAD import static com.google.android.exoplayer2.Player.COMMAND_PLAY_PAUSE; import static com.google.android.exoplayer2.Player.COMMAND_PREPARE_STOP_RELEASE; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM; +import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM; import static com.google.android.exoplayer2.Player.COMMAND_SET_REPEAT_MODE; @@ -8091,6 +8092,7 @@ public final class ExoPlayerTest { assertThat(player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)).isFalse(); assertThat(player.isCommandAvailable(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM)).isTrue(); assertThat(player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM)).isFalse(); + assertThat(player.isCommandAvailable(COMMAND_SEEK_TO_MEDIA_ITEM)).isTrue(); assertThat(player.isCommandAvailable(COMMAND_SET_SPEED_AND_PITCH)).isTrue(); assertThat(player.isCommandAvailable(COMMAND_SET_SHUFFLE_MODE)).isTrue(); assertThat(player.isCommandAvailable(COMMAND_SET_REPEAT_MODE)).isTrue(); @@ -8128,6 +8130,7 @@ public final class ExoPlayerTest { assertThat(player.isCommandAvailable(COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM)).isFalse(); assertThat(player.isCommandAvailable(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM)).isFalse(); assertThat(player.isCommandAvailable(COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM)).isFalse(); + assertThat(player.isCommandAvailable(COMMAND_SEEK_TO_MEDIA_ITEM)).isFalse(); } @Test @@ -9376,6 +9379,7 @@ public final class ExoPlayerTest { private static Player.Commands createCommands(@Player.Command int... commands) { Player.Commands.Builder builder = new Player.Commands.Builder(); builder.addAll(ExoPlayerImpl.PERMANENT_AVAILABLE_COMMANDS); + builder.add(COMMAND_SEEK_TO_MEDIA_ITEM); for (int command : commands) { builder.add(command); }