MediaLibraryService.MediaLibrarySession.Callback


interface MediaLibraryService.MediaLibrarySession.Callback : MediaSession.Callback


An extended MediaSession.Callback for the MediaLibrarySession.

When you return LibraryResult with media items, each item must have valid mediaId and specify isBrowsable and isPlayable in its mediaMetadata.

Summary

Public functions

ListenableFuture<LibraryResult<ImmutableList<MediaItem!>!>!>!
onGetChildren(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    parentId: String!,
    page: @IntRange(from = 0) Int,
    pageSize: @IntRange(from = 1) Int,
    params: MediaLibraryService.LibraryParams?
)

Called when a MediaBrowser requests the child media items of the given parent id by getChildren.

ListenableFuture<LibraryResult<MediaItem!>!>!
onGetItem(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    mediaId: String!
)

Called when a MediaBrowser requests a MediaItem by getItem.

ListenableFuture<LibraryResult<MediaItem!>!>!

Called when a MediaBrowser requests the root MediaItem by getLibraryRoot.

ListenableFuture<LibraryResult<ImmutableList<MediaItem!>!>!>!
onGetSearchResult(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    query: String!,
    page: @IntRange(from = 0) Int,
    pageSize: @IntRange(from = 1) Int,
    params: MediaLibraryService.LibraryParams?
)

Called when a MediaBrowser requests a search result with getSearchResult.

ListenableFuture<LibraryResult<Void!>!>!
onSearch(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    query: String!,
    params: MediaLibraryService.LibraryParams?
)

Called when a MediaBrowser requests a search with search.

ListenableFuture<LibraryResult<Void!>!>!
onSubscribe(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    parentId: String!,
    params: MediaLibraryService.LibraryParams?
)

Called when a MediaBrowser subscribes to the given parent id by subscribe.

ListenableFuture<LibraryResult<Void!>!>!
onUnsubscribe(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    parentId: String!
)

Called when a MediaBrowser unsubscribes from the given parent ID by unsubscribe.

Inherited functions

From androidx.media3.session.MediaSession.Callback
ListenableFuture<(Mutable)List<MediaItem!>!>!
onAddMediaItems(
    mediaSession: MediaSession!,
    controller: MediaSession.ControllerInfo!,
    mediaItems: (Mutable)List<MediaItem!>!
)

Called when a controller requested to add new media items to the playlist via one of the Player.addMediaItem(s) methods.

MediaSession.ConnectionResult!

Called when a controller is about to connect to this session.

ListenableFuture<SessionResult!>!
onCustomCommand(
    session: MediaSession!,
    controller: MediaSession.ControllerInfo!,
    customCommand: SessionCommand!,
    args: Bundle!
)

Called when a controller sent a custom command through sendCustomCommand.

Unit
onDisconnected(
    session: MediaSession!,
    controller: MediaSession.ControllerInfo!
)

Called when a controller is disconnected.

Boolean
@UnstableApi
onMediaButtonEvent(
    session: MediaSession!,
    controllerInfo: MediaSession.ControllerInfo!,
    intent: Intent!
)

Called when a media button event has been received by the session.

ListenableFuture<MediaSession.MediaItemsWithStartPosition!>!
@UnstableApi
onPlaybackResumption(
    mediaSession: MediaSession!,
    controller: MediaSession.ControllerInfo!
)

Returns the playlist with which the player should be prepared when a controller requests to play without a current MediaItem.

Int
@SessionResult.Code
onPlayerCommandRequest(
    session: MediaSession!,
    controller: MediaSession.ControllerInfo!,
    @Player.Command playerCommand: Int
)

This function is deprecated.

Modify the available commands of the player or use setAvailableCommands instead.

Unit
@UnstableApi
onPlayerInteractionFinished(
    session: MediaSession!,
    controllerInfo: MediaSession.ControllerInfo!,
    playerCommands: Player.Commands!
)

Called after all concurrent interactions with the session player from a controller have finished.

Unit
onPostConnect(
    session: MediaSession!,
    controller: MediaSession.ControllerInfo!
)

Called immediately after a controller is connected.

ListenableFuture<MediaSession.MediaItemsWithStartPosition!>!
@UnstableApi
onSetMediaItems(
    mediaSession: MediaSession!,
    controller: MediaSession.ControllerInfo!,
    mediaItems: (Mutable)List<MediaItem!>!,
    startIndex: Int,
    startPositionMs: Long
)

