Search in sources :

Example 26 with State

use of androidx.media3.common.Player.State in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method playerError_notified.

@Test
public void playerError_notified() throws Exception {
    PlaybackException testPlayerError = new PlaybackException(/* messaage= */
    "player error", /* cause= */
    null, PlaybackException.ERROR_CODE_UNSPECIFIED);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

        @Override
        public void onPlaybackStateChanged(PlaybackStateCompat state) {
            playbackStateCompatRef.set(state);
            latch.countDown();
        }
    };
    controllerCompat.registerCallback(callback, handler);
    session.getMockPlayer().notifyPlayerError(testPlayerError);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    PlaybackStateCompat state = playbackStateCompatRef.get();
    assertPlaybackStateCompatErrorEquals(state, testPlayerError);
}
Also used : PlaybackException(androidx.media3.common.PlaybackException) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) AtomicReference(java.util.concurrent.atomic.AtomicReference) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 27 with State

use of androidx.media3.common.Player.State in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method onPlaybackParametersChanged_notifiesPlaybackStateCompatChanges.

@Test
public void onPlaybackParametersChanged_notifiesPlaybackStateCompatChanges() throws Exception {
    PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
    1.5f);
    AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

        @Override
        public void onPlaybackStateChanged(PlaybackStateCompat state) {
            playbackStateRef.set(state);
            latch.countDown();
        }
    };
    controllerCompat.registerCallback(callback, handler);
    session.getMockPlayer().notifyPlaybackParametersChanged(playbackParameters);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(playbackStateRef.get().getPlaybackSpeed()).isWithin(EPSILON).of(playbackParameters.speed);
    assertThat(controllerCompat.getPlaybackState().getPlaybackSpeed()).isWithin(EPSILON).of(playbackParameters.speed);
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) AtomicReference(java.util.concurrent.atomic.AtomicReference) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) CountDownLatch(java.util.concurrent.CountDownLatch) PlaybackParameters(androidx.media3.common.PlaybackParameters) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 28 with State

use of androidx.media3.common.Player.State in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method setPlayer_isNotified.

