Search in sources :

Example 1 with HeartRating

use of androidx.media3.common.HeartRating 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 2 with HeartRating

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

the class MediaUtilsTest method assertRatingEquals.

void assertRatingEquals(Rating rating, RatingCompat ratingCompat) {
    if (rating == null && ratingCompat == null) {
        return;
    }
    assertThat(rating.isRated()).isEqualTo(ratingCompat.isRated());
    if (rating instanceof HeartRating) {
        assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_HEART);
        assertThat(((HeartRating) rating).isHeart()).isEqualTo(ratingCompat.hasHeart());
    } else if (rating instanceof ThumbRating) {
        assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_THUMB_UP_DOWN);
        assertThat(((ThumbRating) rating).isThumbsUp()).isEqualTo(ratingCompat.isThumbUp());
    } else if (rating instanceof StarRating) {
        StarRating starRating = (StarRating) rating;
        switch(starRating.getMaxStars()) {
            case 3:
                assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_3_STARS);
                break;
            case 4:
                assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_4_STARS);
                break;
            case 5:
                assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_5_STARS);
                break;
            // fall out
            default:
        }
        assertThat(starRating.getStarRating()).isEqualTo(ratingCompat.getStarRating());
    } else if (rating instanceof PercentageRating) {
        assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_PERCENTAGE);
        assertThat(((PercentageRating) rating).getPercent()).isEqualTo(ratingCompat.getPercentRating());
    }
}
Also used : ThumbRating(androidx.media3.common.ThumbRating) PercentageRating(androidx.media3.common.PercentageRating) StarRating(androidx.media3.common.StarRating) HeartRating(androidx.media3.common.HeartRating)

Example 3 with HeartRating

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

the class MediaControllerCompatCallbackWithMediaSessionTest method gettersAfterConnected.

@Test
public void gettersAfterConnected() throws Exception {
    @State int testState = STATE_READY;
    int testBufferingPosition = 1500;
    float testSpeed = 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;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackState(testState).setBufferedPosition(testBufferingPosition).setPlaybackParameters(new PlaybackParameters(testSpeed)).setTimeline(testTimeline).setPlaylistMetadata(testPlaylistMetadata).setCurrentMediaItemIndex(testItemIndex).setShuffleModeEnabled(testShuffleModeEnabled).setRepeatMode(testRepeatMode).build();
    session.setPlayer(playerConfig);
    MediaControllerCompat controller = new MediaControllerCompat(context, session.getCompatToken());
    CountDownLatch latch = new CountDownLatch(1);
    controller.registerCallback(new MediaControllerCompat.Callback() {

        @Override
        public void onSessionReady() {
            latch.countDown();
        }
    }, handler);
    assertThat(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
    assertThat(MediaUtils.convertToPlaybackState(controller.getPlaybackState(), controller.getMetadata(), /* timeDiffMs= */
    C.TIME_UNSET)).isEqualTo(testState);
    assertThat(controller.getPlaybackState().getBufferedPosition()).isEqualTo(testBufferingPosition);
    assertThat(controller.getPlaybackState().getPlaybackSpeed()).isWithin(EPSILON).of(testSpeed);
    assertThat(controller.getMetadata().getString(METADATA_KEY_MEDIA_ID)).isEqualTo(testMediaItems.get(testItemIndex).mediaId);
    assertThat(controller.getRatingType()).isEqualTo(RatingCompat.RATING_HEART);
    List<QueueItem> queue = controller.getQueue();
    assertThat(queue).isNotNull();
    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(testPlaylistTitle).isEqualTo(controller.getQueueTitle().toString());
    assertThat(PlaybackStateCompat.SHUFFLE_MODE_ALL).isEqualTo(controller.getShuffleMode());
    assertThat(PlaybackStateCompat.REPEAT_MODE_ONE).isEqualTo(controller.getRepeatMode());
}
Also used : Bundle(android.os.Bundle) CountDownLatch(java.util.concurrent.CountDownLatch) HeartRating(androidx.media3.common.HeartRating) Timeline(androidx.media3.common.Timeline) RepeatMode(androidx.media3.common.Player.RepeatMode) State(androidx.media3.common.Player.State) MediaItem(androidx.media3.common.MediaItem) MediaMetadata(androidx.media3.common.MediaMetadata) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) PlaybackParameters(androidx.media3.common.PlaybackParameters) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 4 with HeartRating

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

the class MediaControllerTest method futuresCompleted_AvailableCommandsChange.

@Test
public void futuresCompleted_AvailableCommandsChange() throws Exception {
    RemoteMediaSession session = remoteSession;
    MediaController controller = controllerTestRule.createController(session.getToken());
    SessionCommands.Builder builder = new SessionCommands.Builder();
    SessionCommand setRatingCommand = new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_SET_RATING);
    SessionCommand customCommand = new SessionCommand("custom", /* extras= */
    Bundle.EMPTY);
    int trials = 100;
    CountDownLatch latch = new CountDownLatch(trials * 2);
    for (int trial = 0; trial < trials; trial++) {
        if (trial % 2 == 0) {
            builder.add(setRatingCommand);
            builder.add(customCommand);
        } else {
            builder.remove(setRatingCommand);
            builder.remove(customCommand);
        }
        session.setAvailableCommands(builder.build(), Player.Commands.EMPTY);
        String testMediaId = "testMediaId";
        Rating testRating = new HeartRating(/* hasHeart= */
        true);
        threadTestRule.getHandler().postAndSync(() -> {
            controller.setRating(testMediaId, testRating).addListener(latch::countDown, Runnable::run);
            controller.sendCustomCommand(customCommand, /* args= */
            Bundle.EMPTY).addListener(latch::countDown, Runnable::run);
        });
    }
    assertWithMessage("All futures should be completed").that(latch.await(LONG_TIMEOUT_MS, MILLISECONDS)).isTrue();
}
Also used : HeartRating(androidx.media3.common.HeartRating) Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) CountDownLatch(java.util.concurrent.CountDownLatch) HeartRating(androidx.media3.common.HeartRating) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

HeartRating (androidx.media3.common.HeartRating)4 LargeTest (androidx.test.filters.LargeTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 Bundle (android.os.Bundle)2 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)2 QueueItem (android.support.v4.media.session.MediaSessionCompat.QueueItem)2 MediaItem (androidx.media3.common.MediaItem)2 MediaMetadata (androidx.media3.common.MediaMetadata)2 PlaybackParameters (androidx.media3.common.PlaybackParameters)2 RepeatMode (androidx.media3.common.Player.RepeatMode)2 State (androidx.media3.common.Player.State)2 StarRating (androidx.media3.common.StarRating)2 Timeline (androidx.media3.common.Timeline)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)1 PercentageRating (androidx.media3.common.PercentageRating)1 Rating (androidx.media3.common.Rating)1 ThumbRating (androidx.media3.common.ThumbRating)1 ImmutableList (com.google.common.collect.ImmutableList)1