use of androidx.media2.common.UriMediaItem in project ExoPlayer by google.
the class DefaultMediaItemConverter method convertToExoPlayerMediaItem.
@Override
public MediaItem convertToExoPlayerMediaItem(androidx.media2.common.MediaItem media2MediaItem) {
if (media2MediaItem instanceof FileMediaItem) {
throw new IllegalStateException("FileMediaItem isn't supported");
}
if (media2MediaItem instanceof CallbackMediaItem) {
throw new IllegalStateException("CallbackMediaItem isn't supported");
}
@Nullable Uri uri = null;
@Nullable String mediaId = null;
@Nullable String title = null;
if (media2MediaItem instanceof UriMediaItem) {
UriMediaItem uriMediaItem = (UriMediaItem) media2MediaItem;
uri = uriMediaItem.getUri();
}
@Nullable androidx.media2.common.MediaMetadata metadata = media2MediaItem.getMetadata();
if (metadata != null) {
@Nullable String uriString = metadata.getString(METADATA_KEY_MEDIA_URI);
mediaId = metadata.getString(METADATA_KEY_MEDIA_ID);
if (uri == null) {
if (uriString != null) {
uri = Uri.parse(uriString);
} else if (mediaId != null) {
uri = Uri.parse("media2:///" + mediaId);
}
}
title = metadata.getString(METADATA_KEY_DISPLAY_TITLE);
if (title == null) {
title = metadata.getString(METADATA_KEY_TITLE);
}
}
if (uri == null) {
// Generate a URI to make it non-null. If not, then the tag passed to setTag will be ignored.
uri = Uri.parse("media2:///");
}
long startPositionMs = media2MediaItem.getStartPosition();
if (startPositionMs == androidx.media2.common.MediaItem.POSITION_UNKNOWN) {
startPositionMs = 0;
}
long endPositionMs = media2MediaItem.getEndPosition();
if (endPositionMs == androidx.media2.common.MediaItem.POSITION_UNKNOWN) {
endPositionMs = C.TIME_END_OF_SOURCE;
}
return new MediaItem.Builder().setUri(uri).setMediaId(mediaId != null ? mediaId : MediaItem.DEFAULT_MEDIA_ID).setMediaMetadata(new MediaMetadata.Builder().setTitle(title).build()).setTag(media2MediaItem).setClippingConfiguration(new MediaItem.ClippingConfiguration.Builder().setStartPositionMs(startPositionMs).setEndPositionMs(endPositionMs).build()).build();
}
use of androidx.media2.common.UriMediaItem 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();
}
}
}
Aggregations