Implement addMediaItems(List) and clearMediaItems() in BasePlayer

PiperOrigin-RevId: 361487730
This commit is contained in:
gyumin 2021-03-08 06:01:26 +00:00 committed by Ian Baker
parent c1dba81ed2
commit ffb7c466ed
6 changed files with 20 additions and 47 deletions

View file

@ -316,11 +316,6 @@ public final class CastPlayer extends BasePlayer {
toMediaQueueItems(mediaItems), startWindowIndex, startPositionMs, repeatMode.value);
}
@Override
public void addMediaItems(List<MediaItem> mediaItems) {
addMediaItemsInternal(toMediaQueueItems(mediaItems), MediaQueueItem.INVALID_ITEM_ID);
}
@Override
public void addMediaItems(int index, List<MediaItem> mediaItems) {
Assertions.checkArgument(index >= 0);
@ -353,8 +348,8 @@ public final class CastPlayer extends BasePlayer {
@Override
public void removeMediaItems(int fromIndex, int toIndex) {
Assertions.checkArgument(
fromIndex >= 0 && toIndex >= fromIndex && toIndex <= currentTimeline.getWindowCount());
Assertions.checkArgument(fromIndex >= 0 && toIndex >= fromIndex);
toIndex = min(toIndex, currentTimeline.getWindowCount());
if (fromIndex == toIndex) {
// Do nothing.
return;
@ -366,11 +361,6 @@ public final class CastPlayer extends BasePlayer {
removeMediaItemsInternal(uids);
}
@Override
public void clearMediaItems() {
removeMediaItems(/* fromIndex= */ 0, /* toIndex= */ currentTimeline.getWindowCount());
}
@Override
public boolean isCommandAvailable(@Command int command) {
return availableCommands.contains(command);

View file

@ -59,6 +59,11 @@ public abstract class BasePlayer implements Player {
addMediaItems(Collections.singletonList(mediaItem));
}
@Override
public final void addMediaItems(List<MediaItem> mediaItems) {
addMediaItems(/* index= */ Integer.MAX_VALUE, mediaItems);
}
@Override
public final void moveMediaItem(int currentIndex, int newIndex) {
if (currentIndex != newIndex) {
@ -71,6 +76,11 @@ public abstract class BasePlayer implements Player {
removeMediaItems(/* fromIndex= */ index, /* toIndex= */ index + 1);
}
@Override
public final void clearMediaItems() {
removeMediaItems(/* fromIndex= */ 0, /* toIndex= */ Integer.MAX_VALUE);
}
@Override
public final void play() {
setPlayWhenReady(true);

View file

@ -1153,7 +1153,8 @@ public interface Player {
/**
* Adds a media item at the given index of the playlist.
*
* @param index The index at which to add the item.
* @param index The index at which to add the media item. If the index is larger than the size of
* the playlist, the media item is added to the end of the playlist.
* @param mediaItem The {@link MediaItem} to add.
*/
void addMediaItem(int index, MediaItem mediaItem);
@ -1168,7 +1169,8 @@ public interface Player {
/**
* Adds a list of media items at the given index of the playlist.
*
* @param index The index at which to add the media items.
* @param index The index at which to add the media items. If the index is larger than the size of
* the playlist, the media items are added to the end of the playlist.
* @param mediaItems The {@link MediaItem MediaItems} to add.
*/
void addMediaItems(int index, List<MediaItem> mediaItems);
@ -1204,7 +1206,8 @@ public interface Player {
* Removes a range of media items from the playlist.
*
* @param fromIndex The index at which to start removing media items.
* @param toIndex The index of the first item to be kept (exclusive).
* @param toIndex The index of the first item to be kept (exclusive). If the index is larger than
* the size of the playlist, media items to the end of the playlist are removed.
*/
void removeMediaItems(int fromIndex, int toIndex);

View file

@ -415,13 +415,9 @@ import java.util.List;
mediaSources, startWindowIndex, startPositionMs, /* resetToDefaultPosition= */ false);
}
@Override
public void addMediaItems(List<MediaItem> mediaItems) {
addMediaItems(/* index= */ mediaSourceHolderSnapshots.size(), mediaItems);
}
@Override
public void addMediaItems(int index, List<MediaItem> mediaItems) {
index = min(index, mediaSourceHolderSnapshots.size());
addMediaSources(index, createMediaSources(mediaItems));
}
@ -464,6 +460,7 @@ import java.util.List;
@Override
public void removeMediaItems(int fromIndex, int toIndex) {
toIndex = min(toIndex, mediaSourceHolderSnapshots.size());
PlaybackInfo playbackInfo = removeMediaItemsInternal(fromIndex, toIndex);
updatePlaybackInfo(
playbackInfo,
@ -501,11 +498,6 @@ import java.util.List;
/* seekProcessed= */ false);
}
@Override
public void clearMediaItems() {
removeMediaItems(/* fromIndex= */ 0, /* toIndex= */ mediaSourceHolderSnapshots.size());
}
@Override
public void setShuffleOrder(ShuffleOrder shuffleOrder) {
Timeline timeline = createMaskingTimeline();

View file

@ -1357,12 +1357,6 @@ public class SimpleExoPlayer extends BasePlayer
player.setMediaSource(mediaSource, startPositionMs);
}
@Override
public void addMediaItems(List<MediaItem> mediaItems) {
verifyApplicationThread();
player.addMediaItems(mediaItems);
}
@Override
public void addMediaItems(int index, List<MediaItem> mediaItems) {
verifyApplicationThread();
@ -1405,12 +1399,6 @@ public class SimpleExoPlayer extends BasePlayer
player.removeMediaItems(fromIndex, toIndex);
}
@Override
public void clearMediaItems() {
verifyApplicationThread();
player.clearMediaItems();
}
@Override
public void setShuffleOrder(ShuffleOrder shuffleOrder) {
verifyApplicationThread();

View file

@ -192,11 +192,6 @@ public abstract class StubExoPlayer extends BasePlayer implements ExoPlayer {
throw new UnsupportedOperationException();
}
@Override
public void addMediaItems(List<MediaItem> mediaItems) {
throw new UnsupportedOperationException();
}
@Override
public void addMediaItems(int index, List<MediaItem> mediaItems) {
throw new UnsupportedOperationException();
@ -232,11 +227,6 @@ public abstract class StubExoPlayer extends BasePlayer implements ExoPlayer {
throw new UnsupportedOperationException();
}
@Override
public void clearMediaItems() {
throw new UnsupportedOperationException();
}
@Override
public boolean isCommandAvailable(int command) {
throw new UnsupportedOperationException();