From f2866a49426c06ae3624e42396e8a65bbbfd97b4 Mon Sep 17 00:00:00 2001 From: jaewan Date: Fri, 7 Aug 2020 15:51:51 +0100 Subject: [PATCH] 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 --- .../media2/SessionPlayerConnectorTest.java | 20 ++++++++---- .../ext/media2/SessionPlayerConnector.java | 31 ++++++++++++++++--- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/extensions/media2/src/androidTest/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnectorTest.java b/extensions/media2/src/androidTest/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnectorTest.java index c80c86271b..3ebd92c7db 100644 --- a/extensions/media2/src/androidTest/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnectorTest.java +++ b/extensions/media2/src/androidTest/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnectorTest.java @@ -434,7 +434,9 @@ public class SessionPlayerConnectorTest { // prepare() will be pending until readAllowed is countDowned. sessionPlayerConnector.prepare(); - AtomicLong seekPosition = new AtomicLong(); + CopyOnWriteArrayList 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); - onSeekCompletedLatch.countDown(); + positionChanges.add(position); + if (position == testFinalSeekToPosition) { + onSeekCompletedLatch.countDown(); + } } }); - ListenableFuture seekFuture1 = sessionPlayerConnector.seekTo(3000); - ListenableFuture seekFuture2 = sessionPlayerConnector.seekTo(2000); + ListenableFuture seekFuture1 = + sessionPlayerConnector.seekTo(testIntermediateSeekToPosition1); + ListenableFuture seekFuture2 = + sessionPlayerConnector.seekTo(testIntermediateSeekToPosition2); ListenableFuture 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 diff --git a/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnector.java b/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnector.java index b5f8648e6a..f97dbc79e2 100644 --- a/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnector.java +++ b/extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/SessionPlayerConnector.java @@ -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 runPlayerCallableBlocking(Callable 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