Search in sources :

Example 6 with Listener

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

the class IcyDataSource method readMetadata.

/**
 * Reads an ICY stream metadata block, passing it to {@link #listener} unless the block is empty.
 *
 * @return True if the block was extracted, including if its length byte indicated a length of
 *     zero. False if the end of the stream was reached.
 * @throws IOException If an error occurs reading from the wrapped {@link DataSource}.
 */
private boolean readMetadata() throws IOException {
    int bytesRead = upstream.read(metadataLengthByteHolder, 0, 1);
    if (bytesRead == C.RESULT_END_OF_INPUT) {
        return false;
    }
    int metadataLength = (metadataLengthByteHolder[0] & 0xFF) << 4;
    if (metadataLength == 0) {
        return true;
    }
    int offset = 0;
    int lengthRemaining = metadataLength;
    byte[] metadata = new byte[metadataLength];
    while (lengthRemaining > 0) {
        bytesRead = upstream.read(metadata, offset, lengthRemaining);
        if (bytesRead == C.RESULT_END_OF_INPUT) {
            return false;
        }
        offset += bytesRead;
        lengthRemaining -= bytesRead;
    }
    // Discard trailing zero bytes.
    while (metadataLength > 0 && metadata[metadataLength - 1] == 0) {
        metadataLength--;
    }
    if (metadataLength > 0) {
        listener.onIcyMetadata(new ParsableByteArray(metadata, metadataLength));
    }
    return true;
}
Also used : ParsableByteArray(androidx.media3.common.util.ParsableByteArray)

Example 7 with Listener

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

the class ExoPlayerTest method setUnsupportedPlaybackSpeedConsecutivelyNotifiesListenerForEveryChangeOnceAndResetsOnceHandled.

@Test
public void setUnsupportedPlaybackSpeedConsecutivelyNotifiesListenerForEveryChangeOnceAndResetsOnceHandled() throws Exception {
    Renderer renderer = new FakeMediaClockRenderer(C.TRACK_TYPE_AUDIO) {

        @Override
        public long getPositionUs() {
            return 0;
        }

        @Override
        public void setPlaybackParameters(PlaybackParameters playbackParameters) {
        }

        @Override
        public PlaybackParameters getPlaybackParameters() {
            return PlaybackParameters.DEFAULT;
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.1f)).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.2f)).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.3f)).play().build();
    List<PlaybackParameters> reportedPlaybackParameters = new ArrayList<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
            reportedPlaybackParameters.add(playbackParameters);
        }
    };
    new ExoPlayerTestRunner.Builder(context).setSupportedFormats(ExoPlayerTestRunner.AUDIO_FORMAT).setRenderers(renderer).setActionSchedule(actionSchedule).setPlayerListener(listener).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(reportedPlaybackParameters).containsExactly(new PlaybackParameters(/* speed= */
    1.1f), new PlaybackParameters(/* speed= */
    1.2f), new PlaybackParameters(/* speed= */
    1.3f), PlaybackParameters.DEFAULT).inOrder();
}
Also used : Listener(androidx.media3.common.Player.Listener) Player(androidx.media3.common.Player) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) ActionSchedule(androidx.media3.test.utils.ActionSchedule) FakeRenderer(androidx.media3.test.utils.FakeRenderer) FakeVideoRenderer(androidx.media3.test.utils.FakeVideoRenderer) FakeMediaClockRenderer(androidx.media3.test.utils.FakeMediaClockRenderer) ArrayList(java.util.ArrayList) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) FakeMediaClockRenderer(androidx.media3.test.utils.FakeMediaClockRenderer) PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Example 8 with Listener

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

the class ExoPlayerTest method seekTo_toSAIMidRolls_playsMidRolls.

