Search in sources :

Example 16 with AudioAttributes

use of androidx.media3.common.AudioAttributes 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 17 with AudioAttributes

use of androidx.media3.common.AudioAttributes in project media by androidx.

the class MediaControllerListenerTest method onAudioAttributesChanged_isCalledAndUpdatesGetter.

@Test
public void onAudioAttributesChanged_isCalledAndUpdatesGetter() throws Exception {
    AudioAttributes testAttributes = new AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).setContentType(C.CONTENT_TYPE_MOVIE).build();
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<AudioAttributes> attributesRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onAudioAttributesChanged(AudioAttributes attributes) {
            if (testAttributes.equals(attributes)) {
                attributesRef.set(controller.getAudioAttributes());
                latch.countDown();
            }
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    remoteSession.getMockPlayer().notifyAudioAttributesChanged(testAttributes);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(attributesRef.get()).isEqualTo(testAttributes);
}
Also used : RemoteMockPlayer(androidx.media3.session.RemoteMediaSession.RemoteMockPlayer) Player(androidx.media3.common.Player) AudioAttributes(androidx.media3.common.AudioAttributes) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 18 with AudioAttributes

use of androidx.media3.common.AudioAttributes in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method setPlayer_playbackTypeChangedToLocal.

@Test
public void setPlayer_playbackTypeChangedToLocal() throws Exception {
    DeviceInfo deviceInfo = new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_REMOTE, /* minVolume= */
    0, /* maxVolume= */
    10);
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceInfo(deviceInfo).build();
    session.setPlayer(playerConfig);
    DeviceInfo deviceInfoToUpdate = new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_LOCAL, /* minVolume= */
    0, /* maxVolume= */
    10);
    int legacyPlaybackTypeToUpdate = MediaControllerCompat.PlaybackInfo.PLAYBACK_TYPE_LOCAL;
    int legacyStream = AudioManager.STREAM_RING;
    AudioAttributesCompat attrsCompat = new AudioAttributesCompat.Builder().setLegacyStreamType(legacyStream).build();
    AudioAttributes attrs = MediaUtils.convertToAudioAttributes(attrsCompat);
    CountDownLatch playbackInfoNotified = new CountDownLatch(1);
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

        @Override
        public void onAudioInfoChanged(MediaControllerCompat.PlaybackInfo info) {
            if (info.getPlaybackType() == legacyPlaybackTypeToUpdate && info.getAudioAttributes().getLegacyStreamType() == legacyStream) {
                playbackInfoNotified.countDown();
            }
        }
    };
    controllerCompat.registerCallback(callback, handler);
    Bundle playerConfigToUpdate = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceInfo(deviceInfoToUpdate).setAudioAttributes(attrs).build();
    session.setPlayer(playerConfigToUpdate);
    // In API 21 and 22, onAudioInfoChanged is not called when playback is changed to local.
    if (Util.SDK_INT == 21 || Util.SDK_INT == 22) {
        PollingCheck.waitFor(TIMEOUT_MS, () -> {
            MediaControllerCompat.PlaybackInfo info = controllerCompat.getPlaybackInfo();
            return info.getPlaybackType() == legacyPlaybackTypeToUpdate && info.getAudioAttributes().getLegacyStreamType() == legacyStream;
        });
    } else {
        assertThat(playbackInfoNotified.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
        MediaControllerCompat.PlaybackInfo info = controllerCompat.getPlaybackInfo();
        assertThat(info.getPlaybackType()).isEqualTo(legacyPlaybackTypeToUpdate);
        assertThat(info.getAudioAttributes().getLegacyStreamType()).isEqualTo(legacyStream);
    }
}
Also used : Bundle(android.os.Bundle) AudioAttributesCompat(androidx.media.AudioAttributesCompat) AudioAttributes(androidx.media3.common.AudioAttributes) CountDownLatch(java.util.concurrent.CountDownLatch) DeviceInfo(androidx.media3.common.DeviceInfo) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

AudioAttributes (androidx.media3.common.AudioAttributes)15 Test (org.junit.Test)11 LargeTest (androidx.test.filters.LargeTest)7 AudioAttributesCompat (androidx.media.AudioAttributesCompat)6 Bundle (android.os.Bundle)5 DeviceInfo (androidx.media3.common.DeviceInfo)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 Nullable (androidx.annotation.Nullable)3 MediaMetadata (androidx.media3.common.MediaMetadata)3 PositionInfo (androidx.media3.common.Player.PositionInfo)3 State (androidx.media3.common.Player.State)3 AuxEffectInfo (androidx.media3.common.AuxEffectInfo)2 MediaItem (androidx.media3.common.MediaItem)2 PlaybackException (androidx.media3.common.PlaybackException)2 PlaybackParameters (androidx.media3.common.PlaybackParameters)2 RepeatMode (androidx.media3.common.Player.RepeatMode)2 Timeline (androidx.media3.common.Timeline)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SuppressLint (android.annotation.SuppressLint)1