@Test
public void setPlayer_isNotified() throws Exception {
    @State int testState = STATE_READY;
    boolean testPlayWhenReady = true;
    long testDurationMs = 200;
    long testCurrentPositionMs = 11;
    long testBufferedPositionMs = 100;
    PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
    1.5f);
    int testItemIndex = 0;
    List<MediaItem> testMediaItems = MediaTestUtils.createMediaItems(/* size= */
    3);
    testMediaItems.set(testItemIndex, new MediaItem.Builder().setMediaId(testMediaItems.get(testItemIndex).mediaId).setMediaMetadata(new MediaMetadata.Builder().setUserRating(new HeartRating(/* isHeart= */
    true)).build()).build());
    Timeline testTimeline = new PlaylistTimeline(testMediaItems);
    String testPlaylistTitle = "testPlaylistTitle";
    MediaMetadata testPlaylistMetadata = new MediaMetadata.Builder().setTitle(testPlaylistTitle).build();
    boolean testShuffleModeEnabled = true;
    @RepeatMode int testRepeatMode = Player.REPEAT_MODE_ONE;
    AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
    AtomicReference<MediaMetadataCompat> metadataRef = new AtomicReference<>();
    AtomicReference<CharSequence> queueTitleRef = new AtomicReference<>();
    AtomicInteger shuffleModeRef = new AtomicInteger();
    AtomicInteger repeatModeRef = new AtomicInteger();
    CountDownLatch latchForPlaybackState = new CountDownLatch(1);
    CountDownLatch latchForMetadata = new CountDownLatch(1);
    CountDownLatch latchForQueue = new CountDownLatch(2);
    CountDownLatch latchForShuffleMode = new CountDownLatch(1);
    CountDownLatch latchForRepeatMode = new CountDownLatch(1);
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

        @Override
        public void onPlaybackStateChanged(PlaybackStateCompat state) {
            playbackStateRef.set(state);
            latchForPlaybackState.countDown();
        }

        @Override
        public void onMetadataChanged(MediaMetadataCompat metadata) {
            metadataRef.set(metadata);
            latchForMetadata.countDown();
        }

        @Override
        public void onQueueChanged(List<QueueItem> queue) {
            latchForQueue.countDown();
        }

        @Override
        public void onQueueTitleChanged(CharSequence title) {
            queueTitleRef.set(title);
            latchForQueue.countDown();
        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            repeatModeRef.set(repeatMode);
            latchForRepeatMode.countDown();
        }

        @Override
        public void onShuffleModeChanged(int shuffleMode) {
            shuffleModeRef.set(shuffleMode);
            latchForShuffleMode.countDown();
        }
    };
    controllerCompat.registerCallback(callback, handler);
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackState(testState).setPlayWhenReady(testPlayWhenReady).setCurrentPosition(testCurrentPositionMs).setBufferedPosition(testBufferedPositionMs).setDuration(testDurationMs).setPlaybackParameters(playbackParameters).setTimeline(testTimeline).setPlaylistMetadata(testPlaylistMetadata).setCurrentMediaItemIndex(testItemIndex).setShuffleModeEnabled(testShuffleModeEnabled).setRepeatMode(testRepeatMode).build();
    session.setPlayer(playerConfig);
    assertThat(latchForPlaybackState.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(playbackStateRef.get().getBufferedPosition()).isEqualTo(testBufferedPositionMs);
    assertThat(playbackStateRef.get().getPosition()).isEqualTo(testCurrentPositionMs);
    assertThat(playbackStateRef.get().getPlaybackSpeed()).isWithin(EPSILON).of(playbackParameters.speed);
    assertThat(latchForMetadata.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(metadataRef.get().getString(METADATA_KEY_MEDIA_ID)).isEqualTo(testMediaItems.get(testItemIndex).mediaId);
    assertThat(metadataRef.get().getLong(METADATA_KEY_DURATION)).isEqualTo(testDurationMs);
    @PlaybackStateCompat.State int playbackStateFromControllerCompat = MediaUtils.convertToPlaybackState(playbackStateRef.get(), metadataRef.get(), /* timeDiffMs= */
    C.TIME_UNSET);
    assertThat(playbackStateFromControllerCompat).isEqualTo(testState);
    assertThat(metadataRef.get().getRating(METADATA_KEY_USER_RATING).hasHeart()).isTrue();
    assertThat(latchForQueue.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    List<QueueItem> queue = controllerCompat.getQueue();
    assertThat(queue).hasSize(testTimeline.getWindowCount());
    for (int i = 0; i < testTimeline.getWindowCount(); i++) {
        assertThat(queue.get(i).getDescription().getMediaId()).isEqualTo(testMediaItems.get(i).mediaId);
    }
    assertThat(queueTitleRef.get().toString()).isEqualTo(testPlaylistTitle);
    assertThat(latchForShuffleMode.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
    assertThat(shuffleModeRef.get()).isEqualTo(PlaybackStateCompat.SHUFFLE_MODE_ALL);
    assertThat(latchForRepeatMode.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
    assertThat(repeatModeRef.get()).isEqualTo(PlaybackStateCompat.REPEAT_MODE_ONE);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) RepeatMode(androidx.media3.common.Player.RepeatMode) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HeartRating(androidx.media3.common.HeartRating) Timeline(androidx.media3.common.Timeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) State(androidx.media3.common.Player.State) MediaItem(androidx.media3.common.MediaItem) MediaMetadata(androidx.media3.common.MediaMetadata) PlaybackParameters(androidx.media3.common.PlaybackParameters) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 29 with State

use of androidx.media3.common.Player.State in project media by androidx.

the class MediaControllerStateMaskingWithMediaSessionCompatTest method seekTo_withNewMediaItemIndex.

@Test
public void seekTo_withNewMediaItemIndex() throws Exception {
    List<MediaItem> mediaItems = MediaTestUtils.createMediaItems(3);
    List<QueueItem> queue = MediaUtils.convertToQueueItemList(mediaItems);
    long initialPosition = 8_000;
    long initialBufferedPosition = 9_200;
    int initialIndex = 0;
    long testPosition = 9_000;
    long testBufferedPosition = 0;
    int testIndex = 1;
    Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_POSITION_DISCONTINUITY, EVENT_PLAYBACK_STATE_CHANGED, EVENT_MEDIA_ITEM_TRANSITION).build());
    session.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PAUSED, initialPosition, /* playbackSpeed= */
    1.0f).setBufferedPosition(initialBufferedPosition).setActiveQueueItemId(queue.get(initialIndex).getQueueId()).build());
    session.setQueue(queue);
    MediaController controller = controllerTestRule.createController(session.getSessionToken());
    CountDownLatch latch = new CountDownLatch(4);
    AtomicReference<PositionInfo> oldPositionInfoRef = new AtomicReference<>();
    AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
    AtomicInteger discontinuityReasonRef = new AtomicInteger();
    AtomicInteger playbackStateRef = new AtomicInteger();
    AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
    AtomicInteger mediaItemTransitionReasonRef = new AtomicInteger();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @DiscontinuityReason int reason) {
            oldPositionInfoRef.set(oldPosition);
            newPositionInfoRef.set(newPosition);
            discontinuityReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onPlaybackStateChanged(@State int playbackState) {
            playbackStateRef.set(playbackState);
            latch.countDown();
        }

        @Override
        public void onMediaItemTransition(@Nullable MediaItem mediaItem, @MediaItemTransitionReason int reason) {
            mediaItemRef.set(mediaItem);
            mediaItemTransitionReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicLong currentPositionRef = new AtomicLong();
    AtomicLong bufferedPositionRef = new AtomicLong();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.seekTo(testIndex, testPosition);
        currentPositionRef.set(controller.getCurrentPosition());
        bufferedPositionRef.set(controller.getBufferedPosition());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(oldPositionInfoRef.get().positionMs).isEqualTo(initialPosition);
    assertThat(oldPositionInfoRef.get().mediaItemIndex).isEqualTo(initialIndex);
    assertThat(newPositionInfoRef.get().positionMs).isEqualTo(testPosition);
    assertThat(newPositionInfoRef.get().mediaItemIndex).isEqualTo(testIndex);
    assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_SEEK);
    assertThat(playbackStateRef.get()).isEqualTo(STATE_BUFFERING);
    assertThat(mediaItemRef.get()).isEqualTo(mediaItems.get(testIndex));
    assertThat(mediaItemTransitionReasonRef.get()).isEqualTo(MEDIA_ITEM_TRANSITION_REASON_SEEK);
    assertThat(onEventsRef.get()).isEqualTo(testEvents);
    assertThat(currentPositionRef.get()).isEqualTo(testPosition);
    assertThat(bufferedPositionRef.get()).isEqualTo(testBufferedPosition);
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Events(androidx.media3.common.Player.Events) PositionInfo(androidx.media3.common.Player.PositionInfo) QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) Player(androidx.media3.common.Player) DiscontinuityReason(androidx.media3.common.Player.DiscontinuityReason) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FlagSet(androidx.media3.common.FlagSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaItemTransitionReason(androidx.media3.common.Player.MediaItemTransitionReason) MediaItem(androidx.media3.common.MediaItem) State(androidx.media3.common.Player.State) Nullable(androidx.annotation.Nullable) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 30 with State

use of androidx.media3.common.Player.State in project media by androidx.

the class MediaControllerStateMaskingWithMediaSessionCompatTest method seekTo_seekBackwardWithinSameMediaItem_resetsBufferedPosition.

@Test
public void seekTo_seekBackwardWithinSameMediaItem_resetsBufferedPosition() throws Exception {
    long initialPosition = 8_000L;
    long initialBufferedPosition = 9_200L;
    int initialIndex = 0;
    long testPosition = 7_000L;
    long testBufferedPosition = testPosition;
    long testTotalBufferedDuration = testBufferedPosition - testPosition;
    Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_POSITION_DISCONTINUITY, EVENT_PLAYBACK_STATE_CHANGED).build());
    session.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PAUSED, initialPosition, /* playbackSpeed= */
    1.0f).setBufferedPosition(initialBufferedPosition).build());
    MediaController controller = controllerTestRule.createController(session.getSessionToken());
    CountDownLatch latch = new CountDownLatch(3);
    AtomicReference<PositionInfo> oldPositionInfoRef = new AtomicReference<>();
    AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
    AtomicInteger discontinuityReasonRef = new AtomicInteger();
    AtomicInteger playbackStateRef = new AtomicInteger();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @DiscontinuityReason int reason) {
            oldPositionInfoRef.set(oldPosition);
            newPositionInfoRef.set(newPosition);
            discontinuityReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onPlaybackStateChanged(@State int playbackState) {
            playbackStateRef.set(playbackState);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicLong currentPositionRef = new AtomicLong();
    AtomicLong bufferedPositionRef = new AtomicLong();
    AtomicLong totalBufferedDurationRef = new AtomicLong();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.seekTo(testPosition);
        currentPositionRef.set(controller.getCurrentPosition());
        bufferedPositionRef.set(controller.getBufferedPosition());
        totalBufferedDurationRef.set(controller.getTotalBufferedDuration());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(oldPositionInfoRef.get().positionMs).isEqualTo(initialPosition);
    assertThat(oldPositionInfoRef.get().mediaItemIndex).isEqualTo(initialIndex);
    assertThat(newPositionInfoRef.get().positionMs).isEqualTo(testPosition);
    assertThat(newPositionInfoRef.get().mediaItemIndex).isEqualTo(initialIndex);
    assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_SEEK);
    assertThat(playbackStateRef.get()).isEqualTo(STATE_BUFFERING);
    assertThat(onEventsRef.get()).isEqualTo(testEvents);
    assertThat(currentPositionRef.get()).isEqualTo(testPosition);
    assertThat(bufferedPositionRef.get()).isEqualTo(testBufferedPosition);
    assertThat(totalBufferedDurationRef.get()).isEqualTo(testTotalBufferedDuration);
}
Also used : Player(androidx.media3.common.Player) DiscontinuityReason(androidx.media3.common.Player.DiscontinuityReason) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FlagSet(androidx.media3.common.FlagSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) Events(androidx.media3.common.Player.Events) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) State(androidx.media3.common.Player.State) PositionInfo(androidx.media3.common.Player.PositionInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)41 Timeline (androidx.media3.common.Timeline)17 Player (androidx.media3.common.Player)15 Nullable (androidx.annotation.Nullable)14 AdPlaybackState (androidx.media3.common.AdPlaybackState)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 MediaItem (androidx.media3.common.MediaItem)12 LargeTest (androidx.test.filters.LargeTest)12 State (androidx.media3.common.Player.State)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 PositionInfo (androidx.media3.common.Player.PositionInfo)10 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)10 ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState (androidx.media3.exoplayer.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState)9 ActionSchedule (androidx.media3.test.utils.ActionSchedule)9 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)8 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)8 PlayerRunnable (androidx.media3.test.utils.ActionSchedule.PlayerRunnable)8 FakeTimeline (androidx.media3.test.utils.FakeTimeline)8 NoUidTimeline (androidx.media3.test.utils.NoUidTimeline)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8