MediaLibraryService.MediaLibrarySession


class MediaLibraryService.MediaLibrarySession : MediaSession


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

Backward compatibility with legacy media browser APIs

A library session supports connection from both MediaBrowser and , but the ControllerInfo may not be precise. Here are the details.

Summary when controller info isn't precise
SDK version for legacy browser for legacy browser
SDK_INT < 21 Actual package name via getPackageName Actual UID
21 <= SDK_INT < 28 for onConnect and onGetLibraryRoot Actual package name via getPackageName Actual UID
21 <= SDK_INT < 28 for other callbacks LEGACY_CONTROLLER Negative value
28 <= SDK_INT Actual package name via getPackageName Actual UID

Summary

Public functions

ImmutableList<MediaSession.ControllerInfo!>!

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

Unit
notifyChildrenChanged(
    parentId: String!,
    itemCount: @IntRange(from = 0) Int,
    params: MediaLibraryService.LibraryParams?
)

Notifies all browsers that are subscribing to the parent of the change to its children regardless of the params given by onSubscribe.

Unit
notifyChildrenChanged(
    browser: MediaSession.ControllerInfo!,
    parentId: String!,
    itemCount: @IntRange(from = 0) Int,
    params: MediaLibraryService.LibraryParams?
)

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

Unit
notifySearchResultChanged(
    browser: MediaSession.ControllerInfo!,
    query: String!,
    itemCount: @IntRange(from = 0) Int,
    params: MediaLibraryService.LibraryParams?
)

Notifies a browser of a change to the search result.

Inherited functions

From androidx.media3.session.MediaSession
Unit

Broadcasts a custom command to all connected controllers.

BitmapLoader!

Returns the BitmapLoader.

(Mutable)List<MediaSession.ControllerInfo!>!

Returns the list of connected controllers.

MediaSession.ControllerInfo?

Returns the ControllerInfo for the controller that sent the current request for a Player method.

ImmutableList<CommandButton!>!

Returns the custom layout of the session.

String!

Returns the session ID.

MediaSession.ControllerInfo?

Returns the ControllerInfo of the media notification controller.

Player!

Returns the underlying Player.

PendingIntent?

Returns the PendingIntent to launch the session activity or null if not set.

MediaSessionCompat.Token!

Returns the MediaSessionCompat.Token of the MediaSessionCompat created internally by this session.

Bundle!

Returns the session extras.

Boolean

Returns whether a play button is shown if playback is suppressed.

SessionToken!

Returns the SessionToken for creating MediaController.

Boolean

Returns whether the given ControllerInfo belongs to an Android Auto companion app controller.

Boolean

Returns whether the given ControllerInfo belongs to an Automotive OS controller.

Boolean

Returns whether the given media controller info belongs to the media notification controller.

Unit

Releases the session and disconnects all connected controllers.

ListenableFuture<SessionResult!>!
sendCustomCommand(
    controller: MediaSession.ControllerInfo!,
    command: SessionCommand!,
    args: Bundle!
)

Sends a custom command to a specific controller.

Unit
setAvailableCommands(
    controller: MediaSession.ControllerInfo!,
    sessionCommands: SessionCommands!,
    playerCommands: Player.Commands!
)

Sets the new available commands for the controller.

Unit

Sets the custom layout that can initially be set when building the session.

ListenableFuture<SessionResult!>!

Sets the custom layout for the given Media3 controller.

Unit
setPlayer(player: Player!)

Sets the underlying Player for this session to dispatch incoming events to.

Unit
@UnstableApi
setSessionActivity(activityPendingIntent: PendingIntent!)

Updates the session activity that was set when building the session.

Unit
setSessionExtras(sessionExtras: Bundle!)

Sets the session extras and sends them to connected controllers.

Unit
setSessionExtras(
    controller: MediaSession.ControllerInfo!,
    sessionExtras: Bundle!
)

Sends the session extras to the connected controller.

Public functions

getSubscribedControllers

@UnstableApi
fun getSubscribedControllers(mediaId: String!): ImmutableList<MediaSession.ControllerInfo!>!

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

Use the returned controller infos to call notifyChildrenChanged 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 notifyChildrenChanged for a controller that didn't subscribe to the media ID results in a no-op.

Parameters
mediaId: String!

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

Returns
ImmutableList<MediaSession.ControllerInfo!>!

A list with the subscribed controllers, may be empty.

notifyChildrenChanged

fun notifyChildrenChanged(
    parentId: String!,
    itemCount: @IntRange(from = 0) Int,
    params: MediaLibraryService.LibraryParams?
): Unit

Notifies all browsers that are subscribing to the parent of the change to its children regardless of the params given by onSubscribe.

Parameters
parentId: String!

The non-empty id of the parent with changes to its children.

itemCount: @IntRange(from = 0) Int

The number of children.

params: MediaLibraryService.LibraryParams?

The optional parameters.

notifyChildrenChanged

fun notifyChildrenChanged(
    browser: MediaSession.ControllerInfo!,
    parentId: String!,
    itemCount: @IntRange(from = 0) Int,
    params: MediaLibraryService.LibraryParams?
): Unit

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: MediaSession.ControllerInfo!

The browser to notify.

parentId: String!

The non-empty id of the parent with changes to its children.

itemCount: @IntRange(from = 0) Int

The number of children, or MAX_VALUE if unknown.

params: MediaLibraryService.LibraryParams?

The parameters given by onSubscribe.

notifySearchResultChanged

fun notifySearchResultChanged(
    browser: MediaSession.ControllerInfo!,
    query: String!,
    itemCount: @IntRange(from = 0) Int,
    params: MediaLibraryService.LibraryParams?
): Unit

Notifies a browser of a change to the search result.

Parameters
browser: MediaSession.ControllerInfo!

The browser to notify.

query: String!

The non-empty search query given by onSearch.

itemCount: @IntRange(from = 0) Int

The number of items that have been found in the search.

params: MediaLibraryService.LibraryParams?

The parameters given by onSearch.