Called when a controller requested to set media items to the playlist via one of the Player.setMediaItem(s) methods.

ListenableFuture<SessionResult!>!
onSetRating(
    session: MediaSession!,
    controller: MediaSession.ControllerInfo!,
    rating: Rating!
)

Called when a controller requested to set a rating for the current media item for the current user by setRating.

ListenableFuture<SessionResult!>!
onSetRating(
    session: MediaSession!,
    controller: MediaSession.ControllerInfo!,
    mediaId: String!,
    rating: Rating!
)

Called when a controller requested to set a rating to a media for the current user by setRating.

Public functions

onGetChildren

fun onGetChildren(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    parentId: String!,
    page: @IntRange(from = 0) Int,
    pageSize: @IntRange(from = 1) Int,
    params: MediaLibraryService.LibraryParams?
): ListenableFuture<LibraryResult<ImmutableList<MediaItem!>!>!>!

Called when a MediaBrowser requests the child media items of the given parent id by getChildren.

Return a ListenableFuture to send a LibraryResult back to the browser asynchronously. You can also return a LibraryResult directly by using Guava's immediateFuture.

The params should be the same as the given params.

To allow getting the children, return a LibraryResult with RESULT_SUCCESS and a list of media items. Return an empty list for no children rather than using error codes.

Parameters
session: MediaLibraryService.MediaLibrarySession!

The session for this event

browser: MediaSession.ControllerInfo!

The browser information.

parentId: String!

The non-empty parent id.

page: @IntRange(from = 0) Int

The page number to get the paginated result starting from 0.

pageSize: @IntRange(from = 1) Int

The page size to get the paginated result. Will be greater than 0.

params: MediaLibraryService.LibraryParams?

The optional parameters passed by the browser.

Returns
ListenableFuture<LibraryResult<ImmutableList<MediaItem!>!>!>!

A pending result that will be resolved with a list of media items.

onGetItem

fun onGetItem(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    mediaId: String!
): ListenableFuture<LibraryResult<MediaItem!>!>!

Called when a MediaBrowser requests a MediaItem by getItem.

Return a ListenableFuture to send a LibraryResult back to the browser asynchronously. You can also return a LibraryResult directly by using Guava's immediateFuture.

To allow getting the item, return a LibraryResult with RESULT_SUCCESS and a MediaItem with a valid mediaId.

Parameters
session: MediaLibraryService.MediaLibrarySession!

The session for this event.

browser: MediaSession.ControllerInfo!

The browser information.

mediaId: String!

The non-empty media id of the requested item.

Returns
ListenableFuture<LibraryResult<MediaItem!>!>!

A pending result that will be resolved with a media item.

onGetLibraryRoot

fun onGetLibraryRoot(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    params: MediaLibraryService.LibraryParams?
): ListenableFuture<LibraryResult<MediaItem!>!>!

Called when a MediaBrowser requests the root MediaItem by getLibraryRoot.

Return a ListenableFuture to send a LibraryResult back to the browser asynchronously. You can also return a LibraryResult directly by using Guava's immediateFuture.

The params may differ from the given params if the session can't provide a root that matches with the params.

To allow browsing the media library, return a LibraryResult with RESULT_SUCCESS and a root MediaItem with a valid mediaId. The media id is required for the browser to get the children under the root.

Interoperability: If this callback is called because a legacy android.support.v4.media.MediaBrowserCompat has requested a androidx.media.MediaBrowserServiceCompat.BrowserRoot, then the main thread may be blocked until the returned future is done. If your service may be queried by a legacy android.support.v4.media.MediaBrowserCompat, you should ensure that the future completes quickly to avoid blocking the main thread for a long period of time.

Parameters
session: MediaLibraryService.MediaLibrarySession!

The session for this event.

browser: MediaSession.ControllerInfo!

The browser information.

params: MediaLibraryService.LibraryParams?

The optional parameters passed by the browser.

Returns
ListenableFuture<LibraryResult<MediaItem!>!>!

A pending result that will be resolved with a root media item.

onGetSearchResult

fun onGetSearchResult(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    query: String!,
    page: @IntRange(from = 0) Int,
    pageSize: @IntRange(from = 1) Int,
    params: MediaLibraryService.LibraryParams?
): ListenableFuture<LibraryResult<ImmutableList<MediaItem!>!>!>!

Called when a MediaBrowser requests a search result with getSearchResult.

Return a ListenableFuture to send a LibraryResult back to the browser asynchronously. You can also return a LibraryResult directly by using Guava's immediateFuture.

