Search in sources :

Example 11 with MediaItem

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

the class SessionPlayerConnectorTest method setRepeatMode_withRepeatAll_continuesToPlayPlaylistWithoutBeingCompleted.

@Test
@LargeTest
public void setRepeatMode_withRepeatAll_continuesToPlayPlaylistWithoutBeingCompleted() 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));
    int listSize = playlist.size();
    // Any value more than list size + 1, to see repeat mode with the recorded video.
    CopyOnWriteArrayList<MediaItem> currentMediaItemChanges = new CopyOnWriteArrayList<>();
    PlayerCallbackForPlaylist callback = new PlayerCallbackForPlaylist(playlist, listSize + 2) {

        @Override
        public void onCurrentMediaItemChanged(SessionPlayer player, MediaItem item) {
            super.onCurrentMediaItemChanged(player, item);
            currentMediaItemChanges.add(item);
            countDown();
        }

        @Override
        public void onPlaybackCompleted(SessionPlayer player) {
            assertWithMessage("Playback shouldn't be completed, Actual changes were %s", currentMediaItemChanges).fail();
        }
    };
    sessionPlayerConnector.registerPlayerCallback(executor, callback);
    assertThat(sessionPlayerConnector.setPlaylist(playlist, null)).isNotNull();
    assertThat(sessionPlayerConnector.prepare()).isNotNull();
    assertThat(sessionPlayerConnector.setRepeatMode(SessionPlayer.REPEAT_MODE_ALL)).isNotNull();
    assertThat(sessionPlayerConnector.play()).isNotNull();
    assertWithMessage("Current media item didn't change as expected. Actual changes were %s", currentMediaItemChanges).that(callback.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
    int expectedMediaItemIndex = 0;
    for (MediaItem mediaItemInPlaybackOrder : currentMediaItemChanges) {
        assertWithMessage("Unexpected media item for %sth playback. Actual changes were %s", expectedMediaItemIndex, currentMediaItemChanges).that(mediaItemInPlaybackOrder).isEqualTo(playlist.get(expectedMediaItemIndex));
        expectedMediaItemIndex = (expectedMediaItemIndex + 1) % listSize;
    }
}
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) 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)

Example 12 with MediaItem

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

the class SessionCallbackBuilderTest method allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable.

@Test
public void allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable() throws Exception {
    List<MediaItem> testPlaylist = new ArrayList<>();
    testPlaylist.add(TestUtils.createMediaItem(R.raw.video_desks));
    UriMediaItem secondPlaylistItem = TestUtils.createMediaItem(R.raw.video_big_buck_bunny);
    testPlaylist.add(secondPlaylistItem);
    CountDownLatch readAllowedLatch = new CountDownLatch(1);
    playerTestRule.setDataSourceInstrumentation(dataSpec -> {
        if (dataSpec.uri.equals(secondPlaylistItem.getUri())) {
            try {
                assertThat(readAllowedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
            } catch (Exception e) {
                assertWithMessage("Unexpected exception %s", e).fail();
            }
        }
    });
    try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).build())) {
        assertPlayerResultSuccess(sessionPlayerConnector.setPlaylist(testPlaylist, null));
        assertPlayerResultSuccess(sessionPlayerConnector.prepare());
        CountDownLatch seekToAllowedForSecondMediaItem = new CountDownLatch(1);
        OnAllowedCommandsChangedListener allowedCommandsChangedListener = (controller, allowedCommands) -> {
            if (allowedCommands.hasCommand(SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO) && controller.getCurrentMediaItemIndex() == 1) {
                seekToAllowedForSecondMediaItem.countDown();
            }
        };
        try (MediaController controller = createConnectedController(session, /* onConnectedListener= */
        null, allowedCommandsChangedListener)) {
            assertPlayerResultSuccess(sessionPlayerConnector.skipToNextPlaylistItem());
            readAllowedLatch.countDown();
            assertThat(seekToAllowedForSecondMediaItem.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) MediaSession(androidx.media2.session.MediaSession) UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) UriMediaItem(androidx.media2.common.UriMediaItem) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 13 with MediaItem

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

the class PlayerWrapper method addPlaylistItem.

public boolean addPlaylistItem(int index, androidx.media2.common.MediaItem media2MediaItem) {
    Assertions.checkArgument(!media2Playlist.contains(media2MediaItem));
    index = Util.constrainValue(index, 0, media2Playlist.size());
    MediaItem exoPlayerMediaItem = Assertions.checkNotNull(mediaItemConverter.convertToExoPlayerMediaItem(media2MediaItem));
    player.addMediaItem(index, exoPlayerMediaItem);
    return true;
}
Also used : CallbackMediaItem(androidx.media2.common.CallbackMediaItem) MediaItem(com.google.android.exoplayer2.MediaItem)

Example 14 with MediaItem

use of androidx.media2.common.MediaItem 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 15 with MediaItem

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

the class PlayerWrapper method updatePlaylist.

private void updatePlaylist(Timeline timeline) {
    List<androidx.media2.common.MediaItem> media2MediaItemToBeRemoved = new ArrayList<>(media2Playlist);
    media2Playlist.clear();
    exoPlayerPlaylist.clear();
    Timeline.Window window = new Timeline.Window();
    int windowCount = timeline.getWindowCount();
    for (int i = 0; i < windowCount; i++) {
        timeline.getWindow(i, window);
        MediaItem exoPlayerMediaItem = window.mediaItem;
        androidx.media2.common.MediaItem media2MediaItem = Assertions.checkNotNull(mediaItemConverter.convertToMedia2MediaItem(exoPlayerMediaItem));
        exoPlayerPlaylist.add(exoPlayerMediaItem);
        media2Playlist.add(media2MediaItem);
        media2MediaItemToBeRemoved.remove(media2MediaItem);
    }
    for (androidx.media2.common.MediaItem item : media2MediaItemToBeRemoved) {
        releaseMediaItem(item);
    }
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) CallbackMediaItem(androidx.media2.common.CallbackMediaItem) MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList)

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