Notify current media item to legacy controllers

This is the workaround for b/159147455.

The issue will be fixed in media2-session 1.1.0-stable, but we'd
better to have workaround until it's ready.

PiperOrigin-RevId: 325434543
This commit is contained in:
jaewan 2020-08-07 15:51:51 +01:00 committed by kim-vde
parent 7f6940fb8b
commit f2866a4942
2 changed files with 41 additions and 10 deletions

View File

@ -434,7 +434,9 @@ public class SessionPlayerConnectorTest {
// prepare() will be pending until readAllowed is countDowned.
sessionPlayerConnector.prepare();
AtomicLong seekPosition = new AtomicLong();
CopyOnWriteArrayList<Long> positionChanges = new CopyOnWriteArrayList<>();
long testIntermediateSeekToPosition1 = 3000;
long testIntermediateSeekToPosition2 = 2000;
long testFinalSeekToPosition = 1000;
CountDownLatch onSeekCompletedLatch = new CountDownLatch(1);
sessionPlayerConnector.registerPlayerCallback(
@ -444,13 +446,17 @@ public class SessionPlayerConnectorTest {
public void onSeekCompleted(@NonNull SessionPlayer player, long position) {
// Do not assert here, because onSeekCompleted() can be called after the player is
// closed.
seekPosition.set(position);
positionChanges.add(position);
if (position == testFinalSeekToPosition) {
onSeekCompletedLatch.countDown();
}
}
});
ListenableFuture<PlayerResult> seekFuture1 = sessionPlayerConnector.seekTo(3000);
ListenableFuture<PlayerResult> seekFuture2 = sessionPlayerConnector.seekTo(2000);
ListenableFuture<PlayerResult> seekFuture1 =
sessionPlayerConnector.seekTo(testIntermediateSeekToPosition1);
ListenableFuture<PlayerResult> seekFuture2 =
sessionPlayerConnector.seekTo(testIntermediateSeekToPosition2);
ListenableFuture<PlayerResult> seekFuture3 =
sessionPlayerConnector.seekTo(testFinalSeekToPosition);
@ -460,7 +466,9 @@ public class SessionPlayerConnectorTest {
assertThat(seekFuture2.get().getResultCode()).isEqualTo(RESULT_INFO_SKIPPED);
assertThat(seekFuture3.get().getResultCode()).isEqualTo(RESULT_SUCCESS);
assertThat(onSeekCompletedLatch.await(PLAYBACK_COMPLETED_WAIT_TIME_MS, MILLISECONDS)).isTrue();
assertThat(seekPosition.get()).isEqualTo(testFinalSeekToPosition);
assertThat(positionChanges)
.containsNoneOf(testIntermediateSeekToPosition1, testIntermediateSeekToPosition2);
assertThat(positionChanges).contains(testFinalSeekToPosition);
}
@Test

View File

@ -578,6 +578,7 @@ public final class SessionPlayerConnector extends SessionPlayer {
if (!notifyCurrentMediaItem && !notifyCurrentPlaylist) {
return;
}
long currentPosition = getCurrentPosition();
notifySessionPlayerCallback(
callback -> {
if (notifyCurrentPlaylist) {
@ -587,7 +588,13 @@ public final class SessionPlayerConnector extends SessionPlayer {
if (notifyCurrentMediaItem) {
Assertions.checkNotNull(
currentMediaItem, "PlaylistManager#currentMediaItem() cannot be changed to null");
callback.onCurrentMediaItemChanged(SessionPlayerConnector.this, currentMediaItem);
// Workaround for MediaSession's issue that current media item change isn't propagated
// to the legacy controllers.
// TODO(b/160846312): Remove this workaround with media2 1.1.0-stable.
callback.onSeekCompleted(SessionPlayerConnector.this, currentPosition);
}
});
}
@ -598,9 +605,16 @@ public final class SessionPlayerConnector extends SessionPlayer {
return;
}
this.currentMediaItem = currentMediaItem;
long currentPosition = getCurrentPosition();
notifySessionPlayerCallback(
callback ->
callback.onCurrentMediaItemChanged(SessionPlayerConnector.this, currentMediaItem));
callback -> {
callback.onCurrentMediaItemChanged(SessionPlayerConnector.this, currentMediaItem);
// Workaround for MediaSession's issue that current media item change isn't propagated
// to the legacy controllers.
// TODO(b/160846312): Remove this workaround with media2 1.1.0-stable.
callback.onSeekCompleted(SessionPlayerConnector.this, currentPosition);
});
}
private <T> T runPlayerCallableBlocking(Callable<T> callable) {
@ -689,8 +703,9 @@ public final class SessionPlayerConnector extends SessionPlayer {
@Override
public void onSeekCompleted() {
long currentPosition = getCurrentPosition();
notifySessionPlayerCallback(
callback -> callback.onSeekCompleted(SessionPlayerConnector.this, getCurrentPosition()));
callback -> callback.onSeekCompleted(SessionPlayerConnector.this, currentPosition));
}
@Override
@ -720,8 +735,16 @@ public final class SessionPlayerConnector extends SessionPlayer {
return;
}
currentMediaItem = mediaItem;
long currentPosition = getCurrentPosition();
notifySessionPlayerCallback(
callback -> callback.onCurrentMediaItemChanged(SessionPlayerConnector.this, mediaItem));
callback -> {
callback.onCurrentMediaItemChanged(SessionPlayerConnector.this, mediaItem);
// Workaround for MediaSession's issue that current media item change isn't propagated
// to the legacy controllers.
// TODO(b/160846312): Remove this workaround with media2 1.1.0-stable.
callback.onSeekCompleted(SessionPlayerConnector.this, currentPosition);
});
}
@Override