public final class

MediaLibraryService.MediaLibrarySession

extends MediaSession

 java.lang.Object

androidx.media3.session.MediaSession

↳androidx.media3.session.MediaLibraryService.MediaLibrarySession

Overview

An extended MediaSession for the MediaLibraryService. Build an instance with MediaLibraryService.MediaLibrarySession.Builder and return it from MediaSessionService.

Backward compatibility with legacy media browser APIs

A library session supports connection from both MediaBrowser and android.support.v4.media.MediaBrowserCompat, but the MediaSession.ControllerInfo may not be precise. Here are the details.

Summary when controller info isn't precise
SDK version MediaSession.ControllerInfo.getPackageName()
for legacy browser
MediaSession.ControllerInfo.getUid()
for legacy browser
SDK_INT < 28
for onConnect
and onGetLibraryRoot
Actual package name via Actual UID
SDK_INT < 28
for other callbacks
MediaSession.ControllerInfo.LEGACY_CONTROLLER_PACKAGE_NAME Negative value
28 <= SDK_INT Actual package name via Actual UID

Summary

Fields
public static final intLIBRARY_ERROR_REPLICATION_MODE_FATAL

Session errors returned by the MediaLibraryService.MediaLibrarySession.Callback as part of a LibraryResult are replicated to the platform session as a fatal error.

public static final intLIBRARY_ERROR_REPLICATION_MODE_NON_FATAL

Session errors returned by the MediaLibraryService.MediaLibrarySession.Callback as part of a LibraryResult are replicated to the platform session as a non-fatal error.

public static final intLIBRARY_ERROR_REPLICATION_MODE_NONE

No library service errors are replicated.

Methods
public voidclearReplicatedLibraryError()

Clears the replicated library error in the platform session that was set when a LibraryResult with an error result code was returned by the MediaLibraryService.MediaLibrarySession.Callback that is replicated and if legacy session error replication is not turned off.

public <any>getSubscribedControllers(java.lang.String mediaId)

Returns the controllers that are currently subscribed to the given mediaId.

public voidnotifyChildrenChanged(MediaSession.ControllerInfo browser, java.lang.String parentId, int itemCount, MediaLibraryService.LibraryParams params)

Notifies a browser that is subscribed to a browsable media item that the children of the item have changed.

public voidnotifyChildrenChanged(java.lang.String parentId, int itemCount, MediaLibraryService.LibraryParams params)

Notifies all browsers that are subscribing to the parent of the change to its children regardless of the params given by MediaLibraryService.MediaLibrarySession.Callback.onSubscribe(MediaLibraryService.MediaLibrarySession, MediaSession.ControllerInfo, String, MediaLibraryService.LibraryParams).

public voidnotifySearchResultChanged(MediaSession.ControllerInfo browser, java.lang.String query, int itemCount, MediaLibraryService.LibraryParams params)

Notifies a browser of a change to the search result.

from MediaSessionbroadcastCustomCommand, getBitmapLoader, getConnectedControllers, getControllerForCurrentRequest, getCustomLayout, getId, getMediaNotificationControllerInfo, getPlatformToken, getPlayer, getSessionActivity, getSessionCompatToken, getSessionExtras, getShowPlayButtonIfPlaybackIsSuppressed, getToken, isAutoCompanionController, isAutomotiveController, isMediaNotificationController, release, sendCustomCommand, sendError, sendError, setAvailableCommands, setCustomLayout, setCustomLayout, setPlayer, setSessionActivity, setSessionActivity, setSessionExtras, setSessionExtras
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int LIBRARY_ERROR_REPLICATION_MODE_NONE

No library service errors are replicated.

public static final int LIBRARY_ERROR_REPLICATION_MODE_FATAL

Session errors returned by the MediaLibraryService.MediaLibrarySession.Callback as part of a LibraryResult are replicated to the platform session as a fatal error.

public static final int LIBRARY_ERROR_REPLICATION_MODE_NON_FATAL

Session errors returned by the MediaLibraryService.MediaLibrarySession.Callback as part of a LibraryResult are replicated to the platform session as a non-fatal error.

Methods

public <any> getSubscribedControllers(java.lang.String mediaId)

Returns the controllers that are currently subscribed to the given mediaId.

Use the returned controller infos to call MediaLibraryService.MediaLibrarySession.notifyChildrenChanged(MediaSession.ControllerInfo, String, int, MediaLibraryService.LibraryParams) in case the children of the media item with the given media ID have changed and the connected controller should fetch them again.

Note that calling MediaLibraryService.MediaLibrarySession.notifyChildrenChanged(MediaSession.ControllerInfo, String, int, MediaLibraryService.LibraryParams) for a controller that didn't subscribe to the media ID results in a no-op.

Parameters:

mediaId: The ID of the media item for which to get subscribed controllers.

Returns:

A list with the subscribed controllers, may be empty.

public void notifyChildrenChanged(MediaSession.ControllerInfo browser, java.lang.String parentId, int itemCount, MediaLibraryService.LibraryParams params)

Notifies a browser that is subscribed to a browsable media item that the children of the item have changed. This method is also called immediately after subscribing was successful.

Parameters:

browser: The browser to notify.
parentId: The non-empty id of the parent with changes to its children.
itemCount: The number of children, or MAX_VALUE if unknown.
params: The parameters given by MediaLibraryService.MediaLibrarySession.Callback.onSubscribe(MediaLibraryService.MediaLibrarySession, MediaSession.ControllerInfo, String, MediaLibraryService.LibraryParams).

public void notifyChildrenChanged(java.lang.String parentId, int itemCount, MediaLibraryService.LibraryParams params)

Notifies all browsers that are subscribing to the parent of the change to its children regardless of the params given by MediaLibraryService.MediaLibrarySession.Callback.onSubscribe(MediaLibraryService.MediaLibrarySession, MediaSession.ControllerInfo, String, MediaLibraryService.LibraryParams).

Parameters:

parentId: The non-empty id of the parent with changes to its children.
itemCount: The number of children.
params: The optional parameters.

public void notifySearchResultChanged(MediaSession.ControllerInfo browser, java.lang.String query, int itemCount, MediaLibraryService.LibraryParams params)

Notifies a browser of a change to the search result.

Parameters:

browser: The browser to notify.
query: The non-empty search query given by MediaLibraryService.MediaLibrarySession.Callback.onSearch(MediaLibraryService.MediaLibrarySession, MediaSession.ControllerInfo, String, MediaLibraryService.LibraryParams).
itemCount: The number of items that have been found in the search.
params: The parameters given by MediaLibraryService.MediaLibrarySession.Callback.onSearch(MediaLibraryService.MediaLibrarySession, MediaSession.ControllerInfo, String, MediaLibraryService.LibraryParams).

public void clearReplicatedLibraryError()

Clears the replicated library error in the platform session that was set when a LibraryResult with an error result code was returned by the MediaLibraryService.MediaLibrarySession.Callback that is replicated and if legacy session error replication is not turned off.

Note: If a LibraryResult.RESULT_SUCCESS was returned by a method of MediaLibraryService.MediaLibrarySession.Callback that is considered for replication, the error is cleared automatically by the library.

Calling this method updates the platform session playback state in case there was a replicated error set. If no error was set, calling this method is a no-op.