use of androidx.media3.common.MediaItem in project media by androidx.
the class CastPlayerTest method removeMediaItem_atTheEnd_notifiesAvailableCommandsChanged.
@Test
public void removeMediaItem_atTheEnd_notifiesAvailableCommandsChanged() {
Player.Commands defaultCommands = createWithDefaultCommands();
Player.Commands commandsWithSeekToNextWindow = createWithDefaultCommands(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, COMMAND_SEEK_TO_NEXT);
Player.Commands emptyTimelineCommands = createWithDefaultCommands(/* isTimelineEmpty= */
true);
MediaItem mediaItem1 = createMediaItem(/* mediaQueueItemId= */
1);
MediaItem mediaItem2 = createMediaItem(/* mediaQueueItemId= */
2);
MediaItem mediaItem3 = createMediaItem(/* mediaQueueItemId= */
3);
castPlayer.addMediaItems(ImmutableList.of(mediaItem1, mediaItem2, mediaItem3));
updateTimeLine(ImmutableList.of(mediaItem1, mediaItem2, mediaItem3), /* mediaQueueItemIds= */
new int[] { 1, 2, 3 }, /* currentItemId= */
1);
verify(mockListener).onAvailableCommandsChanged(commandsWithSeekToNextWindow);
// Check that there were no other calls to onAvailableCommandsChanged.
verify(mockListener).onAvailableCommandsChanged(any());
castPlayer.removeMediaItem(/* index= */
2);
updateTimeLine(ImmutableList.of(mediaItem1, mediaItem2), /* mediaQueueItemIds= */
new int[] { 1, 2 }, /* currentItemId= */
1);
verify(mockListener).onAvailableCommandsChanged(any());
castPlayer.removeMediaItem(/* index= */
1);
updateTimeLine(ImmutableList.of(mediaItem1), /* mediaQueueItemIds= */
new int[] { 1 }, /* currentItemId= */
1);
verify(mockListener).onAvailableCommandsChanged(defaultCommands);
verify(mockListener, times(2)).onAvailableCommandsChanged(any());
castPlayer.removeMediaItem(/* index= */
0);
updateTimeLine(ImmutableList.of(), /* mediaQueueItemIds= */
new int[0], /* currentItemId= */
C.INDEX_UNSET);
verify(mockListener).onAvailableCommandsChanged(emptyTimelineCommands);
verify(mockListener, times(3)).onAvailableCommandsChanged(any());
}
use of androidx.media3.common.MediaItem in project media by androidx.
the class CastPlayerTest method removeCurrentMediaItem_byRemoteClient_notifiesPositionDiscontinuity.
@Test
// Mocks deprecated method used by the CastPlayer.
@SuppressWarnings("deprecation")
public void removeCurrentMediaItem_byRemoteClient_notifiesPositionDiscontinuity() {
MediaItem mediaItem1 = createMediaItem(/* mediaQueueItemId= */
1);
MediaItem mediaItem2 = createMediaItem(/* mediaQueueItemId= */
2);
List<MediaItem> mediaItems = ImmutableList.of(mediaItem1, mediaItem2);
castPlayer.addMediaItems(mediaItems);
updateTimeLine(mediaItems, new int[] { 1, 2 }, /* currentItemId= */
1, new int[] { MediaInfo.STREAM_TYPE_BUFFERED, MediaInfo.STREAM_TYPE_BUFFERED }, /* durationsMs= */
new long[] { 20_000L, 30_000L }, /* positionMs= */
1234);
// Update with the new timeline after removal on the device.
updateTimeLine(ImmutableList.of(mediaItem2), /* mediaQueueItemIds= */
new int[] { 2 }, /* currentItemId= */
2, new int[] { MediaInfo.STREAM_TYPE_BUFFERED }, /* durationsMs= */
new long[] { 30_000L }, /* positionMs= */
0);
Player.PositionInfo oldPosition = new Player.PositionInfo(/* windowUid= */
1, /* windowIndex= */
0, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(1).build(), /* periodUid= */
1, /* periodIndex= */
0, // position at which we receive the timeline change
0, // position at which we receive the timeline change
0, /* adGroupIndex= */
C.INDEX_UNSET, /* adIndexInAdGroup= */
C.INDEX_UNSET);
Player.PositionInfo newPosition = new Player.PositionInfo(/* windowUid= */
2, /* windowIndex= */
0, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(2).build(), /* periodUid= */
2, /* periodIndex= */
0, /* positionMs= */
0, /* contentPositionMs= */
0, /* adGroupIndex= */
C.INDEX_UNSET, /* adIndexInAdGroup= */
C.INDEX_UNSET);
InOrder inOrder = Mockito.inOrder(mockListener);
inOrder.verify(mockListener).onPositionDiscontinuity(eq(Player.DISCONTINUITY_REASON_REMOVE));
inOrder.verify(mockListener).onPositionDiscontinuity(eq(oldPosition), eq(newPosition), eq(Player.DISCONTINUITY_REASON_REMOVE));
inOrder.verify(mockListener, never()).onPositionDiscontinuity(anyInt());
inOrder.verify(mockListener, never()).onPositionDiscontinuity(any(), any(), anyInt());
}
use of androidx.media3.common.MediaItem in project media by androidx.
the class CastPlayerTest method removeMediaItem_current_notifiesAvailableCommandsChanged.
@Test
public void removeMediaItem_current_notifiesAvailableCommandsChanged() {
Player.Commands defaultCommands = createWithDefaultCommands();
Player.Commands commandsWithSeekToNextWindow = createWithDefaultCommands(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, COMMAND_SEEK_TO_NEXT);
MediaItem mediaItem1 = createMediaItem(/* mediaQueueItemId= */
1);
MediaItem mediaItem2 = createMediaItem(/* mediaQueueItemId= */
2);
castPlayer.addMediaItems(ImmutableList.of(mediaItem1, mediaItem2));
updateTimeLine(ImmutableList.of(mediaItem1, mediaItem2), /* mediaQueueItemIds= */
new int[] { 1, 2 }, /* currentItemId= */
1);
verify(mockListener).onAvailableCommandsChanged(commandsWithSeekToNextWindow);
// Check that there were no other calls to onAvailableCommandsChanged.
verify(mockListener).onAvailableCommandsChanged(any());
castPlayer.removeMediaItem(/* index= */
0);
updateTimeLine(ImmutableList.of(mediaItem2), /* mediaQueueItemIds= */
new int[] { 2 }, /* currentItemId= */
2);
verify(mockListener).onAvailableCommandsChanged(defaultCommands);
verify(mockListener, times(2)).onAvailableCommandsChanged(any());
}
use of androidx.media3.common.MediaItem in project media by androidx.
the class CastPlayerTest method updateTimeLine.
private void updateTimeLine(List<MediaItem> mediaItems, int[] mediaQueueItemIds, int currentItemId, int[] streamTypes, long[] durationsMs, long positionMs) {
// Set up mocks to allow the player to update the timeline.
List<MediaQueueItem> queueItems = new ArrayList<>();
for (int i = 0; i < mediaQueueItemIds.length; i++) {
MediaItem mediaItem = mediaItems.get(i);
int mediaQueueItemId = mediaQueueItemIds[i];
int streamType = streamTypes[i];
long durationMs = durationsMs[i];
MediaInfo.Builder mediaInfoBuilder = new MediaInfo.Builder(mediaItem.localConfiguration.uri.toString()).setStreamType(streamType).setContentType(mediaItem.localConfiguration.mimeType);
if (durationMs != C.TIME_UNSET) {
mediaInfoBuilder.setStreamDuration(durationMs);
}
MediaInfo mediaInfo = mediaInfoBuilder.build();
MediaQueueItem mediaQueueItem = mock(MediaQueueItem.class);
when(mediaQueueItem.getItemId()).thenReturn(mediaQueueItemId);
when(mediaQueueItem.getMedia()).thenReturn(mediaInfo);
queueItems.add(mediaQueueItem);
if (mediaQueueItemId == currentItemId) {
when(mockRemoteMediaClient.getCurrentItem()).thenReturn(mediaQueueItem);
when(mockMediaStatus.getMediaInfo()).thenReturn(mediaInfo);
}
}
if (positionMs != C.TIME_UNSET) {
when(mockRemoteMediaClient.getApproximateStreamPosition()).thenReturn(positionMs);
}
when(mockMediaQueue.getItemIds()).thenReturn(mediaQueueItemIds);
when(mockMediaStatus.getQueueItems()).thenReturn(queueItems);
when(mockMediaStatus.getCurrentItemId()).thenReturn(currentItemId == C.INDEX_UNSET ? 0 : currentItemId);
// Call listener to update the timeline of the player.
remoteMediaClientCallback.onStatusUpdated();
}
use of androidx.media3.common.MediaItem in project media by androidx.
the class CastPlayerTest method autoTransition_notifiesPositionDiscontinuity.
@Test
// Mocks deprecated method used by the CastPlayer.
@SuppressWarnings("deprecation")
public void autoTransition_notifiesPositionDiscontinuity() {
int[] mediaQueueItemIds = new int[] { 1, 2 };
int[] streamTypes = { MediaInfo.STREAM_TYPE_BUFFERED, MediaInfo.STREAM_TYPE_BUFFERED };
long[] durationsFirstMs = { 12500, C.TIME_UNSET };
// When the remote Cast player transitions to an item that wasn't played before, the media state
// delivers the duration for that media item which updates the timeline accordingly.
long[] durationsSecondMs = { 12500, 22000 };
List<MediaItem> mediaItems = createMediaItems(mediaQueueItemIds);
castPlayer.addMediaItems(mediaItems);
updateTimeLine(mediaItems, mediaQueueItemIds, /* currentItemId= */
1, /* streamTypes= */
streamTypes, /* durationsMs= */
durationsFirstMs, /* positionMs= */
C.TIME_UNSET);
updateTimeLine(mediaItems, mediaQueueItemIds, /* currentItemId= */
2, /* streamTypes= */
streamTypes, /* durationsMs= */
durationsSecondMs, /* positionMs= */
C.TIME_UNSET);
Player.PositionInfo oldPosition = new Player.PositionInfo(/* windowUid= */
1, /* windowIndex= */
0, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(1).build(), /* periodUid= */
1, /* periodIndex= */
0, /* positionMs= */
12500, /* contentPositionMs= */
12500, /* adGroupIndex= */
C.INDEX_UNSET, /* adIndexInAdGroup= */
C.INDEX_UNSET);
Player.PositionInfo newPosition = new Player.PositionInfo(/* windowUid= */
2, /* windowIndex= */
1, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(2).build(), /* periodUid= */
2, /* periodIndex= */
1, /* positionMs= */
0, /* contentPositionMs= */
0, /* adGroupIndex= */
C.INDEX_UNSET, /* adIndexInAdGroup= */
C.INDEX_UNSET);
InOrder inOrder = Mockito.inOrder(mockListener);
inOrder.verify(mockListener).onPositionDiscontinuity(eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
inOrder.verify(mockListener).onPositionDiscontinuity(eq(oldPosition), eq(newPosition), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
inOrder.verify(mockListener, never()).onPositionDiscontinuity(anyInt());
inOrder.verify(mockListener, never()).onPositionDiscontinuity(any(), any(), anyInt());
}
Aggregations