The params should be the same as the given params.

To allow getting the search result, return a LibraryResult with RESULT_SUCCESS and a list of media items. Return an empty list for no children rather than using error codes.

Typically, the query is requested through onSearch before, but it may not especially when android.support.v4.media.MediaBrowserCompat.search() is used.

Parameters
session: MediaLibraryService.MediaLibrarySession!

The session for this event.

browser: MediaSession.ControllerInfo!

The browser information.

query: String!

The non-empty search query.

page: @IntRange(from = 0) Int

The page number to get the paginated result starting from 0.

pageSize: @IntRange(from = 1) Int

The page size to get the paginated result. Will be greater than 0.

params: MediaLibraryService.LibraryParams?

The optional parameters passed by the browser.

Returns
ListenableFuture<LibraryResult<ImmutableList<MediaItem!>!>!>!

A pending result that will be resolved with a list of media items.

onSearch

fun onSearch(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    query: String!,
    params: MediaLibraryService.LibraryParams?
): ListenableFuture<LibraryResult<Void!>!>!

Called when a MediaBrowser requests a search with search.

Return a ListenableFuture to send a LibraryResult back to the browser asynchronously. You can also return a LibraryResult directly by using Guava's immediateFuture.

The params should be the same as the given params.

Return LibraryResult with a result code for the search and notify the number of search result (media items) through notifySearchResultChanged. will ask the search result afterwards through onGetSearchResult.

Parameters
session: MediaLibraryService.MediaLibrarySession!

The session for this event.

browser: MediaSession.ControllerInfo!

The browser information.

query: String!

The non-empty search query.

params: MediaLibraryService.LibraryParams?

The optional parameters passed by the browser.

Returns
ListenableFuture<LibraryResult<Void!>!>!

A pending result that will be resolved with a result code.

onSubscribe

fun onSubscribe(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    parentId: String!,
    params: MediaLibraryService.LibraryParams?
): ListenableFuture<LibraryResult<Void!>!>!

Called when a MediaBrowser subscribes to the given parent id by subscribe.

See getSubscribedControllers also.

By default, the library calls onGetItem for the parentId that the browser requests to subscribe to. If onGetItem returns RESULT_SUCCESS with a browsable item, the subscription is accepted and notifyChildrenChanged is immediately called with an itemCount of MAX_VALUE. In all other cases, the subscription is rejected and a result value different to RESULT_SUCCESS is returned from this method.

To implement a different behavior, an app can safely override this method without calling super. Return a result code RESULT_SUCCESS to accept the subscription, or return a result different to RESULT_SUCCESS to prevent controllers from subscribing.

Return a ListenableFuture to send a LibraryResult back to the browser asynchronously. You can also return a LibraryResult directly by using Guava's immediateFuture.

The params returned to the caller should be the same as the params passed into this method.

Interoperability: This will be called by android.support.v4.media.MediaBrowserCompat.subscribe(), but won't be called by subscribe.

Parameters
session: MediaLibraryService.MediaLibrarySession!

The session for this event.

browser: MediaSession.ControllerInfo!

The browser information.

parentId: String!

The non-empty parent id.

params: MediaLibraryService.LibraryParams?

The optional parameters passed by the browser.

Returns
ListenableFuture<LibraryResult<Void!>!>!

A pending result that will be resolved with a result code.

onUnsubscribe

fun onUnsubscribe(
    session: MediaLibraryService.MediaLibrarySession!,
    browser: MediaSession.ControllerInfo!,
    parentId: String!
): ListenableFuture<LibraryResult<Void!>!>!

Called when a MediaBrowser unsubscribes from the given parent ID by unsubscribe.

Return a ListenableFuture to send a LibraryResult back to the browser asynchronously. You can also return a LibraryResult directly by using Guava's immediateFuture.

Apps normally don't need to implement this method, because the library maintains the subscribed controllers internally and an app can use getSubscribedControllers to get subscribed controllers for which to call notifyChildrenChanged.

Interoperability: This will be called by android.support.v4.media.MediaBrowserCompat.unsubscribe(), but won't be called by unsubscribe.

Parameters
session: MediaLibraryService.MediaLibrarySession!

The session for this event.

browser: MediaSession.ControllerInfo!

The browser information.

parentId: String!

The non-empty parent id.

Returns
ListenableFuture<LibraryResult<Void!>!>!

A pending result that will be resolved with a result code.