Search in sources :

Example 21 with RepeatMode

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

the class MediaControllerTest method gettersAfterConnected.

@Test
public void gettersAfterConnected() throws Exception {
    long currentPositionMs = 11;
    long contentPositionMs = 33;
    long durationMs = 200;
    long bufferedPositionMs = 100;
    int bufferedPercentage = 50;
    long totalBufferedDurationMs = 120;
    long currentLiveOffsetMs = 10;
    long contentDurationMs = 300;
    long contentBufferedPositionMs = 240;
    boolean isPlayingAd = true;
    int currentAdGroupIndex = 33;
    int currentAdIndexInAdGroup = 22;
    PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
    0.5f);
    boolean playWhenReady = true;
    @Player.PlaybackSuppressionReason int playbackSuppressionReason = Player.PLAYBACK_SUPPRESSION_REASON_TRANSIENT_AUDIO_FOCUS_LOSS;
    @Player.State int playbackState = Player.STATE_READY;
    boolean isPlaying = true;
    boolean isLoading = true;
    boolean isShuffleModeEnabled = true;
    @RepeatMode int repeatMode = Player.REPEAT_MODE_ONE;
    long seekBackIncrementMs = 1_000;
    long seekForwardIncrementMs = 2_000;
    long maxSeekToPreviousPositionMs = 300;
    TrackSelectionParameters trackSelectionParameters = TrackSelectionParameters.DEFAULT_WITHOUT_CONTEXT.buildUpon().setMaxVideoSizeSd().build();
    Timeline timeline = MediaTestUtils.createTimeline(5);
    int currentMediaItemIndex = 3;
    MediaItem currentMediaItem = timeline.getWindow(currentMediaItemIndex, new Timeline.Window()).mediaItem;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setCurrentPosition(currentPositionMs).setContentPosition(contentPositionMs).setDuration(durationMs).setBufferedPosition(bufferedPositionMs).setBufferedPercentage(bufferedPercentage).setTotalBufferedDuration(totalBufferedDurationMs).setCurrentLiveOffset(currentLiveOffsetMs).setContentDuration(contentDurationMs).setContentBufferedPosition(contentBufferedPositionMs).setIsPlayingAd(isPlayingAd).setCurrentAdGroupIndex(currentAdGroupIndex).setCurrentAdIndexInAdGroup(currentAdIndexInAdGroup).setPlaybackParameters(playbackParameters).setPlayWhenReady(playWhenReady).setPlaybackSuppressionReason(playbackSuppressionReason).setPlaybackState(playbackState).setIsPlaying(isPlaying).setIsLoading(isLoading).setShuffleModeEnabled(isShuffleModeEnabled).setRepeatMode(repeatMode).setSeekBackIncrement(seekBackIncrementMs).setSeekForwardIncrement(seekForwardIncrementMs).setMaxSeekToPreviousPositionMs(maxSeekToPreviousPositionMs).setTrackSelectionParameters(trackSelectionParameters).setTimeline(timeline).setCurrentMediaItemIndex(currentMediaItemIndex).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    threadTestRule.getHandler().postAndSync(() -> controller.setTimeDiffMs(0L));
    AtomicLong currentPositionMsRef = new AtomicLong();
    AtomicLong contentPositionMsRef = new AtomicLong();
    AtomicLong durationMsRef = new AtomicLong();
    AtomicLong bufferedPositionMsRef = new AtomicLong();
    AtomicInteger bufferedPercentageRef = new AtomicInteger();
    AtomicLong totalBufferedDurationMsRef = new AtomicLong();
    AtomicLong currentLiveOffsetMsRef = new AtomicLong();
    AtomicLong contentDurationMsRef = new AtomicLong();
    AtomicLong contentBufferedPositionMsRef = new AtomicLong();
    AtomicBoolean isPlayingAdRef = new AtomicBoolean();
    AtomicInteger currentAdGroupIndexRef = new AtomicInteger();
    AtomicInteger currentAdIndexInAdGroupRef = new AtomicInteger();
    AtomicReference<PlaybackParameters> playbackParametersRef = new AtomicReference<>();
    AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
    AtomicBoolean playWhenReadyRef = new AtomicBoolean();
    AtomicInteger playbackSuppressionReasonRef = new AtomicInteger();
    AtomicInteger playbackStateRef = new AtomicInteger();
    AtomicBoolean isPlayingRef = new AtomicBoolean();
    AtomicBoolean isLoadingRef = new AtomicBoolean();
    AtomicBoolean isShuffleModeEnabledRef = new AtomicBoolean();
    AtomicInteger repeatModeRef = new AtomicInteger();
    AtomicLong seekBackIncrementRef = new AtomicLong();
    AtomicLong seekForwardIncrementRef = new AtomicLong();
    AtomicLong maxSeekToPreviousPositionMsRef = new AtomicLong();
    AtomicReference<TrackSelectionParameters> trackSelectionParametersRef = new AtomicReference<>();
    AtomicReference<Timeline> timelineRef = new AtomicReference<>();
    AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
    AtomicReference<MediaItem> currentMediaItemRef = new AtomicReference<>();
    threadTestRule.getHandler().postAndSync(() -> {
        currentPositionMsRef.set(controller.getCurrentPosition());
        contentPositionMsRef.set(controller.getContentPosition());
        durationMsRef.set(controller.getDuration());
        bufferedPositionMsRef.set(controller.getBufferedPosition());
        bufferedPercentageRef.set(controller.getBufferedPercentage());
        totalBufferedDurationMsRef.set(controller.getTotalBufferedDuration());
        currentLiveOffsetMsRef.set(controller.getCurrentLiveOffset());
        contentDurationMsRef.set(controller.getContentDuration());
        contentBufferedPositionMsRef.set(controller.getContentBufferedPosition());
        playbackParametersRef.set(controller.getPlaybackParameters());
        isPlayingAdRef.set(controller.isPlayingAd());
        currentAdGroupIndexRef.set(controller.getCurrentAdGroupIndex());
        currentAdIndexInAdGroupRef.set(controller.getCurrentAdIndexInAdGroup());
        mediaItemRef.set(controller.getCurrentMediaItem());
        playWhenReadyRef.set(controller.getPlayWhenReady());
        playbackSuppressionReasonRef.set(controller.getPlaybackSuppressionReason());
        playbackStateRef.set(controller.getPlaybackState());
        isPlayingRef.set(controller.isPlaying());
        isLoadingRef.set(controller.isLoading());
        isShuffleModeEnabledRef.set(controller.getShuffleModeEnabled());
        repeatModeRef.set(controller.getRepeatMode());
        seekBackIncrementRef.set(controller.getSeekBackIncrement());
        seekForwardIncrementRef.set(controller.getSeekForwardIncrement());
        maxSeekToPreviousPositionMsRef.set(controller.getMaxSeekToPreviousPosition());
        trackSelectionParametersRef.set(controller.getTrackSelectionParameters());
        timelineRef.set(controller.getCurrentTimeline());
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
        currentMediaItemRef.set(controller.getCurrentMediaItem());
    });
    assertThat(currentPositionMsRef.get()).isEqualTo(currentPositionMs);
    assertThat(contentPositionMsRef.get()).isEqualTo(contentPositionMs);
    assertThat(durationMsRef.get()).isEqualTo(durationMs);
    assertThat(bufferedPositionMsRef.get()).isEqualTo(bufferedPositionMs);
    assertThat(bufferedPercentageRef.get()).isEqualTo(bufferedPercentage);
    assertThat(totalBufferedDurationMsRef.get()).isEqualTo(totalBufferedDurationMs);
    assertThat(currentLiveOffsetMsRef.get()).isEqualTo(currentLiveOffsetMs);
    assertThat(contentDurationMsRef.get()).isEqualTo(contentDurationMs);
    assertThat(contentBufferedPositionMsRef.get()).isEqualTo(contentBufferedPositionMs);
    assertThat(playbackParametersRef.get()).isEqualTo(playbackParameters);
    assertThat(isPlayingAdRef.get()).isEqualTo(isPlayingAd);
    assertThat(currentAdGroupIndexRef.get()).isEqualTo(currentAdGroupIndex);
    assertThat(currentAdIndexInAdGroupRef.get()).isEqualTo(currentAdIndexInAdGroup);
    assertThat(playWhenReadyRef.get()).isEqualTo(playWhenReady);
    assertThat(playbackSuppressionReasonRef.get()).isEqualTo(playbackSuppressionReason);
    assertThat(playbackStateRef.get()).isEqualTo(playbackState);
    assertThat(isPlayingRef.get()).isEqualTo(isPlaying);
    assertThat(isLoadingRef.get()).isEqualTo(isLoading);
    assertThat(isShuffleModeEnabledRef.get()).isEqualTo(isShuffleModeEnabled);
    assertThat(repeatModeRef.get()).isEqualTo(repeatMode);
    assertThat(seekBackIncrementRef.get()).isEqualTo(seekBackIncrementMs);
    assertThat(seekForwardIncrementRef.get()).isEqualTo(seekForwardIncrementMs);
    assertThat(maxSeekToPreviousPositionMsRef.get()).isEqualTo(maxSeekToPreviousPositionMs);
    assertThat(trackSelectionParametersRef.get()).isEqualTo(trackSelectionParameters);
    assertTimelineMediaItemsEquals(timelineRef.get(), timeline);
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(currentMediaItemIndex);
    assertThat(currentMediaItemRef.get()).isEqualTo(currentMediaItem);
}
Also used : Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RepeatMode(androidx.media3.common.Player.RepeatMode) Timeline(androidx.media3.common.Timeline) AtomicLong(java.util.concurrent.atomic.AtomicLong) TrackSelectionParameters(androidx.media3.common.TrackSelectionParameters) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaItem(androidx.media3.common.MediaItem) PlaybackParameters(androidx.media3.common.PlaybackParameters) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 22 with RepeatMode

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

