use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.
the class SessionPlayerConnectorTest method getPlaylist_returnsPlaylistInUnderlyingPlayer.
@Test
@LargeTest
public void getPlaylist_returnsPlaylistInUnderlyingPlayer() {
List<MediaItem> playlistToExoPlayer = TestUtils.createPlaylist(4);
DefaultMediaItemConverter converter = new DefaultMediaItemConverter();
List<com.google.android.exoplayer2.MediaItem> exoMediaItems = new ArrayList<>();
for (MediaItem mediaItem : playlistToExoPlayer) {
exoMediaItems.add(converter.convertToExoPlayerMediaItem(mediaItem));
}
AtomicReference<List<MediaItem>> playlistFromSessionPlayer = new AtomicReference<>();
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
exoPlayer.setMediaItems(exoMediaItems);
try (SessionPlayerConnector sessionPlayer = new SessionPlayerConnector(exoPlayer)) {
List<MediaItem> playlist = sessionPlayer.getPlaylist();
playlistFromSessionPlayer.set(playlist);
}
});
assertThat(playlistFromSessionPlayer.get()).isEqualTo(playlistToExoPlayer);
}
use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.
the class SessionPlayerConnectorTest method play_onceWithAudioResource_changesPlayerStateToPlaying.
@Test
@LargeTest
public void play_onceWithAudioResource_changesPlayerStateToPlaying() throws Exception {
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
AudioAttributesCompat attributes = new AudioAttributesCompat.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build();
sessionPlayerConnector.setAudioAttributes(attributes);
CountDownLatch onPlayingLatch = new CountDownLatch(1);
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
@Override
public void onPlayerStateChanged(SessionPlayer player, int playerState) {
if (playerState == PLAYER_STATE_PLAYING) {
onPlayingLatch.countDown();
}
}
});
sessionPlayerConnector.prepare();
sessionPlayerConnector.play();
assertThat(onPlayingLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.
the class SessionPlayerConnectorTest method setPlaybackSpeed_afterPlayback_remainsSame.
@Test
@LargeTest
public void setPlaybackSpeed_afterPlayback_remainsSame() throws Exception {
int resId1 = R.raw.video_big_buck_bunny;
MediaItem mediaItem1 = new UriMediaItem.Builder(RawResourceDataSource.buildRawResourceUri(resId1)).setStartPosition(6_000).setEndPosition(7_000).build();
MediaItem mediaItem2 = new UriMediaItem.Builder(RawResourceDataSource.buildRawResourceUri(resId1)).setStartPosition(3_000).setEndPosition(4_000).build();
List<MediaItem> items = new ArrayList<>();
items.add(mediaItem1);
items.add(mediaItem2);
sessionPlayerConnector.setPlaylist(items, null);
CountDownLatch onPlaybackCompletedLatch = new CountDownLatch(1);
SessionPlayer.PlayerCallback callback = new SessionPlayer.PlayerCallback() {
@Override
public void onPlaybackCompleted(SessionPlayer player) {
onPlaybackCompletedLatch.countDown();
}
};
sessionPlayerConnector.registerPlayerCallback(executor, callback);
sessionPlayerConnector.prepare().get();
sessionPlayerConnector.setPlaybackSpeed(2.0f);
sessionPlayerConnector.play();
assertThat(onPlaybackCompletedLatch.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
assertThat(sessionPlayerConnector.getCurrentMediaItem()).isEqualTo(mediaItem2);
assertThat(sessionPlayerConnector.getPlaybackSpeed()).isWithin(0.001f).of(2.0f);
}
use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.
the class SessionPlayerConnectorTest method pause_byUnderlyingPlayer_notifiesOnPlayerStateChanges.
@Test
@LargeTest
public void pause_byUnderlyingPlayer_notifiesOnPlayerStateChanges() throws Exception {
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
CountDownLatch onPausedLatch = new CountDownLatch(1);
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
@Override
public void onPlayerStateChanged(SessionPlayer player, int playerState) {
if (playerState == PLAYER_STATE_PAUSED) {
onPausedLatch.countDown();
}
}
});
assertPlayerResultSuccess(sessionPlayerConnector.play());
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> exoPlayer.setPlayWhenReady(false));
assertThat(onPausedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
use of androidx.media2.common.SessionPlayer in project ExoPlayer by google.
the class SessionPlayerConnectorTest method pause_byUnderlyingPlayerInListener_changesToPlayerStatePaused.
@Test
@LargeTest
public void pause_byUnderlyingPlayerInListener_changesToPlayerStatePaused() throws Exception {
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
CountDownLatch playerStateChangesLatch = new CountDownLatch(3);
CopyOnWriteArrayList<Integer> playerStateChanges = new CopyOnWriteArrayList<>();
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
@Override
public void onPlayerStateChanged(SessionPlayer player, int playerState) {
playerStateChanges.add(playerState);
playerStateChangesLatch.countDown();
}
});
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> exoPlayer.addListener(new Player.Listener() {
@Override
public void onPlayWhenReadyChanged(boolean playWhenReady, int reason) {
if (playWhenReady) {
exoPlayer.setPlayWhenReady(false);
}
}
}));
assertPlayerResultSuccess(sessionPlayerConnector.play());
assertThat(playerStateChangesLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
assertThat(playerStateChanges).containsExactly(// After prepare()
PLAYER_STATE_PAUSED, // After play()
PLAYER_STATE_PLAYING, // After setPlayWhenREady(false)
PLAYER_STATE_PAUSED).inOrder();
assertThat(sessionPlayerConnector.getPlayerState()).isEqualTo(PLAYER_STATE_PAUSED);
}
Aggregations