Search in sources :

Example 6 with MediaMetadata

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;
}
Also used : CallbackMediaItem(androidx.media2.common.CallbackMediaItem) MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList)

Example 7 with MediaMetadata

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();
        }
    }
}
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) SessionPlayer(androidx.media2.common.SessionPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) MediaMetadata(androidx.media2.common.MediaMetadata) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 8 with MediaMetadata

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();
}
Also used : UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) SessionPlayer(androidx.media2.common.SessionPlayer) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MediaMetadata(androidx.media2.common.MediaMetadata) CountDownLatch(java.util.concurrent.CountDownLatch) MediumTest(androidx.test.filters.MediumTest) LargeTest(androidx.test.filters.LargeTest) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Example 9 with MediaMetadata

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);
}
Also used : UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) CountDownLatch(java.util.concurrent.CountDownLatch) MediumTest(androidx.test.filters.MediumTest) LargeTest(androidx.test.filters.LargeTest) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Example 10 with MediaMetadata

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);
        }
    });
}
Also used : CallbackMediaItem(androidx.media2.common.CallbackMediaItem) MediaItem(androidx.media2.common.MediaItem) FileMediaItem(androidx.media2.common.FileMediaItem) MediaMetadata(androidx.media2.common.MediaMetadata)

Aggregations

MediaItem (androidx.media2.common.MediaItem)10 MediaMetadata (androidx.media2.common.MediaMetadata)9 SessionPlayer (androidx.media2.common.SessionPlayer)8 UriMediaItem (androidx.media2.common.UriMediaItem)8 LargeTest (androidx.test.filters.LargeTest)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 Test (org.junit.Test)8 MediumTest (androidx.test.filters.MediumTest)7 SmallTest (androidx.test.filters.SmallTest)7 ArrayList (java.util.ArrayList)5 CallbackMediaItem (androidx.media2.common.CallbackMediaItem)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 FileMediaItem (androidx.media2.common.FileMediaItem)2 Context (android.content.Context)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 TextUtils (android.text.TextUtils)1 Nullable (androidx.annotation.Nullable)1 ContextCompat (androidx.core.content.ContextCompat)1 Rating (androidx.media2.common.Rating)1