Search in sources :

Example 21 with MediaItem

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);
}
Also used : UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) 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 22 with MediaItem

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);
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) UriMediaItem(androidx.media2.common.UriMediaItem) 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 23 with MediaItem

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);
}
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 24 with MediaItem

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();
}
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) 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 25 with MediaItem

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);
        }
    });
}
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)22 UriMediaItem (androidx.media2.common.UriMediaItem)18 LargeTest (androidx.test.filters.LargeTest)16 Test (org.junit.Test)16 SessionPlayer (androidx.media2.common.SessionPlayer)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 MediumTest (androidx.test.filters.MediumTest)13 SmallTest (androidx.test.filters.SmallTest)13 ArrayList (java.util.ArrayList)13 MediaMetadata (androidx.media2.common.MediaMetadata)11 CallbackMediaItem (androidx.media2.common.CallbackMediaItem)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 Nullable (androidx.annotation.Nullable)7 FileMediaItem (androidx.media2.common.FileMediaItem)5 Uri (android.net.Uri)4 SessionCommand (androidx.media2.session.SessionCommand)4 SessionCommandGroup (androidx.media2.session.SessionCommandGroup)4 List (java.util.List)4 Context (android.content.Context)3 Bundle (android.os.Bundle)3