Search in sources :

Example 16 with SessionPlayer

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

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

the class TestUtils method loadResource.

public static void loadResource(int resId, SessionPlayer sessionPlayer) throws Exception {
    MediaItem mediaItem = createMediaItem(resId);
    assertPlayerResultSuccess(sessionPlayer.setMediaItem(mediaItem));
}
Also used : UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem)

Example 18 with SessionPlayer

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

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

the class SessionPlayerConnectorTest method seekTo_whenPrepared_notifiesOnSeekCompleted.

@Test
@LargeTest
public void seekTo_whenPrepared_notifiesOnSeekCompleted() throws Throwable {
    long mp4DurationMs = 8_484L;
    TestUtils.loadResource(R.raw.video_big_buck_bunny, sessionPlayerConnector);
    assertPlayerResultSuccess(sessionPlayerConnector.prepare());
    CountDownLatch onSeekCompletedLatch = new CountDownLatch(1);
    SessionPlayer.PlayerCallback callback = new SessionPlayer.PlayerCallback() {

        @Override
        public void onSeekCompleted(SessionPlayer player, long position) {
            onSeekCompletedLatch.countDown();
        }
    };
    sessionPlayerConnector.registerPlayerCallback(executor, callback);
    sessionPlayerConnector.seekTo(mp4DurationMs >> 1);
    assertThat(onSeekCompletedLatch.await(PLAYBACK_COMPLETED_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 20 with SessionPlayer

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

the class SessionPlayerConnectorTest method seekTo_byUnderlyingPlayer_notifiesOnSeekCompleted.

@Test
@LargeTest
public void seekTo_byUnderlyingPlayer_notifiesOnSeekCompleted() throws Exception {
    TestUtils.loadResource(R.raw.video_big_buck_bunny, sessionPlayerConnector);
    assertPlayerResultSuccess(sessionPlayerConnector.prepare());
    ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
    long testSeekPosition = 1023;
    AtomicLong seekPosition = new AtomicLong();
    CountDownLatch onSeekCompletedLatch = new CountDownLatch(1);
    sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {

        @Override
        public void onSeekCompleted(SessionPlayer player, long position) {
            // Do not assert here, because onSeekCompleted() can be called after the player is
            // closed.
            seekPosition.set(position);
            onSeekCompletedLatch.countDown();
        }
    });
    InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> exoPlayer.seekTo(testSeekPosition));
    assertThat(onSeekCompletedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
    assertThat(seekPosition.get()).isEqualTo(testSeekPosition);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) 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