use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class SessionPlayerConnectorTest method getPlaylist_returnsPlaylistInUnderlyingPlayer.
@Test
@LargeTest
public void getPlaylist_returnsPlaylistInUnderlyingPlayer() {
List<MediaItem> playlistToExoPlayer = TestUtils.createPlaylist(4);
DefaultMediaItemConverter converter = new DefaultMediaItemConverter();
List<com.google.android.exoplayer2.MediaItem> exoMediaItems = new ArrayList<>();
for (MediaItem mediaItem : playlistToExoPlayer) {
exoMediaItems.add(converter.convertToExoPlayerMediaItem(mediaItem));
}
AtomicReference<List<MediaItem>> playlistFromSessionPlayer = new AtomicReference<>();
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
exoPlayer.setMediaItems(exoMediaItems);
try (SessionPlayerConnector sessionPlayer = new SessionPlayerConnector(exoPlayer)) {
List<MediaItem> playlist = sessionPlayer.getPlaylist();
playlistFromSessionPlayer.set(playlist);
}
});
assertThat(playlistFromSessionPlayer.get()).isEqualTo(playlistToExoPlayer);
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class SessionPlayerConnectorTest method setPlaybackSpeed_afterPlayback_remainsSame.
@Test
@LargeTest
public void setPlaybackSpeed_afterPlayback_remainsSame() throws Exception {
int resId1 = R.raw.video_big_buck_bunny;
MediaItem mediaItem1 = new UriMediaItem.Builder(RawResourceDataSource.buildRawResourceUri(resId1)).setStartPosition(6_000).setEndPosition(7_000).build();
MediaItem mediaItem2 = new UriMediaItem.Builder(RawResourceDataSource.buildRawResourceUri(resId1)).setStartPosition(3_000).setEndPosition(4_000).build();
List<MediaItem> items = new ArrayList<>();
items.add(mediaItem1);
items.add(mediaItem2);
sessionPlayerConnector.setPlaylist(items, null);
CountDownLatch onPlaybackCompletedLatch = new CountDownLatch(1);
SessionPlayer.PlayerCallback callback = new SessionPlayer.PlayerCallback() {
@Override
public void onPlaybackCompleted(SessionPlayer player) {
onPlaybackCompletedLatch.countDown();
}
};
sessionPlayerConnector.registerPlayerCallback(executor, callback);
sessionPlayerConnector.prepare().get();
sessionPlayerConnector.setPlaybackSpeed(2.0f);
sessionPlayerConnector.play();
assertThat(onPlaybackCompletedLatch.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
assertThat(sessionPlayerConnector.getCurrentMediaItem()).isEqualTo(mediaItem2);
assertThat(sessionPlayerConnector.getPlaybackSpeed()).isWithin(0.001f).of(2.0f);
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class SessionPlayerConnectorTest method addPlaylistItem_calledOnlyOnce_notifiesPlaylistChangeOnlyOnce.
@Test
@LargeTest
public void addPlaylistItem_calledOnlyOnce_notifiesPlaylistChangeOnlyOnce() throws Exception {
List<MediaItem> playlist = TestUtils.createPlaylist(10);
assertPlayerResultSuccess(sessionPlayerConnector.setPlaylist(playlist, /* metadata= */
null));
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
CountDownLatch onPlaylistChangedLatch = new CountDownLatch(2);
int addIndex = 2;
MediaItem newMediaItem = TestUtils.createMediaItem();
playlist.add(addIndex, newMediaItem);
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
@Override
public void onPlaylistChanged(SessionPlayer player, @Nullable List<MediaItem> list, @Nullable MediaMetadata metadata) {
assertThat(list).isEqualTo(playlist);
onPlaylistChangedLatch.countDown();
}
});
sessionPlayerConnector.addPlaylistItem(addIndex, newMediaItem);
assertThat(onPlaylistChangedLatch.await(PLAYLIST_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isFalse();
assertThat(onPlaylistChangedLatch.getCount()).isEqualTo(1);
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class SessionPlayerConnectorTest method play_withPlaylist_notifiesOnCurrentMediaItemChangedAndOnPlaybackCompleted.
@Test
@LargeTest
public void play_withPlaylist_notifiesOnCurrentMediaItemChangedAndOnPlaybackCompleted() 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));
CountDownLatch onPlaybackCompletedLatch = new CountDownLatch(1);
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
int currentMediaItemChangedCount = 0;
@Override
public void onCurrentMediaItemChanged(SessionPlayer player, MediaItem item) {
assertThat(item).isEqualTo(player.getCurrentMediaItem());
int expectedCurrentIndex = currentMediaItemChangedCount++;
assertThat(player.getCurrentMediaItemIndex()).isEqualTo(expectedCurrentIndex);
assertThat(item).isEqualTo(playlist.get(expectedCurrentIndex));
}
@Override
public void onPlaybackCompleted(SessionPlayer player) {
onPlaybackCompletedLatch.countDown();
}
});
assertThat(sessionPlayerConnector.setPlaylist(playlist, null)).isNotNull();
assertThat(sessionPlayerConnector.prepare()).isNotNull();
assertThat(sessionPlayerConnector.play()).isNotNull();
assertThat(onPlaybackCompletedLatch.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
use of androidx.media2.common.MediaItem in project ExoPlayer by google.
the class SessionPlayerConnector method handlePlaylistChangedOnHandler.
// TODO(internal b/160846312): Remove this suppress warnings and call onCurrentMediaItemChanged
// with a null item once we depend on media2 1.2.0.
private void handlePlaylistChangedOnHandler() {
List<MediaItem> currentPlaylist = player.getPlaylist();
MediaMetadata playlistMetadata = player.getPlaylistMetadata();
MediaItem currentMediaItem = player.getCurrentMediaItem();
boolean notifyCurrentMediaItem = !Util.areEqual(this.currentMediaItem, currentMediaItem) && currentMediaItem != null;
this.currentMediaItem = currentMediaItem;
long currentPosition = getCurrentPosition();
notifySessionPlayerCallback(callback -> {
callback.onPlaylistChanged(SessionPlayerConnector.this, currentPlaylist, playlistMetadata);
if (notifyCurrentMediaItem) {
callback.onCurrentMediaItemChanged(SessionPlayerConnector.this, currentMediaItem);
}
});
}
Aggregations