From bb11e0286eaa49b4178dfa29ebaea5dafba8fc39 Mon Sep 17 00:00:00 2001 From: michaelkatz Date: Fri, 20 Jan 2023 14:25:09 +0000 Subject: [PATCH] Add onSetMediaItems listener with access to start index and position Added onSetMediaItems callback listener to allow the session to modify/set MediaItem list, starting index and position before call to Player.setMediaItem(s). Added conditional check in MediaSessionStub.setMediaItem methods to only call player.setMediaItem rather than setMediaItems if player does not support COMMAND_CHANGE_MEDIA_ITEMS PiperOrigin-RevId: 503427927 --- RELEASENOTES.md | 3 + .../androidx/media3/session/MediaSession.java | 168 +++++++++++++++- .../media3/session/MediaSessionImpl.java | 8 + .../session/MediaSessionLegacyStub.java | 19 +- .../media3/session/MediaSessionStub.java | 188 ++++++++++++------ .../androidx/media3/session/MediaUtils.java | 27 +++ .../session/MediaSessionCallbackTest.java | 173 +++++++++++++++- ...CallbackWithMediaControllerCompatTest.java | 118 +++++++++-- .../session/MediaSessionPlayerTest.java | 16 +- 9 files changed, 621 insertions(+), 99 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 26435e3bf7..b283432421 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -52,6 +52,9 @@ Release notes * Add the media session as an argument of `getMediaButtons()` of the `DefaultMediaNotificationProvider` and use immutable lists for clarity ([#216](https://github.com/androidx/media/issues/216)). + * Add `onSetMediaItems` callback listener to provide means to modify/set + `MediaItem` list, starting index and position by session before setting + onto Player ([#156](https://github.com/androidx/media/issues/156)). * Metadata: * Parse multiple null-separated values from ID3 frames, as permitted by ID3 v2.4. diff --git a/libraries/session/src/main/java/androidx/media3/session/MediaSession.java b/libraries/session/src/main/java/androidx/media3/session/MediaSession.java index c7ca83d86a..888cb26242 100644 --- a/libraries/session/src/main/java/androidx/media3/session/MediaSession.java +++ b/libraries/session/src/main/java/androidx/media3/session/MediaSession.java @@ -59,6 +59,8 @@ import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.Util; import androidx.media3.session.MediaLibraryService.LibraryParams; import com.google.common.base.Objects; +import com.google.common.collect.ImmutableList; +import com.google.common.primitives.Longs; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import java.util.HashMap; @@ -1056,13 +1058,13 @@ public class MediaSession { /** * Called when a controller requested to add new {@linkplain MediaItem media items} to the - * playlist via one of the {@code Player.addMediaItem(s)} or {@code Player.setMediaItem(s)} - * methods. + * playlist via one of the {@code Player.addMediaItem(s)} methods. Unless overriden, {@link + * Callback#onSetMediaItems} will direct {@code Player.setMediaItem(s)} to this method as well. * - *

This callback is also called when an app is using a legacy {@link - * MediaControllerCompat.TransportControls} to prepare or play media (for instance when browsing - * the catalogue and then selecting an item for preparation from Android Auto that is using the - * legacy Media1 library). + *

In addition, unless {@link Callback#onSetMediaItems} is overridden, this callback is also + * called when an app is using a legacy {@link MediaControllerCompat.TransportControls} to + * prepare or play media (for instance when browsing the catalogue and then selecting an item + * for preparation from Android Auto that is using the legacy Media1 library). * *

Note that the requested {@linkplain MediaItem media items} don't have a {@link * MediaItem.LocalConfiguration} (for example, a URI) and need to be updated to make them @@ -1075,8 +1077,8 @@ public class MediaSession { * the {@link MediaItem media items} have been resolved, the session will call {@link * Player#setMediaItems} or {@link Player#addMediaItems} as requested. * - *

Interoperability: This method will be called in response to the following {@link - * MediaControllerCompat} methods: + *

Interoperability: This method will be called, unless {@link Callback#onSetMediaItems} is + * overridden, in response to the following {@link MediaControllerCompat} methods: * *