Search in sources :

Example 11 with Callback

use of androidx.media3.exoplayer.offline.DownloadHelper.Callback in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method setPlayer_playbackTypeNotChanged_remote.

@Test
public void setPlayer_playbackTypeNotChanged_remote() throws Exception {
    DeviceInfo deviceInfo = new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_REMOTE, /* minVolume= */
    0, /* maxVolume= */
    10);
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceInfo(deviceInfo).setDeviceVolume(1).build();
    session.setPlayer(playerConfig);
    DeviceInfo deviceInfoToUpdate = new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_REMOTE, /* minVolume= */
    0, /* maxVolume= */
    25);
    int legacyPlaybackTypeToUpdate = MediaControllerCompat.PlaybackInfo.PLAYBACK_TYPE_REMOTE;
    int deviceVolumeToUpdate = 10;
    CountDownLatch playbackInfoNotified = new CountDownLatch(1);
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

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

Example 12 with Callback

use of androidx.media3.exoplayer.offline.DownloadHelper.Callback 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 13 with Callback

use of androidx.media3.exoplayer.offline.DownloadHelper.Callback 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 14 with Callback

use of androidx.media3.exoplayer.offline.DownloadHelper.Callback 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 15 with Callback

use of androidx.media3.exoplayer.offline.DownloadHelper.Callback in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method playlistMetadataChange.

@Test
public void playlistMetadataChange() throws Exception {
    AtomicReference<CharSequence> queueTitleRef = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

        @Override
        public void onQueueTitleChanged(CharSequence title) {
            queueTitleRef.set(title);
            latch.countDown();
        }
    };
    controllerCompat.registerCallback(callback, handler);
    String playlistTitle = "playlistTitle";
    MediaMetadata playlistMetadata = new MediaMetadata.Builder().setTitle(playlistTitle).build();
    session.getMockPlayer().setPlaylistMetadata(playlistMetadata);
    session.getMockPlayer().notifyPlaylistMetadataChanged();
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(queueTitleRef.get().toString()).isEqualTo(playlistTitle);
}
Also used : MediaMetadata(androidx.media3.common.MediaMetadata) 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)

Aggregations

Test (org.junit.Test)55 LargeTest (androidx.test.filters.LargeTest)33 CountDownLatch (java.util.concurrent.CountDownLatch)30 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 Bundle (android.os.Bundle)15 ControllerInfo (androidx.media3.session.MediaSession.ControllerInfo)15 MediaItem (androidx.media3.common.MediaItem)14 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)12 Nullable (androidx.annotation.Nullable)10 PlaybackParameters (androidx.media3.common.PlaybackParameters)10 Player (androidx.media3.common.Player)10 ArrayList (java.util.ArrayList)10 HandlerThread (android.os.HandlerThread)9 HandlerWrapper (androidx.media3.common.util.HandlerWrapper)9 SessionCallback (androidx.media3.session.MediaSession.SessionCallback)9 FakeTimeline (androidx.media3.test.utils.FakeTimeline)9 Uri (android.net.Uri)8 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)8 ImmutableList (com.google.common.collect.ImmutableList)7 Timeline (androidx.media3.common.Timeline)6