Search in sources :

Example 21 with SessionPlayer

use of androidx.media2.common.SessionPlayer 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 SessionPlayer

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

the class SessionPlayerConnectorTest method play_onceWithAudioResource_changesPlayerStateToPlaying.

@Test
@LargeTest
public void play_onceWithAudioResource_changesPlayerStateToPlaying() throws Exception {
    TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
    AudioAttributesCompat attributes = new AudioAttributesCompat.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build();
    sessionPlayerConnector.setAudioAttributes(attributes);
    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();
            }
        }
    });
    sessionPlayerConnector.prepare();
    sessionPlayerConnector.play();
    assertThat(onPlayingLatch.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) LargeTest(androidx.test.filters.LargeTest)

Example 23 with SessionPlayer

use of androidx.media2.common.SessionPlayer 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 24 with SessionPlayer

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

the class SessionPlayerConnectorTest method pause_byUnderlyingPlayer_notifiesOnPlayerStateChanges.

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

        @Override
        public void onPlayerStateChanged(SessionPlayer player, int playerState) {
            if (playerState == PLAYER_STATE_PAUSED) {
                onPausedLatch.countDown();
            }
        }
    });
    assertPlayerResultSuccess(sessionPlayerConnector.play());
    InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> exoPlayer.setPlayWhenReady(false));
    assertThat(onPausedLatch.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)

Example 25 with SessionPlayer

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

the class SessionPlayerConnectorTest method pause_byUnderlyingPlayerInListener_changesToPlayerStatePaused.

@Test
@LargeTest
public void pause_byUnderlyingPlayerInListener_changesToPlayerStatePaused() throws Exception {
    TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
    ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
    CountDownLatch playerStateChangesLatch = new CountDownLatch(3);
    CopyOnWriteArrayList<Integer> playerStateChanges = new CopyOnWriteArrayList<>();
    sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {

        @Override
        public void onPlayerStateChanged(SessionPlayer player, int playerState) {
            playerStateChanges.add(playerState);
            playerStateChangesLatch.countDown();
        }
    });
    assertPlayerResultSuccess(sessionPlayerConnector.prepare());
    InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> exoPlayer.addListener(new Player.Listener() {

        @Override
        public void onPlayWhenReadyChanged(boolean playWhenReady, int reason) {
            if (playWhenReady) {
                exoPlayer.setPlayWhenReady(false);
            }
        }
    }));
    assertPlayerResultSuccess(sessionPlayerConnector.play());
    assertThat(playerStateChangesLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
    assertThat(playerStateChanges).containsExactly(// After prepare()
    PLAYER_STATE_PAUSED, // After play()
    PLAYER_STATE_PLAYING, // After setPlayWhenREady(false)
    PLAYER_STATE_PAUSED).inOrder();
    assertThat(sessionPlayerConnector.getPlayerState()).isEqualTo(PLAYER_STATE_PAUSED);
}
Also used : SessionPlayer(androidx.media2.common.SessionPlayer) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) 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