@Test
public void seekTo_toSAIMidRolls_playsMidRolls() throws Exception {
    ArgumentCaptor<PositionInfo> oldPositionArgumentCaptor = ArgumentCaptor.forClass(PositionInfo.class);
    ArgumentCaptor<PositionInfo> newPositionArgumentCaptor = ArgumentCaptor.forClass(PositionInfo.class);
    ArgumentCaptor<Integer> reasonArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
    FakeTimeline adTimeline = FakeTimeline.createMultiPeriodAdTimeline("windowId", /* numberOfPlayedAds= */
    0, /* isAdPeriodFlags...= */
    false, true, true, false);
    Listener listener = mock(Listener.class);
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    player.addListener(listener);
    AtomicReference<ServerSideAdInsertionMediaSource> sourceReference = new AtomicReference<>();
    sourceReference.set(new ServerSideAdInsertionMediaSource(new FakeMediaSource(adTimeline), contentTimeline -> {
        sourceReference.get().setAdPlaybackStates(adTimeline.getAdPlaybackStates(/* windowIndex= */
        0));
        return true;
    }));
    player.setMediaSource(sourceReference.get());
    player.pause();
    player.prepare();
    runUntilPlaybackState(player, Player.STATE_READY);
    player.seekTo(2500);
    player.play();
    runUntilPlaybackState(player, Player.STATE_ENDED);
    player.release();
    verify(listener, times(6)).onPositionDiscontinuity(oldPositionArgumentCaptor.capture(), newPositionArgumentCaptor.capture(), reasonArgumentCaptor.capture());
    assertThat(reasonArgumentCaptor.getAllValues()).containsExactly(1, 2, 0, 0, 0, 0).inOrder();
    List<PositionInfo> oldPositions = oldPositionArgumentCaptor.getAllValues();
    List<PositionInfo> newPositions = newPositionArgumentCaptor.getAllValues();
    // seek discontinuity
    assertThat(oldPositions.get(0).periodIndex).isEqualTo(0);
    assertThat(oldPositions.get(0).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(0).periodIndex).isEqualTo(1);
    assertThat(newPositions.get(0).adGroupIndex).isEqualTo(-1);
    // seek adjustment discontinuity
    assertThat(oldPositions.get(1).periodIndex).isEqualTo(1);
    assertThat(oldPositions.get(1).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(1).periodIndex).isEqualTo(1);
    assertThat(newPositions.get(1).adGroupIndex).isEqualTo(0);
    // auto transition to last frame of first ad period
    assertThat(oldPositions.get(2).periodIndex).isEqualTo(1);
    assertThat(oldPositions.get(2).adGroupIndex).isEqualTo(0);
    assertThat(newPositions.get(2).periodIndex).isEqualTo(1);
    assertThat(newPositions.get(2).adGroupIndex).isEqualTo(-1);
    // auto transition to second ad period
    assertThat(oldPositions.get(3).periodIndex).isEqualTo(1);
    assertThat(oldPositions.get(3).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(3).periodIndex).isEqualTo(2);
    assertThat(newPositions.get(3).adGroupIndex).isEqualTo(0);
    // auto transition to last frame of second ad period
    assertThat(oldPositions.get(4).periodIndex).isEqualTo(2);
    assertThat(oldPositions.get(4).adGroupIndex).isEqualTo(0);
    assertThat(newPositions.get(4).periodIndex).isEqualTo(2);
    assertThat(newPositions.get(4).adGroupIndex).isEqualTo(-1);
    // auto transition to the final content period
    assertThat(oldPositions.get(5).periodIndex).isEqualTo(2);
    assertThat(oldPositions.get(5).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(5).periodIndex).isEqualTo(3);
    assertThat(newPositions.get(5).adGroupIndex).isEqualTo(-1);
    assertThat(newPositions.get(5).positionMs).isEqualTo(2500);
    assertThat(newPositions.get(5).contentPositionMs).isEqualTo(2500);
}
Also used : IllegalSeekPositionException(androidx.media3.common.IllegalSeekPositionException) COMMAND_ADJUST_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_ADJUST_DEVICE_VOLUME) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Uri(android.net.Uri) COMMAND_SEEK_TO_NEXT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM) COMMAND_GET_CURRENT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_GET_CURRENT_MEDIA_ITEM) Allocator(androidx.media3.exoplayer.upstream.Allocator) COMMAND_GET_MEDIA_ITEMS_METADATA(androidx.media3.common.Player.COMMAND_GET_MEDIA_ITEMS_METADATA) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) DefaultTrackSelector(androidx.media3.exoplayer.trackselection.DefaultTrackSelector) COMMAND_SEEK_TO_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_MEDIA_ITEM) COMMAND_GET_TIMELINE(androidx.media3.common.Player.COMMAND_GET_TIMELINE) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) Looper(android.os.Looper) COMMAND_SET_REPEAT_MODE(androidx.media3.common.Player.COMMAND_SET_REPEAT_MODE) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) Surface(android.view.Surface) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Range(com.google.common.collect.Range) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) TextInformationFrame(androidx.media3.extractor.metadata.id3.TextInformationFrame) TestPlayerRunHelper(androidx.media3.test.utils.robolectric.TestPlayerRunHelper) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(androidx.annotation.Nullable) Allocation(androidx.media3.exoplayer.upstream.Allocation) ArgumentMatchers.anyFloat(org.mockito.ArgumentMatchers.anyFloat) Timeline(androidx.media3.common.Timeline) BinaryFrame(androidx.media3.extractor.metadata.id3.BinaryFrame) FakeRenderer(androidx.media3.test.utils.FakeRenderer) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeClock(androidx.media3.test.utils.FakeClock) Mockito.mock(org.mockito.Mockito.mock) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Iterables(com.google.common.collect.Iterables) AdPlaybackState(androidx.media3.common.AdPlaybackState) Loader(androidx.media3.exoplayer.upstream.Loader) COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_IN_CURRENT_MEDIA_ITEM) TestPlayerRunHelper.runUntilPositionDiscontinuity(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPositionDiscontinuity) RunWith(org.junit.runner.RunWith) Config(org.robolectric.annotation.Config) COMMAND_PLAY_PAUSE(androidx.media3.common.Player.COMMAND_PLAY_PAUSE) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ArrayList(java.util.ArrayList) FakeTrackSelection(androidx.media3.test.utils.FakeTrackSelection) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Assertions(androidx.media3.common.util.Assertions) DiscontinuityReason(androidx.media3.common.Player.DiscontinuityReason) COMMAND_SEEK_TO_DEFAULT_POSITION(androidx.media3.common.Player.COMMAND_SEEK_TO_DEFAULT_POSITION) Before(org.junit.Before) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Player(androidx.media3.common.Player) ShadowLooper(org.robolectric.shadows.ShadowLooper) COMMAND_SET_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_SET_DEVICE_VOLUME) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) TestPlayerRunHelper.runUntilSleepingForOffload(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilSleepingForOffload) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Listener(androidx.media3.common.Player.Listener) TestUtil.assertTimelinesSame(androidx.media3.test.utils.TestUtil.assertTimelinesSame) FakeShuffleOrder(androidx.media3.test.utils.FakeShuffleOrder) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito.never(org.mockito.Mockito.never) COMMAND_GET_AUDIO_ATTRIBUTES(androidx.media3.common.Player.COMMAND_GET_AUDIO_ATTRIBUTES) FakeVideoRenderer(androidx.media3.test.utils.FakeVideoRenderer) COMMAND_GET_DEVICE_VOLUME(androidx.media3.common.Player.COMMAND_GET_DEVICE_VOLUME) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) Mockito.reset(org.mockito.Mockito.reset) FakeMediaSourceFactory(androidx.media3.test.utils.FakeMediaSourceFactory) COMMAND_SET_VIDEO_SURFACE(androidx.media3.common.Player.COMMAND_SET_VIDEO_SURFACE) COMMAND_SEEK_TO_PREVIOUS(androidx.media3.common.Player.COMMAND_SEEK_TO_PREVIOUS) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) COMMAND_CHANGE_MEDIA_ITEMS(androidx.media3.common.Player.COMMAND_CHANGE_MEDIA_ITEMS) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Action(androidx.media3.test.utils.Action) Random(java.util.Random) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) COMMAND_STOP(androidx.media3.common.Player.COMMAND_STOP) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) AudioAttributes(androidx.media3.common.AudioAttributes) ApplicationProvider(androidx.test.core.app.ApplicationProvider) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) FakeTrackSelector(androidx.media3.test.utils.FakeTrackSelector) ArgumentMatcher(org.mockito.ArgumentMatcher) END_OF_STREAM_ITEM(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) COMMAND_SET_SPEED_AND_PITCH(androidx.media3.common.Player.COMMAND_SET_SPEED_AND_PITCH) FakeAdaptiveDataSet(androidx.media3.test.utils.FakeAdaptiveDataSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FakeMediaClockRenderer(androidx.media3.test.utils.FakeMediaClockRenderer) Mockito.atLeast(org.mockito.Mockito.atLeast) Assert.fail(org.junit.Assert.fail) FakeSampleStreamItem(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem) MediaItem(androidx.media3.common.MediaItem) TestPlayerRunHelper.runUntilTimelineChanged(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilTimelineChanged) COMMAND_GET_TRACK_INFOS(androidx.media3.common.Player.COMMAND_GET_TRACK_INFOS) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeChunkSource(androidx.media3.test.utils.FakeChunkSource) TestPlayerRunHelper.playUntilPosition(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilPosition) PlaybackException(androidx.media3.common.PlaybackException) TrackGroupArray(androidx.media3.common.TrackGroupArray) Util(androidx.media3.common.util.Util) Collectors(java.util.stream.Collectors) Format(androidx.media3.common.Format) COMMAND_SET_TRACK_SELECTION_PARAMETERS(androidx.media3.common.Player.COMMAND_SET_TRACK_SELECTION_PARAMETERS) COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM(androidx.media3.common.Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM) PlaybackParameters(androidx.media3.common.PlaybackParameters) List(java.util.List) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) TrackSelectionArray(androidx.media3.common.TrackSelectionArray) Mockito.inOrder(org.mockito.Mockito.inOrder) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) AdditionalMatchers.not(org.mockito.AdditionalMatchers.not) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TrackGroup(androidx.media3.common.TrackGroup) Context(android.content.Context) DEFAULT_WINDOW_DURATION_US(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_DURATION_US) COMMAND_SET_SHUFFLE_MODE(androidx.media3.common.Player.COMMAND_SET_SHUFFLE_MODE) COMMAND_GET_TEXT(androidx.media3.common.Player.COMMAND_GET_TEXT) Assert.assertThrows(org.junit.Assert.assertThrows) MediaMetadata(androidx.media3.common.MediaMetadata) Metadata(androidx.media3.common.Metadata) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled) TracksInfo(androidx.media3.common.TracksInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestPlayerRunHelper.playUntilStartOfMediaItem(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) Intent(android.content.Intent) COMMAND_PREPARE(androidx.media3.common.Player.COMMAND_PREPARE) FakeTimeline(androidx.media3.test.utils.FakeTimeline) AtomicReference(java.util.concurrent.atomic.AtomicReference) Shadows.shadowOf(org.robolectric.Shadows.shadowOf) ActionSchedule(androidx.media3.test.utils.ActionSchedule) AudioManager(android.media.AudioManager) FakeDataSource(androidx.media3.test.utils.FakeDataSource) HashSet(java.util.HashSet) COMMAND_SEEK_FORWARD(androidx.media3.common.Player.COMMAND_SEEK_FORWARD) COMMAND_SET_VOLUME(androidx.media3.common.Player.COMMAND_SET_VOLUME) TimelineWindowDefinition(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition) PlayerTarget(androidx.media3.test.utils.ActionSchedule.PlayerTarget) ArgumentCaptor(org.mockito.ArgumentCaptor) ShadowAudioManager(org.robolectric.shadows.ShadowAudioManager) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) ImmutableList(com.google.common.collect.ImmutableList) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Window(androidx.media3.common.Timeline.Window) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) RobolectricUtil.runMainLooperUntil(androidx.media3.test.utils.robolectric.RobolectricUtil.runMainLooperUntil) SurfaceTexture(android.graphics.SurfaceTexture) DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US) MediaSource(androidx.media3.exoplayer.source.MediaSource) InOrder(org.mockito.InOrder) COMMAND_SEEK_BACK(androidx.media3.common.Player.COMMAND_SEEK_BACK) MimeTypes(androidx.media3.common.MimeTypes) COMMAND_SEEK_TO_NEXT(androidx.media3.common.Player.COMMAND_SEEK_TO_NEXT) Clock(androidx.media3.common.util.Clock) Truth.assertThat(com.google.common.truth.Truth.assertThat) COMMAND_SET_MEDIA_ITEMS_METADATA(androidx.media3.common.Player.COMMAND_SET_MEDIA_ITEMS_METADATA) TestPlayerRunHelper.runUntilReceiveOffloadSchedulingEnabledNewState(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilReceiveOffloadSchedulingEnabledNewState) Mockito.verify(org.mockito.Mockito.verify) COMMAND_GET_VOLUME(androidx.media3.common.Player.COMMAND_GET_VOLUME) C(androidx.media3.common.C) Mockito(org.mockito.Mockito) PositionInfo(androidx.media3.common.Player.PositionInfo) FakeSampleStreamItem.oneByteSample(androidx.media3.test.utils.FakeSampleStream.FakeSampleStreamItem.oneByteSample) STATE_ENDED(androidx.media3.common.Player.STATE_ENDED) MediaPeriod(androidx.media3.exoplayer.source.MediaPeriod) Collections(java.util.Collections) TestPlayerRunHelper.runUntilPlaybackState(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.runUntilPlaybackState) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeTimeline(androidx.media3.test.utils.FakeTimeline) PositionInfo(androidx.media3.common.Player.PositionInfo) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Example 9 with Listener

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

