Search in sources :

Example 6 with SessionPlayer

use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.

the class SessionPlayerConnectorTest method setPlaylist_byUnderlyingPlayerAfterPrepare_notifiesOnPlaylistChanged.

@Test
@LargeTest
public void setPlaylist_byUnderlyingPlayerAfterPrepare_notifiesOnPlaylistChanged() throws Exception {
    List<MediaItem> playlistToSessionPlayer = TestUtils.createPlaylist(2);
    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();
            }
        }
    });
    sessionPlayerConnector.prepare();
    sessionPlayerConnector.setPlaylist(playlistToSessionPlayer, /* metadata= */
    null);
    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 7 with SessionPlayer

use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.

the class SessionPlayerConnectorTest method prepare_notifiesOnPlayerStateChanged.

@Test
@LargeTest
public void prepare_notifiesOnPlayerStateChanged() throws Throwable {
    TestUtils.loadResource(R.raw.video_big_buck_bunny, sessionPlayerConnector);
    CountDownLatch onPlayerStatePaused = new CountDownLatch(1);
    SessionPlayer.PlayerCallback callback = new SessionPlayer.PlayerCallback() {

        @Override
        public void onPlayerStateChanged(SessionPlayer player, int state) {
            if (state == SessionPlayer.PLAYER_STATE_PAUSED) {
                onPlayerStatePaused.countDown();
            }
        }
    };
    sessionPlayerConnector.registerPlayerCallback(executor, callback);
    assertPlayerResultSuccess(sessionPlayerConnector.prepare());
    assertThat(onPlayerStatePaused.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
Also used : SessionPlayer(androidx.media2.common.SessionPlayer) 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 8 with SessionPlayer

use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.

the class SessionPlayerConnectorTest method removePlaylistItem_calledOnlyOnce_notifiesPlaylistChangeOnlyOnce.

@Test
@LargeTest
public void removePlaylistItem_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 removeIndex = 3;
    playlist.remove(removeIndex);
    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.removePlaylistItem(removeIndex);
    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 9 with SessionPlayer

use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.

the class SessionPlayerConnectorTest method play_onceWithAudioResourceOnMainThread_notifiesOnPlayerStateChanged.

@Test
@MediumTest
public void play_onceWithAudioResourceOnMainThread_notifiesOnPlayerStateChanged() throws Exception {
    CountDownLatch onPlayerStatePlayingLatch = new CountDownLatch(1);
    InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
        try {
            TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
        } catch (Exception e) {
            assertWithMessage(e.getMessage()).fail();
        }
        AudioAttributesCompat attributes = new AudioAttributesCompat.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build();
        sessionPlayerConnector.setAudioAttributes(attributes);
        sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {

            @Override
            public void onPlayerStateChanged(SessionPlayer player, int playerState) {
                if (playerState == PLAYER_STATE_PLAYING) {
                    onPlayerStatePlayingLatch.countDown();
                }
            }
        });
        sessionPlayerConnector.prepare();
        sessionPlayerConnector.play();
    });
    assertThat(onPlayerStatePlayingLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
Also used : AudioAttributesCompat(androidx.media.AudioAttributesCompat) SessionPlayer(androidx.media2.common.SessionPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) MediumTest(androidx.test.filters.MediumTest) LargeTest(androidx.test.filters.LargeTest) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) MediumTest(androidx.test.filters.MediumTest)

Example 10 with SessionPlayer

use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.

the class SessionPlayerConnectorTest method play_byUnderlyingPlayer_notifiesOnPlayerStateChanges.

@Test
@LargeTest
public void play_byUnderlyingPlayer_notifiesOnPlayerStateChanges() throws Exception {
    TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
    ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
    CountDownLatch onPlayingLatch = new CountDownLatch(1);
    sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {

        @Override
        public void onPlayerStateChanged(SessionPlayer player, int playerState) {
            if (playerState == PLAYER_STATE_PLAYING) {
                onPlayingLatch.countDown();
            }
        }
    });
    assertPlayerResultSuccess(sessionPlayerConnector.prepare());
    InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> exoPlayer.setPlayWhenReady(true));
    assertThat(onPlayingLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
Also used : SessionPlayer(androidx.media2.common.SessionPlayer) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) 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)

Aggregations

LargeTest (androidx.test.filters.LargeTest)26 Test (org.junit.Test)26 SessionPlayer (androidx.media2.common.SessionPlayer)25 MediumTest (androidx.test.filters.MediumTest)25 SmallTest (androidx.test.filters.SmallTest)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 MediaItem (androidx.media2.common.MediaItem)16 UriMediaItem (androidx.media2.common.UriMediaItem)15 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)11 ArrayList (java.util.ArrayList)9 MediaMetadata (androidx.media2.common.MediaMetadata)8 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)5 Nullable (androidx.annotation.Nullable)3 AudioAttributesCompat (androidx.media.AudioAttributesCompat)2 SessionCommand (androidx.media2.session.SessionCommand)2 SessionCommandGroup (androidx.media2.session.SessionCommandGroup)2 List (java.util.List)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Context (android.content.Context)1 Uri (android.net.Uri)1