Search in sources :

Example 1 with UriMediaItem

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();
}
Also used : CallbackMediaItem(androidx.media2.common.CallbackMediaItem) Uri(android.net.Uri) FileMediaItem(androidx.media2.common.FileMediaItem) Nullable(androidx.annotation.Nullable) UriMediaItem(androidx.media2.common.UriMediaItem)

Example 2 with UriMediaItem

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();
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) MediaSession(androidx.media2.session.MediaSession) UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) UriMediaItem(androidx.media2.common.UriMediaItem) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Uri (android.net.Uri)2 Nullable (androidx.annotation.Nullable)2 UriMediaItem (androidx.media2.common.UriMediaItem)2 Context (android.content.Context)1 Bundle (android.os.Bundle)1 TextUtils (android.text.TextUtils)1 ContextCompat (androidx.core.content.ContextCompat)1 CallbackMediaItem (androidx.media2.common.CallbackMediaItem)1 FileMediaItem (androidx.media2.common.FileMediaItem)1 MediaItem (androidx.media2.common.MediaItem)1 MediaMetadata (androidx.media2.common.MediaMetadata)1 Rating (androidx.media2.common.Rating)1 SessionPlayer (androidx.media2.common.SessionPlayer)1 HeartRating (androidx.media2.session.HeartRating)1 MediaController (androidx.media2.session.MediaController)1 MediaSession (androidx.media2.session.MediaSession)1 SessionCommand (androidx.media2.session.SessionCommand)1 SessionCommandGroup (androidx.media2.session.SessionCommandGroup)1 SessionResult (androidx.media2.session.SessionResult)1 ApplicationProvider (androidx.test.core.app.ApplicationProvider)1