use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class ExoPlayerTest method setMediaItems_callsListenersWithSameInstanceOfMediaItem.
@Test
public void setMediaItems_callsListenersWithSameInstanceOfMediaItem() throws Exception {
ArgumentCaptor<Timeline> timeline = ArgumentCaptor.forClass(Timeline.class);
ArgumentCaptor<Player.PositionInfo> oldPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
ArgumentCaptor<Player.PositionInfo> newPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
ArgumentCaptor<MediaItem> currentMediaItem = ArgumentCaptor.forClass(MediaItem.class);
Window window = new Timeline.Window();
ExoPlayer player = new TestExoPlayerBuilder(context).setMediaSourceFactory(new FakeMediaSourceFactory()).build();
Player.Listener listener = mock(Player.Listener.class);
player.addListener(listener);
List<MediaItem> playlist = ImmutableList.of(MediaItem.fromUri("http://item-0.com/"), MediaItem.fromUri("http://item-1.com/"));
player.setMediaItems(playlist);
player.prepare();
player.seekTo(/* positionMs= */
2000);
player.play();
TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
InOrder inOrder = inOrder(listener);
inOrder.verify(listener).onTimelineChanged(timeline.capture(), eq(Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED));
inOrder.verify(listener).onMediaItemTransition(currentMediaItem.capture(), eq(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED));
inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_SEEK));
inOrder.verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
inOrder.verify(listener).onMediaItemTransition(currentMediaItem.capture(), eq(Player.MEDIA_ITEM_TRANSITION_REASON_AUTO));
inOrder.verify(listener, never()).onPositionDiscontinuity(any(), any(), anyInt());
inOrder.verify(listener, never()).onMediaItemTransition(any(), anyInt());
assertThat(timeline.getValue().getWindow(0, window).mediaItem).isSameInstanceAs(playlist.get(0));
assertThat(timeline.getValue().getWindow(1, window).mediaItem).isSameInstanceAs(playlist.get(1));
assertThat(oldPosition.getAllValues().get(0).mediaItem).isSameInstanceAs(playlist.get(0));
assertThat(newPosition.getAllValues().get(0).mediaItem).isSameInstanceAs(playlist.get(0));
assertThat(oldPosition.getAllValues().get(1).mediaItem).isSameInstanceAs(playlist.get(0));
assertThat(newPosition.getAllValues().get(1).mediaItem).isSameInstanceAs(playlist.get(1));
assertThat(currentMediaItem.getAllValues().get(0)).isSameInstanceAs(playlist.get(0));
assertThat(currentMediaItem.getAllValues().get(1)).isSameInstanceAs(playlist.get(1));
player.release();
}
use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class ExoPlayerTest method seekTo_sameWindow_doesNotNotifyMediaItemTransition.
@Test
public void seekTo_sameWindow_doesNotNotifyMediaItemTransition() throws Exception {
List<MediaItem> reportedMediaItems = new ArrayList<>();
List<Integer> reportedTransitionReasons = new ArrayList<>();
MediaSource mediaSource1 = FakeMediaSource.createWithWindowId(/* windowId= */
new Object());
MediaSource mediaSource2 = 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.setMediaSources(ImmutableList.of(mediaSource1, mediaSource2));
player.prepare();
runUntilPlaybackState(player, Player.STATE_READY);
player.seekTo(/* mediaItemIndex= */
0, /* positionMs= */
2000);
assertThat(reportedMediaItems).containsExactly(mediaSource1.getMediaItem()).inOrder();
assertThat(reportedTransitionReasons).containsExactly(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED).inOrder();
player.release();
}
use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class ExoPlayerTest method recursivePlayerChangesAreReportedInCorrectOrder.
@Test
public void recursivePlayerChangesAreReportedInCorrectOrder() throws Exception {
// The listener stops the player as soon as it's ready (which should report a timeline and state
// change) and sets playWhenReady to false when the timeline callback is received.
final AtomicReference<Player> playerReference = new AtomicReference<>();
final List<Boolean> playerListenerPlayWhenReady = new ArrayList<>();
final List<Integer> playerListenerStates = new ArrayList<>();
List<Integer> sequence = new ArrayList<>();
final Player.Listener playerListener = new Player.Listener() {
@Override
public void onPlaybackStateChanged(@Player.State int playbackState) {
playerListenerStates.add(playbackState);
if (playbackState == Player.STATE_READY) {
playerReference.get().stop(/* reset= */
true);
sequence.add(0);
}
}
@Override
public void onTimelineChanged(Timeline timeline, int reason) {
if (timeline.isEmpty()) {
playerReference.get().pause();
sequence.add(1);
}
}
@Override
public void onPlayWhenReadyChanged(boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
playerListenerPlayWhenReady.add(playWhenReady);
sequence.add(2);
}
};
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {
@Override
public void run(ExoPlayer player) {
playerReference.set(player);
player.addListener(playerListener);
}
}).build();
new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
assertThat(playerListenerStates).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
assertThat(playerListenerPlayWhenReady).containsExactly(false).inOrder();
assertThat(sequence).containsExactly(0, 1, 2).inOrder();
}
use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class ExoPlayerTest method seekTo_cancelsSourceDiscontinuity_callsOnPositionDiscontinuity.
@Test
public void seekTo_cancelsSourceDiscontinuity_callsOnPositionDiscontinuity() throws Exception {
Timeline timeline1 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
1), new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
2));
final Timeline timeline2 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
1), new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
3));
final FakeMediaSource mediaSource = new FakeMediaSource(timeline1, ExoPlayerTestRunner.VIDEO_FORMAT);
ExoPlayer player = new TestExoPlayerBuilder(context).build();
Player.Listener listener = mock(Player.Listener.class);
player.addListener(listener);
player.setMediaSource(mediaSource);
player.prepare();
TestPlayerRunHelper.playUntilPosition(player, /* mediaItemIndex= */
1, /* positionMs= */
2000);
player.seekTo(/* mediaItemIndex= */
1, /* positionMs= */
2122);
// This causes a DISCONTINUITY_REASON_REMOVE between pending operations that needs to be
// cancelled by the seek below.
mediaSource.setNewSourceInfo(timeline2);
player.play();
player.seekTo(/* mediaItemIndex= */
0, /* positionMs= */
2222);
TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
ArgumentCaptor<Player.PositionInfo> oldPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
ArgumentCaptor<Player.PositionInfo> newPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
InOrder inOrder = inOrder(listener);
inOrder.verify(listener).onPositionDiscontinuity(any(), any(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
inOrder.verify(listener, times(2)).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_SEEK));
inOrder.verify(listener).onPositionDiscontinuity(any(), any(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
inOrder.verify(listener, never()).onPositionDiscontinuity(any(), any(), anyInt());
List<Player.PositionInfo> oldPositions = oldPosition.getAllValues();
List<Player.PositionInfo> newPositions = newPosition.getAllValues();
// First seek
assertThat(oldPositions.get(0).mediaItemIndex).isEqualTo(1);
assertThat(oldPositions.get(0).positionMs).isIn(Range.closed(1980L, 2000L));
assertThat(oldPositions.get(0).contentPositionMs).isIn(Range.closed(1980L, 2000L));
assertThat(newPositions.get(0).mediaItemIndex).isEqualTo(1);
assertThat(newPositions.get(0).positionMs).isEqualTo(2122);
assertThat(newPositions.get(0).contentPositionMs).isEqualTo(2122);
// Second seek.
assertThat(oldPositions.get(1).mediaItemIndex).isEqualTo(1);
assertThat(oldPositions.get(1).positionMs).isEqualTo(2122);
assertThat(oldPositions.get(1).contentPositionMs).isEqualTo(2122);
assertThat(newPositions.get(1).mediaItemIndex).isEqualTo(0);
assertThat(newPositions.get(1).positionMs).isEqualTo(2222);
assertThat(newPositions.get(1).contentPositionMs).isEqualTo(2222);
player.release();
}
use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class ExoPlayerTest method simplePlaybackHasNoPlaybackSuppression.
@Test
public void simplePlaybackHasNoPlaybackSuppression() throws Exception {
ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).play().waitForPlaybackState(Player.STATE_READY).pause().play().build();
AtomicBoolean seenPlaybackSuppression = new AtomicBoolean();
Player.Listener listener = new Player.Listener() {
@Override
public void onPlaybackSuppressionReasonChanged(@Player.PlaybackSuppressionReason int playbackSuppressionReason) {
seenPlaybackSuppression.set(true);
}
};
new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).setPlayerListener(listener).build().start().blockUntilEnded(TIMEOUT_MS);
assertThat(seenPlaybackSuppression.get()).isFalse();
}
Aggregations