the class MediaControllerListenerTest method setPlayer_notifiesChangedValues.

@Test
public void setPlayer_notifiesChangedValues() throws Exception {
    @State int testState = STATE_BUFFERING;
    Timeline testTimeline = MediaTestUtils.createTimeline(/* windowCount= */
    3);
    MediaMetadata testPlaylistMetadata = new MediaMetadata.Builder().setTitle("title").build();
    AudioAttributes testAudioAttributes = MediaUtils.convertToAudioAttributes(new AudioAttributesCompat.Builder().setLegacyStreamType(AudioManager.STREAM_RING).build());
    boolean testShuffleModeEnabled = true;
    @RepeatMode int testRepeatMode = REPEAT_MODE_ALL;
    int testCurrentAdGroupIndex = 33;
    int testCurrentAdIndexInAdGroup = 11;
    AtomicInteger stateRef = new AtomicInteger();
    AtomicReference<Timeline> timelineRef = new AtomicReference<>();
    AtomicReference<MediaMetadata> playlistMetadataRef = new AtomicReference<>();
    AtomicReference<AudioAttributes> audioAttributesRef = new AtomicReference<>();
    AtomicInteger currentAdGroupIndexRef = new AtomicInteger();
    AtomicInteger currentAdIndexInAdGroupRef = new AtomicInteger();
    AtomicBoolean shuffleModeEnabledRef = new AtomicBoolean();
    AtomicInteger repeatModeRef = new AtomicInteger();
    CountDownLatch latch = new CountDownLatch(7);
    controller = controllerTestRule.createController(remoteSession.getToken());
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(new Player.Listener() {

        @Override
        public void onAudioAttributesChanged(AudioAttributes attributes) {
            audioAttributesRef.set(attributes);
            latch.countDown();
        }

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

        @Override
        public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
            timelineRef.set(timeline);
            latch.countDown();
        }

        @Override
        public void onPlaylistMetadataChanged(MediaMetadata playlistMetadata) {
            playlistMetadataRef.set(playlistMetadata);
            latch.countDown();
        }

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @DiscontinuityReason int reason) {
            currentAdGroupIndexRef.set(newPosition.adGroupIndex);
            currentAdIndexInAdGroupRef.set(newPosition.adIndexInAdGroup);
            latch.countDown();
        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
            shuffleModeEnabledRef.set(shuffleModeEnabled);
            latch.countDown();
        }

        @Override
        public void onRepeatModeChanged(@RepeatMode int repeatMode) {
            repeatModeRef.set(repeatMode);
            latch.countDown();
        }
    }));
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackState(testState).setAudioAttributes(testAudioAttributes).setTimeline(testTimeline).setPlaylistMetadata(testPlaylistMetadata).setShuffleModeEnabled(testShuffleModeEnabled).setRepeatMode(testRepeatMode).setCurrentAdGroupIndex(testCurrentAdGroupIndex).setCurrentAdIndexInAdGroup(testCurrentAdIndexInAdGroup).build();
    remoteSession.setPlayer(playerConfig);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(stateRef.get()).isEqualTo(testState);
    MediaTestUtils.assertMediaIdEquals(testTimeline, timelineRef.get());
    assertThat(playlistMetadataRef.get()).isEqualTo(testPlaylistMetadata);
    assertThat(audioAttributesRef.get()).isEqualTo(testAudioAttributes);
    assertThat(currentAdGroupIndexRef.get()).isEqualTo(testCurrentAdGroupIndex);
    assertThat(currentAdIndexInAdGroupRef.get()).isEqualTo(testCurrentAdIndexInAdGroup);
    assertThat(shuffleModeEnabledRef.get()).isEqualTo(testShuffleModeEnabled);
    assertThat(repeatModeRef.get()).isEqualTo(testRepeatMode);
}
Also used : DiscontinuityReason(androidx.media3.common.Player.DiscontinuityReason) AudioAttributesCompat(androidx.media.AudioAttributesCompat) Bundle(android.os.Bundle) AudioAttributes(androidx.media3.common.AudioAttributes) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) RepeatMode(androidx.media3.common.Player.RepeatMode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) State(androidx.media3.common.Player.State) MediaMetadata(androidx.media3.common.MediaMetadata) PositionInfo(androidx.media3.common.Player.PositionInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 23 with RepeatMode

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

the class MediaControllerListenerTest method onEvents_whenNewCommandIsCalledInsideListener_containsEventFromNewCommand.

@Test
public void onEvents_whenNewCommandIsCalledInsideListener_containsEventFromNewCommand() throws Exception {
    Player.Events testEvents = new Player.Events(new FlagSet.Builder().addAll(EVENT_REPEAT_MODE_CHANGED, EVENT_SHUFFLE_MODE_ENABLED_CHANGED).build());
    List<Integer> listenerEventCodes = new ArrayList<>();
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(3);
    AtomicReference<Player.Events> eventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onRepeatModeChanged(@Player.RepeatMode int repeatMode) {
            controller.setShuffleModeEnabled(true);
            listenerEventCodes.add(EVENT_REPEAT_MODE_CHANGED);
            latch.countDown();
        }

        @Override
        public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
            listenerEventCodes.add(EVENT_SHUFFLE_MODE_ENABLED_CHANGED);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            listenerEventCodes.add(EVENT_ON_EVENTS);
            eventsRef.set(events);
            latch.countDown();
        }
    };
    controller.addListener(listener);
    remoteSession.getMockPlayer().setRepeatMode(REPEAT_MODE_ONE);
    remoteSession.getMockPlayer().notifyRepeatModeChanged();
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(listenerEventCodes).containsExactly(EVENT_REPEAT_MODE_CHANGED, EVENT_SHUFFLE_MODE_ENABLED_CHANGED, EVENT_ON_EVENTS);
    assertThat(eventsRef.get()).isEqualTo(testEvents);
}
Also used : RemoteMockPlayer(androidx.media3.session.RemoteMediaSession.RemoteMockPlayer) Player(androidx.media3.common.Player) RepeatMode(androidx.media3.common.Player.RepeatMode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FlagSet(androidx.media3.common.FlagSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 24 with RepeatMode

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

the class MediaControllerListenerTest method onEvents_whenOnRepeatModeChanges_isCalledAfterOtherMethods.

@Test
public void onEvents_whenOnRepeatModeChanges_isCalledAfterOtherMethods() throws Exception {
    Player.Events testEvents = new Player.Events(new FlagSet.Builder().add(EVENT_REPEAT_MODE_CHANGED).build());
    List<Integer> listenerEventCodes = new ArrayList<>();
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<Player.Events> eventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onRepeatModeChanged(@Player.RepeatMode int repeatMode) {
            listenerEventCodes.add(EVENT_REPEAT_MODE_CHANGED);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            listenerEventCodes.add(EVENT_ON_EVENTS);
            eventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    remoteSession.getMockPlayer().setRepeatMode(REPEAT_MODE_ONE);
    remoteSession.getMockPlayer().notifyRepeatModeChanged();
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(listenerEventCodes).containsExactly(EVENT_REPEAT_MODE_CHANGED, EVENT_ON_EVENTS);
    assertThat(eventsRef.get()).isEqualTo(testEvents);
}
Also used : RemoteMockPlayer(androidx.media3.session.RemoteMediaSession.RemoteMockPlayer) Player(androidx.media3.common.Player) RepeatMode(androidx.media3.common.Player.RepeatMode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FlagSet(androidx.media3.common.FlagSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 25 with RepeatMode

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

the class MediaControllerStateMaskingWithMediaSessionCompatTest method setRepeatMode.

@Test
public void setRepeatMode() throws Exception {
    int testRepeatMode = REPEAT_MODE_ALL;
    Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_REPEAT_MODE_CHANGED).build());
    MediaController controller = controllerTestRule.createController(session.getSessionToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicInteger repeatModeFromCallbackRef = new AtomicInteger();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            repeatModeFromCallbackRef.set(repeatMode);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger repeatModeFromGetterRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.setRepeatMode(testRepeatMode);
        repeatModeFromGetterRef.set(controller.getRepeatMode());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(repeatModeFromCallbackRef.get()).isEqualTo(testRepeatMode);
    assertThat(onEventsRef.get()).isEqualTo(testEvents);
    assertThat(repeatModeFromGetterRef.get()).isEqualTo(testRepeatMode);
}
Also used : FlagSet(androidx.media3.common.FlagSet) Player(androidx.media3.common.Player) Events(androidx.media3.common.Player.Events) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 LargeTest (androidx.test.filters.LargeTest)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 RepeatMode (androidx.media3.common.Player.RepeatMode)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 Player (androidx.media3.common.Player)9 Timeline (androidx.media3.common.Timeline)9 Nullable (androidx.annotation.Nullable)7 MediaMetadata (androidx.media3.common.MediaMetadata)7 Bundle (android.os.Bundle)6 MediaItem (androidx.media3.common.MediaItem)6 PlaybackParameters (androidx.media3.common.PlaybackParameters)6 FlagSet (androidx.media3.common.FlagSet)5 State (androidx.media3.common.Player.State)5 MediaPeriodId (androidx.media3.exoplayer.source.MediaSource.MediaPeriodId)4 RemoteMockPlayer (androidx.media3.session.RemoteMediaSession.RemoteMockPlayer)4 AudioAttributes (androidx.media3.common.AudioAttributes)3 PositionInfo (androidx.media3.common.Player.PositionInfo)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3