use of androidx.media2.common.MediaMetadata 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.MediaMetadata in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setMediaItemProvider_withMediaItemProvider_receivesOnCreateMediaItem.
@Test
public void setMediaItemProvider_withMediaItemProvider_receivesOnCreateMediaItem() throws Exception {
Uri testMediaUri = RawResourceDataSource.buildRawResourceUri(R.raw.audio);
CountDownLatch providerLatch = new CountDownLatch(1);
SessionCallbackBuilder.MediaIdMediaItemProvider mediaIdMediaItemProvider = new SessionCallbackBuilder.MediaIdMediaItemProvider();
SessionCallbackBuilder.MediaItemProvider provider = (session, controllerInfo, mediaId) -> {
assertThat(mediaId).isEqualTo(testMediaUri.toString());
providerLatch.countDown();
return mediaIdMediaItemProvider.onCreateMediaItem(session, controllerInfo, mediaId);
};
CountDownLatch currentMediaItemChangedLatch = new CountDownLatch(1);
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
@Override
public void onCurrentMediaItemChanged(SessionPlayer player, MediaItem item) {
MediaMetadata metadata = item.getMetadata();
assertThat(metadata.getString(MediaMetadata.METADATA_KEY_MEDIA_ID)).isEqualTo(testMediaUri.toString());
currentMediaItemChangedLatch.countDown();
}
});
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setMediaItemProvider(provider).build())) {
try (MediaController controller = createConnectedController(session)) {
assertSessionResultSuccess(controller.setMediaItem(testMediaUri.toString()), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat(providerLatch.await(0, MILLISECONDS)).isTrue();
assertThat(currentMediaItemChangedLatch.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
}
}
use of androidx.media2.common.MediaMetadata in project ExoPlayer by google.
the class SessionPlayerConnectorTest method setPlaylist_byUnderlyingPlayerBeforePrepare_notifiesOnPlaylistChanged.
@Test
@LargeTest
public void setPlaylist_byUnderlyingPlayerBeforePrepare_notifiesOnPlaylistChanged() throws Exception {
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));
}
CountDownLatch onPlaylistChangedLatch = new CountDownLatch(1);
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
@Override
public void onPlaylistChanged(SessionPlayer player, @Nullable List<MediaItem> list, @Nullable MediaMetadata metadata) {
if (Util.areEqual(list, playlistToExoPlayer)) {
onPlaylistChangedLatch.countDown();
}
}
});
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> playerTestRule.getExoPlayer().setMediaItems(exoMediaItems));
assertThat(onPlaylistChangedLatch.await(PLAYLIST_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
use of androidx.media2.common.MediaMetadata 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.MediaMetadata 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