From ff9585f153ce48297e9a9fb85743c511574aa903 Mon Sep 17 00:00:00 2001 From: bachinger Date: Mon, 22 Nov 2021 11:51:56 +0000 Subject: [PATCH] Provide an opt-out from clearing media items on stop After removing the deprecated call to player.stop(/* reset= */ true) and instead using two calls to the player, overridding stop in a ForwardingPlayer does not help to avoid clearing the player. To remove the deprecation we need an option so that users who want not to clear the player have a way to do so. PiperOrigin-RevId: 411518090 --- RELEASENOTES.md | 5 +++++ .../ext/mediasession/MediaSessionConnector.java | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 704d6f54bf..3b34c286c7 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -14,6 +14,11 @@ * HLS: * Support key-frame accurate seeking in HLS ([#2882](https://github.com/google/ExoPlayer/issues/2882)). + * Correctly populate `Format.label` for audio only HLS streams + ([#9608](https://github.com/google/ExoPlayer/issues/9608)). +* MediaSession extension + * Remove deprecated call to `onStop(/* reset= */ true)` and provide an + opt-out flag for apps that don't want to clear the playlist on stop. ### 2.16.1 (2021-11-18) diff --git a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java index 751a850fbe..85b141eba8 100644 --- a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java +++ b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java @@ -466,6 +466,7 @@ public final class MediaSessionConnector { private long enabledPlaybackActions; private boolean metadataDeduplicationEnabled; private boolean dispatchUnsupportedActionsEnabled; + private boolean clearMediaItemsOnStop; /** * Creates an instance. @@ -486,6 +487,7 @@ public final class MediaSessionConnector { enabledPlaybackActions = DEFAULT_PLAYBACK_ACTIONS; mediaSession.setFlags(BASE_MEDIA_SESSION_FLAGS); mediaSession.setCallback(componentListener, new Handler(looper)); + clearMediaItemsOnStop = true; } /** @@ -699,6 +701,14 @@ public final class MediaSessionConnector { this.dispatchUnsupportedActionsEnabled = dispatchUnsupportedActionsEnabled; } + /** + * Sets whether media items are cleared from the playlist when a client sends a {@link + * MediaControllerCompat.TransportControls#stop()} command. + */ + public void setClearMediaItemsOnStop(boolean clearMediaItemsOnStop) { + this.clearMediaItemsOnStop = clearMediaItemsOnStop; + } + /** * Sets whether {@link MediaMetadataProvider#sameAs(MediaMetadataCompat, MediaMetadataCompat)} * should be consulted before calling {@link MediaSessionCompat#setMetadata(MediaMetadataCompat)}. @@ -1208,7 +1218,10 @@ public final class MediaSessionConnector { @Override public void onStop() { if (canDispatchPlaybackAction(PlaybackStateCompat.ACTION_STOP)) { - player.stop(/* reset= */ true); + player.stop(); + if (clearMediaItemsOnStop) { + player.clearMediaItems(); + } } }