the class ExoPlayerTest method setMediaSource_notifiesMediaItemTransition.

@Test
public void setMediaSource_notifiesMediaItemTransition() {
    List<MediaItem> reportedMediaItems = new ArrayList<>();
    List<Integer> reportedTransitionReasons = new ArrayList<>();
    MediaSource mediaSource = FakeMediaSource.createWithWindowId(/* windowId= */
    new Object());
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    player.addListener(new Listener() {

        @Override
        public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
            reportedMediaItems.add(mediaItem);
            reportedTransitionReasons.add(reason);
        }
    });
    player.setMediaSource(mediaSource);
    assertThat(reportedMediaItems).containsExactly(mediaSource.getMediaItem());
    assertThat(reportedTransitionReasons).containsExactly(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED);
    player.release();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) MediaItem(androidx.media3.common.MediaItem) TestPlayerRunHelper.playUntilStartOfMediaItem(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) ArrayList(java.util.ArrayList) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Example 10 with Listener

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

the class ExoPlayerTest method onEvents_correspondToListenerCalls.

@Test
public void onEvents_correspondToListenerCalls() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Player.Listener listener = mock(Player.Listener.class);
    player.addListener(listener);
    Format formatWithStaticMetadata = new Format.Builder().setSampleMimeType(MimeTypes.VIDEO_H264).setMetadata(new Metadata(new BinaryFrame(/* id= */
    "", /* data= */
    new byte[0]), new TextInformationFrame(/* id= */
    "TT2", /* description= */
    null, /* value= */
    "title"))).build();
    // Set multiple values together.
    player.setMediaSource(new FakeMediaSource(new FakeTimeline(), formatWithStaticMetadata));
    player.seekTo(2_000);
    player.setPlaybackParameters(new PlaybackParameters(/* speed= */
    2.0f));
    runUntilPendingCommandsAreFullyHandled(player);
    verify(listener).onTimelineChanged(any(), anyInt());
    verify(listener).onMediaItemTransition(any(), anyInt());
    verify(listener).onPositionDiscontinuity(any(), any(), anyInt());
    verify(listener).onPlaybackParametersChanged(any());
    ArgumentCaptor<Player.Events> eventCaptor = ArgumentCaptor.forClass(Player.Events.class);
    verify(listener).onEvents(eq(player), eventCaptor.capture());
    Player.Events events = eventCaptor.getValue();
    assertThat(events.contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(events.contains(Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
    assertThat(events.contains(Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
    assertThat(events.contains(Player.EVENT_PLAYBACK_PARAMETERS_CHANGED)).isTrue();
    // Set values recursively.
    player.addListener(new Player.Listener() {

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            player.setShuffleModeEnabled(true);
        }
    });
    player.setRepeatMode(Player.REPEAT_MODE_ONE);
    runUntilPendingCommandsAreFullyHandled(player);
    verify(listener).onRepeatModeChanged(anyInt());
    verify(listener).onShuffleModeEnabledChanged(anyBoolean());
    verify(listener, times(2)).onEvents(eq(player), eventCaptor.capture());
    events = Iterables.getLast(eventCaptor.getAllValues());
    assertThat(events.contains(Player.EVENT_REPEAT_MODE_CHANGED)).isTrue();
    assertThat(events.contains(Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED)).isTrue();
    // Ensure all other events are called (even though we can't control how exactly they are
    // combined together in onEvents calls).
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    player.play();
    player.setMediaItem(MediaItem.fromUri("http://this-will-throw-an-exception.mp4"));
    TestPlayerRunHelper.runUntilError(player);
    runUntilPendingCommandsAreFullyHandled(player);
    player.release();
    // Verify that all callbacks have been called at least once.
    verify(listener, atLeastOnce()).onTimelineChanged(any(), anyInt());
    verify(listener, atLeastOnce()).onMediaItemTransition(any(), anyInt());
    verify(listener, atLeastOnce()).onPositionDiscontinuity(any(), any(), anyInt());
    verify(listener, atLeastOnce()).onPlaybackParametersChanged(any());
    verify(listener, atLeastOnce()).onRepeatModeChanged(anyInt());
    verify(listener, atLeastOnce()).onShuffleModeEnabledChanged(anyBoolean());
    verify(listener, atLeastOnce()).onPlaybackStateChanged(anyInt());
    verify(listener, atLeastOnce()).onIsLoadingChanged(anyBoolean());
    verify(listener, atLeastOnce()).onTracksChanged(any(), any());
    verify(listener, atLeastOnce()).onMediaMetadataChanged(any());
    verify(listener, atLeastOnce()).onPlayWhenReadyChanged(anyBoolean(), anyInt());
    verify(listener, atLeastOnce()).onIsPlayingChanged(anyBoolean());
    verify(listener, atLeastOnce()).onPlayerErrorChanged(any());
    verify(listener, atLeastOnce()).onPlayerError(any());
    // Verify all the same events have been recorded with onEvents.
    verify(listener, atLeastOnce()).onEvents(eq(player), eventCaptor.capture());
    List<Player.Events> allEvents = eventCaptor.getAllValues();
    assertThat(containsEvent(allEvents, Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAYBACK_PARAMETERS_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_REPEAT_MODE_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAYBACK_STATE_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_IS_LOADING_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_TRACKS_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_MEDIA_METADATA_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAY_WHEN_READY_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_IS_PLAYING_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAYER_ERROR)).isTrue();
}
Also used : Player(androidx.media3.common.Player) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaMetadata(androidx.media3.common.MediaMetadata) Metadata(androidx.media3.common.Metadata) TextInformationFrame(androidx.media3.extractor.metadata.id3.TextInformationFrame) Listener(androidx.media3.common.Player.Listener) Format(androidx.media3.common.Format) BinaryFrame(androidx.media3.extractor.metadata.id3.BinaryFrame) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)216 Player (androidx.media3.common.Player)173 CountDownLatch (java.util.concurrent.CountDownLatch)143 AtomicReference (java.util.concurrent.atomic.AtomicReference)108 LargeTest (androidx.test.filters.LargeTest)102 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)102 MediaItem (androidx.media3.common.MediaItem)70 Timeline (androidx.media3.common.Timeline)70 EventTime (androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime)68 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)58 PositionInfo (androidx.media3.common.Player.PositionInfo)56 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)54 Bundle (android.os.Bundle)50 Listener (androidx.media3.common.Player.Listener)45 ArrayList (java.util.ArrayList)45 Nullable (androidx.annotation.Nullable)42 RemoteMockPlayer (androidx.media3.session.RemoteMediaSession.RemoteMockPlayer)40 ConcatenatingMediaSource (androidx.media3.exoplayer.source.ConcatenatingMediaSource)36 AtomicLong (java.util.concurrent.atomic.AtomicLong)36 MediaSource (androidx.media3.exoplayer.source.MediaSource)35