Migrate usages of SimpleExoPlayer to ExoPlayer
SimpleExoPlayer is being deprecated in favour of ExoPlayer. PiperOrigin-RevId: 402869414
This commit is contained in:
parent
3c19850ed3
commit
23fc1f4384
@ -22,7 +22,6 @@ import androidx.annotation.Nullable;
|
|||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
import androidx.test.platform.app.InstrumentationRegistry;
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
import com.google.android.exoplayer2.ExoPlayer;
|
import com.google.android.exoplayer2.ExoPlayer;
|
||||||
import com.google.android.exoplayer2.SimpleExoPlayer;
|
|
||||||
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
|
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
|
||||||
import com.google.android.exoplayer2.upstream.DataSource;
|
import com.google.android.exoplayer2.upstream.DataSource;
|
||||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||||
@ -49,7 +48,7 @@ import org.junit.rules.ExternalResource;
|
|||||||
private ExecutorService executor;
|
private ExecutorService executor;
|
||||||
|
|
||||||
private SessionPlayerConnector sessionPlayerConnector;
|
private SessionPlayerConnector sessionPlayerConnector;
|
||||||
private SimpleExoPlayer exoPlayer;
|
private ExoPlayer exoPlayer;
|
||||||
@Nullable private DataSourceInstrumentation dataSourceInstrumentation;
|
@Nullable private DataSourceInstrumentation dataSourceInstrumentation;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -111,7 +110,7 @@ import org.junit.rules.ExternalResource;
|
|||||||
return sessionPlayerConnector;
|
return sessionPlayerConnector;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleExoPlayer getSimpleExoPlayer() {
|
public ExoPlayer getExoPlayer() {
|
||||||
return exoPlayer;
|
return exoPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,6 @@ import com.google.android.exoplayer2.DefaultControlDispatcher;
|
|||||||
import com.google.android.exoplayer2.ExoPlayer;
|
import com.google.android.exoplayer2.ExoPlayer;
|
||||||
import com.google.android.exoplayer2.ForwardingPlayer;
|
import com.google.android.exoplayer2.ForwardingPlayer;
|
||||||
import com.google.android.exoplayer2.Player;
|
import com.google.android.exoplayer2.Player;
|
||||||
import com.google.android.exoplayer2.SimpleExoPlayer;
|
|
||||||
import com.google.android.exoplayer2.ext.media2.test.R;
|
import com.google.android.exoplayer2.ext.media2.test.R;
|
||||||
import com.google.android.exoplayer2.upstream.RawResourceDataSource;
|
import com.google.android.exoplayer2.upstream.RawResourceDataSource;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
@ -167,20 +166,19 @@ public class SessionPlayerConnectorTest {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
SimpleExoPlayer simpleExoPlayer = null;
|
ExoPlayer exoPlayer = null;
|
||||||
SessionPlayerConnector playerConnector = null;
|
SessionPlayerConnector playerConnector = null;
|
||||||
try {
|
try {
|
||||||
simpleExoPlayer = new ExoPlayer.Builder(context).setLooper(Looper.myLooper()).build();
|
exoPlayer = new ExoPlayer.Builder(context).setLooper(Looper.myLooper()).build();
|
||||||
playerConnector =
|
playerConnector = new SessionPlayerConnector(exoPlayer, new DefaultMediaItemConverter());
|
||||||
new SessionPlayerConnector(simpleExoPlayer, new DefaultMediaItemConverter());
|
|
||||||
playerConnector.setControlDispatcher(controlDispatcher);
|
playerConnector.setControlDispatcher(controlDispatcher);
|
||||||
assertPlayerResult(playerConnector.play(), RESULT_INFO_SKIPPED);
|
assertPlayerResult(playerConnector.play(), RESULT_INFO_SKIPPED);
|
||||||
} finally {
|
} finally {
|
||||||
if (playerConnector != null) {
|
if (playerConnector != null) {
|
||||||
playerConnector.close();
|
playerConnector.close();
|
||||||
}
|
}
|
||||||
if (simpleExoPlayer != null) {
|
if (exoPlayer != null) {
|
||||||
simpleExoPlayer.release();
|
exoPlayer.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -195,9 +193,9 @@ public class SessionPlayerConnectorTest {
|
|||||||
Player forwardingPlayer = null;
|
Player forwardingPlayer = null;
|
||||||
SessionPlayerConnector playerConnector = null;
|
SessionPlayerConnector playerConnector = null;
|
||||||
try {
|
try {
|
||||||
Player simpleExoPlayer = new ExoPlayer.Builder(context).setLooper(Looper.myLooper()).build();
|
Player exoPlayer = new ExoPlayer.Builder(context).setLooper(Looper.myLooper()).build();
|
||||||
forwardingPlayer =
|
forwardingPlayer =
|
||||||
new ForwardingPlayer(simpleExoPlayer) {
|
new ForwardingPlayer(exoPlayer) {
|
||||||
@Override
|
@Override
|
||||||
public boolean isCommandAvailable(int command) {
|
public boolean isCommandAvailable(int command) {
|
||||||
if (command == COMMAND_PLAY_PAUSE) {
|
if (command == COMMAND_PLAY_PAUSE) {
|
||||||
@ -457,13 +455,12 @@ public class SessionPlayerConnectorTest {
|
|||||||
public void seekTo_whenUnderlyingPlayerAlsoSeeks_throwsNoException() throws Exception {
|
public void seekTo_whenUnderlyingPlayerAlsoSeeks_throwsNoException() throws Exception {
|
||||||
TestUtils.loadResource(R.raw.video_big_buck_bunny, sessionPlayerConnector);
|
TestUtils.loadResource(R.raw.video_big_buck_bunny, sessionPlayerConnector);
|
||||||
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
||||||
SimpleExoPlayer simpleExoPlayer = playerTestRule.getSimpleExoPlayer();
|
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
|
||||||
|
|
||||||
List<ListenableFuture<PlayerResult>> futures = new ArrayList<>();
|
List<ListenableFuture<PlayerResult>> futures = new ArrayList<>();
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
futures.add(sessionPlayerConnector.seekTo(4123));
|
futures.add(sessionPlayerConnector.seekTo(4123));
|
||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> exoPlayer.seekTo(1243));
|
||||||
.runOnMainSync(() -> simpleExoPlayer.seekTo(1243));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ListenableFuture<PlayerResult> future : futures) {
|
for (ListenableFuture<PlayerResult> future : futures) {
|
||||||
@ -477,7 +474,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
public void seekTo_byUnderlyingPlayer_notifiesOnSeekCompleted() throws Exception {
|
public void seekTo_byUnderlyingPlayer_notifiesOnSeekCompleted() throws Exception {
|
||||||
TestUtils.loadResource(R.raw.video_big_buck_bunny, sessionPlayerConnector);
|
TestUtils.loadResource(R.raw.video_big_buck_bunny, sessionPlayerConnector);
|
||||||
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
||||||
SimpleExoPlayer simpleExoPlayer = playerTestRule.getSimpleExoPlayer();
|
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
|
||||||
long testSeekPosition = 1023;
|
long testSeekPosition = 1023;
|
||||||
AtomicLong seekPosition = new AtomicLong();
|
AtomicLong seekPosition = new AtomicLong();
|
||||||
CountDownLatch onSeekCompletedLatch = new CountDownLatch(1);
|
CountDownLatch onSeekCompletedLatch = new CountDownLatch(1);
|
||||||
@ -494,7 +491,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
});
|
});
|
||||||
|
|
||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation()
|
||||||
.runOnMainSync(() -> simpleExoPlayer.seekTo(testSeekPosition));
|
.runOnMainSync(() -> exoPlayer.seekTo(testSeekPosition));
|
||||||
assertThat(onSeekCompletedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
assertThat(onSeekCompletedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
||||||
assertThat(seekPosition.get()).isEqualTo(testSeekPosition);
|
assertThat(seekPosition.get()).isEqualTo(testSeekPosition);
|
||||||
}
|
}
|
||||||
@ -837,7 +834,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation()
|
||||||
.runOnMainSync(() -> playerTestRule.getSimpleExoPlayer().setMediaItems(exoMediaItems));
|
.runOnMainSync(() -> playerTestRule.getExoPlayer().setMediaItems(exoMediaItems));
|
||||||
assertThat(onPlaylistChangedLatch.await(PLAYLIST_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
assertThat(onPlaylistChangedLatch.await(PLAYLIST_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -870,7 +867,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
sessionPlayerConnector.prepare();
|
sessionPlayerConnector.prepare();
|
||||||
sessionPlayerConnector.setPlaylist(playlistToSessionPlayer, /* metadata= */ null);
|
sessionPlayerConnector.setPlaylist(playlistToSessionPlayer, /* metadata= */ null);
|
||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation()
|
||||||
.runOnMainSync(() -> playerTestRule.getSimpleExoPlayer().setMediaItems(exoMediaItems));
|
.runOnMainSync(() -> playerTestRule.getExoPlayer().setMediaItems(exoMediaItems));
|
||||||
assertThat(onPlaylistChangedLatch.await(PLAYLIST_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
assertThat(onPlaylistChangedLatch.await(PLAYLIST_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1055,7 +1052,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
@LargeTest
|
@LargeTest
|
||||||
public void play_byUnderlyingPlayer_notifiesOnPlayerStateChanges() throws Exception {
|
public void play_byUnderlyingPlayer_notifiesOnPlayerStateChanges() throws Exception {
|
||||||
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
|
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
|
||||||
SimpleExoPlayer simpleExoPlayer = playerTestRule.getSimpleExoPlayer();
|
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
|
||||||
|
|
||||||
CountDownLatch onPlayingLatch = new CountDownLatch(1);
|
CountDownLatch onPlayingLatch = new CountDownLatch(1);
|
||||||
sessionPlayerConnector.registerPlayerCallback(
|
sessionPlayerConnector.registerPlayerCallback(
|
||||||
@ -1071,7 +1068,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
|
|
||||||
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation()
|
||||||
.runOnMainSync(() -> simpleExoPlayer.setPlayWhenReady(true));
|
.runOnMainSync(() -> exoPlayer.setPlayWhenReady(true));
|
||||||
|
|
||||||
assertThat(onPlayingLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
assertThat(onPlayingLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
||||||
}
|
}
|
||||||
@ -1090,7 +1087,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
@LargeTest
|
@LargeTest
|
||||||
public void pause_byUnderlyingPlayer_notifiesOnPlayerStateChanges() throws Exception {
|
public void pause_byUnderlyingPlayer_notifiesOnPlayerStateChanges() throws Exception {
|
||||||
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
|
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
|
||||||
SimpleExoPlayer simpleExoPlayer = playerTestRule.getSimpleExoPlayer();
|
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
|
||||||
|
|
||||||
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
|
||||||
|
|
||||||
@ -1107,7 +1104,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
});
|
});
|
||||||
assertPlayerResultSuccess(sessionPlayerConnector.play());
|
assertPlayerResultSuccess(sessionPlayerConnector.play());
|
||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation()
|
||||||
.runOnMainSync(() -> simpleExoPlayer.setPlayWhenReady(false));
|
.runOnMainSync(() -> exoPlayer.setPlayWhenReady(false));
|
||||||
|
|
||||||
assertThat(onPausedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
assertThat(onPausedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
|
||||||
}
|
}
|
||||||
@ -1116,7 +1113,7 @@ public class SessionPlayerConnectorTest {
|
|||||||
@LargeTest
|
@LargeTest
|
||||||
public void pause_byUnderlyingPlayerInListener_changesToPlayerStatePaused() throws Exception {
|
public void pause_byUnderlyingPlayerInListener_changesToPlayerStatePaused() throws Exception {
|
||||||
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
|
TestUtils.loadResource(R.raw.audio, sessionPlayerConnector);
|
||||||
SimpleExoPlayer simpleExoPlayer = playerTestRule.getSimpleExoPlayer();
|
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
|
||||||
|
|
||||||
CountDownLatch playerStateChangesLatch = new CountDownLatch(3);
|
CountDownLatch playerStateChangesLatch = new CountDownLatch(3);
|
||||||
CopyOnWriteArrayList<Integer> playerStateChanges = new CopyOnWriteArrayList<>();
|
CopyOnWriteArrayList<Integer> playerStateChanges = new CopyOnWriteArrayList<>();
|
||||||
@ -1134,12 +1131,12 @@ public class SessionPlayerConnectorTest {
|
|||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation()
|
||||||
.runOnMainSync(
|
.runOnMainSync(
|
||||||
() ->
|
() ->
|
||||||
simpleExoPlayer.addListener(
|
exoPlayer.addListener(
|
||||||
new Player.Listener() {
|
new Player.Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void onPlayWhenReadyChanged(boolean playWhenReady, int reason) {
|
public void onPlayWhenReadyChanged(boolean playWhenReady, int reason) {
|
||||||
if (playWhenReady) {
|
if (playWhenReady) {
|
||||||
simpleExoPlayer.setPlayWhenReady(false);
|
exoPlayer.setPlayWhenReady(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@ -1292,11 +1289,10 @@ public class SessionPlayerConnectorTest {
|
|||||||
InstrumentationRegistry.getInstrumentation()
|
InstrumentationRegistry.getInstrumentation()
|
||||||
.runOnMainSync(
|
.runOnMainSync(
|
||||||
() -> {
|
() -> {
|
||||||
SimpleExoPlayer simpleExoPlayer = playerTestRule.getSimpleExoPlayer();
|
ExoPlayer exoPlayer = playerTestRule.getExoPlayer();
|
||||||
simpleExoPlayer.setMediaItems(exoMediaItems);
|
exoPlayer.setMediaItems(exoMediaItems);
|
||||||
|
|
||||||
try (SessionPlayerConnector sessionPlayer =
|
try (SessionPlayerConnector sessionPlayer = new SessionPlayerConnector(exoPlayer)) {
|
||||||
new SessionPlayerConnector(simpleExoPlayer)) {
|
|
||||||
List<MediaItem> playlist = sessionPlayer.getPlaylist();
|
List<MediaItem> playlist = sessionPlayer.getPlaylist();
|
||||||
playlistFromSessionPlayer.set(playlist);
|
playlistFromSessionPlayer.set(playlist);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user