use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class SessionPlayerConnectorTest method setRepeatMode_withRepeatAll_continuesToPlayPlaylistWithoutBeingCompleted.
@Test
@LargeTest
public void setRepeatMode_withRepeatAll_continuesToPlayPlaylistWithoutBeingCompleted() throws Exception {
List<MediaItem> playlist = new ArrayList<>();
playlist.add(TestUtils.createMediaItem(R.raw.video_1));
playlist.add(TestUtils.createMediaItem(R.raw.video_2));
playlist.add(TestUtils.createMediaItem(R.raw.video_3));
int listSize = playlist.size();
// Any value more than list size + 1, to see repeat mode with the recorded video.
CopyOnWriteArrayList<MediaItem> currentMediaItemChanges = new CopyOnWriteArrayList<>();
PlayerCallbackForPlaylist callback = new PlayerCallbackForPlaylist(playlist, listSize + 2) {
@Override
public void onCurrentMediaItemChanged(SessionPlayer player, MediaItem item) {
super.onCurrentMediaItemChanged(player, item);
currentMediaItemChanges.add(item);
countDown();
}
@Override
public void onPlaybackCompleted(SessionPlayer player) {
assertWithMessage("Playback shouldn't be completed, Actual changes were %s", currentMediaItemChanges).fail();
}
};
sessionPlayerConnector.registerPlayerCallback(executor, callback);
assertThat(sessionPlayerConnector.setPlaylist(playlist, null)).isNotNull();
assertThat(sessionPlayerConnector.prepare()).isNotNull();
assertThat(sessionPlayerConnector.setRepeatMode(SessionPlayer.REPEAT_MODE_ALL)).isNotNull();
assertThat(sessionPlayerConnector.play()).isNotNull();
assertWithMessage("Current media item didn't change as expected. Actual changes were %s", currentMediaItemChanges).that(callback.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
int expectedMediaItemIndex = 0;
for (MediaItem mediaItemInPlaybackOrder : currentMediaItemChanges) {
assertWithMessage("Unexpected media item for %sth playback. Actual changes were %s", expectedMediaItemIndex, currentMediaItemChanges).that(mediaItemInPlaybackOrder).isEqualTo(playlist.get(expectedMediaItemIndex));
expectedMediaItemIndex = (expectedMediaItemIndex + 1) % listSize;
}
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class SessionCallbackBuilderTest method allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable.
@Test
public void allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable() throws Exception {
List<MediaItem> testPlaylist = new ArrayList<>();
testPlaylist.add(TestUtils.createMediaItem(R.raw.video_desks));
UriMediaItem secondPlaylistItem = TestUtils.createMediaItem(R.raw.video_big_buck_bunny);
testPlaylist.add(secondPlaylistItem);
CountDownLatch readAllowedLatch = new CountDownLatch(1);
playerTestRule.setDataSourceInstrumentation(dataSpec -> {
if (dataSpec.uri.equals(secondPlaylistItem.getUri())) {
try {
assertThat(readAllowedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
} catch (Exception e) {
assertWithMessage("Unexpected exception %s", e).fail();
}
}
});
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).build())) {
assertPlayerResultSuccess(sessionPlayerConnector.setPlaylist(testPlaylist, null));
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
CountDownLatch seekToAllowedForSecondMediaItem = new CountDownLatch(1);
OnAllowedCommandsChangedListener allowedCommandsChangedListener = (controller, allowedCommands) -> {
if (allowedCommands.hasCommand(SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO) && controller.getCurrentMediaItemIndex() == 1) {
seekToAllowedForSecondMediaItem.countDown();
}
};
try (MediaController controller = createConnectedController(session, /* onConnectedListener= */
null, allowedCommandsChangedListener)) {
assertPlayerResultSuccess(sessionPlayerConnector.skipToNextPlaylistItem());
readAllowedLatch.countDown();
assertThat(seekToAllowedForSecondMediaItem.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
}
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class PlayerWrapper method addPlaylistItem.
public boolean addPlaylistItem(int index, androidx.media2.common.MediaItem media2MediaItem) {
Assertions.checkArgument(!media2Playlist.contains(media2MediaItem));
index = Util.constrainValue(index, 0, media2Playlist.size());
MediaItem exoPlayerMediaItem = Assertions.checkNotNull(mediaItemConverter.convertToExoPlayerMediaItem(media2MediaItem));
player.addMediaItem(index, exoPlayerMediaItem);
return true;
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class PlayerWrapper method setPlaylist.
public boolean setPlaylist(List<androidx.media2.common.MediaItem> playlist, @Nullable MediaMetadata metadata) {
// Check for duplication.
for (int i = 0; i < playlist.size(); i++) {
androidx.media2.common.MediaItem media2MediaItem = playlist.get(i);
Assertions.checkArgument(playlist.indexOf(media2MediaItem) == i);
}
this.playlistMetadata = metadata;
List<MediaItem> exoPlayerMediaItems = new ArrayList<>();
for (int i = 0; i < playlist.size(); i++) {
androidx.media2.common.MediaItem media2MediaItem = playlist.get(i);
MediaItem exoPlayerMediaItem = Assertions.checkNotNull(mediaItemConverter.convertToExoPlayerMediaItem(media2MediaItem));
exoPlayerMediaItems.add(exoPlayerMediaItem);
}
player.setMediaItems(exoPlayerMediaItems, /* resetPosition= */
true);
currentWindowIndex = getCurrentMediaItemIndex();
return true;
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class PlayerWrapper method updatePlaylist.
private void updatePlaylist(Timeline timeline) {
List<androidx.media2.common.MediaItem> media2MediaItemToBeRemoved = new ArrayList<>(media2Playlist);
media2Playlist.clear();
exoPlayerPlaylist.clear();
Timeline.Window window = new Timeline.Window();
int windowCount = timeline.getWindowCount();
for (int i = 0; i < windowCount; i++) {
timeline.getWindow(i, window);
MediaItem exoPlayerMediaItem = window.mediaItem;
androidx.media2.common.MediaItem media2MediaItem = Assertions.checkNotNull(mediaItemConverter.convertToMedia2MediaItem(exoPlayerMediaItem));
exoPlayerPlaylist.add(exoPlayerMediaItem);
media2Playlist.add(media2MediaItem);
media2MediaItemToBeRemoved.remove(media2MediaItem);
}
for (androidx.media2.common.MediaItem item : media2MediaItemToBeRemoved) {
releaseMediaItem(item);
}
}
Aggregations