MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback

Added in 1.0.0
Deprecated in 1.3.0

public class MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback extends MediaSession.SessionCallback


Callback for the MediaLibrarySession.

When you return LibraryResult with media items, items must have valid METADATA_KEY_MEDIA_ID and specify METADATA_KEY_BROWSABLE and METADATA_KEY_PLAYABLE.

Summary

Public constructors

Public methods

@NonNull LibraryResult
onGetChildren(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String parentId,
    @IntRange(from = 0) int page,
    @IntRange(from = 1) int pageSize,
    @Nullable MediaLibraryService.LibraryParams params
)

Called to get children of given parent id.

@NonNull LibraryResult

Called to get an item.

@NonNull LibraryResult

Called to get the root information for browsing by a MediaBrowser.

@NonNull LibraryResult
onGetSearchResult(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String query,
    @IntRange(from = 0) int page,
    @IntRange(from = 1) int pageSize,
    @Nullable MediaLibraryService.LibraryParams params
)

Called to get the search result.

int

Called when a controller requests search.

int

Called when a controller subscribes to the parent.

int

Called when a controller unsubscribes to the parent.

Inherited methods

From androidx.media2.session.MediaSession.SessionCallback
int
onCommandRequest(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull SessionCommand command
)

Called when a controller sent a command which will be sent directly to one of the following:

@Nullable SessionCommandGroup
onConnect(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller
)

Called when a controller is created for this session.

@Nullable MediaItem
onCreateMediaItem(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String mediaId
)

Called when a controller has sent a command with a MediaItem to add a new media item to this session.

@NonNull SessionResult
onCustomCommand(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull SessionCommand customCommand,
    @Nullable Bundle args
)

Called when a controller sent a custom command through sendCustomCommand.

void

Called when a controller is disconnected.

int
onFastForward(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller
)

Called when a controller called fastForward.

void
onPostConnect(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller
)

Called immediately after a controller is connected.

int
onRewind(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller
)

Called when a controller called rewind.

int
onSetMediaUri(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull Uri uri,
    @Nullable Bundle extras
)

Called when a controller requested to set the specific media item(s) represented by a URI through setMediaUri.

int
onSetRating(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String mediaId,
    @NonNull Rating rating
)

Called when a controller set rating of a media item through setRating.

int

Called when a controller called skipBackward.

int
onSkipForward(
    @NonNull MediaSession session,
    @NonNull MediaSession.ControllerInfo controller
)

Called when a controller called skipForward.

Public constructors

MediaLibrarySessionCallback

Added in 1.0.0
Deprecated in 1.3.0
public MediaLibrarySessionCallback()

Public methods

onGetChildren

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull LibraryResult onGetChildren(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String parentId,
    @IntRange(from = 0) int page,
    @IntRange(from = 1) int pageSize,
    @Nullable MediaLibraryService.LibraryParams params
)

Called to get children of given parent id. Return the children here for the browser.

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

Parameters
@NonNull MediaLibraryService.MediaLibrarySession session

the session for this event

@NonNull MediaSession.ControllerInfo controller

controller

@NonNull String parentId

non-empty parent id to get children

@IntRange(from = 0) int page

page number. Starts from 0.

@IntRange(from = 1) int pageSize

page size. Should be greater or equal to 1.

@Nullable MediaLibraryService.LibraryParams params

library params

Returns
@NonNull LibraryResult

a library result with a list of media item with the id. A runtime exception will be thrown if an invalid result is returned.

onGetItem

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull LibraryResult onGetItem(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String mediaId
)

Called to get an item.

To allow getting the item, return the LibraryResult with the RESULT_SUCCESS and the media item.

Parameters
@NonNull MediaLibraryService.MediaLibrarySession session

the session for this event

@NonNull MediaSession.ControllerInfo controller

controller

@NonNull String mediaId

non-empty media id of the requested item

Returns
@NonNull LibraryResult

a library result with a media item with the id. A runtime exception will be thrown if an invalid result is returned.

onGetLibraryRoot

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull LibraryResult onGetLibraryRoot(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @Nullable MediaLibraryService.LibraryParams params
)

Called to get the root information for browsing by a MediaBrowser.

To allow browsing media information, return the LibraryResult with the RESULT_SUCCESS and the root media item with the valid media id. The media id must be included for the browser to get the children under it.

Interoperability: this callback may be called on the main thread, regardless of the callback executor.

Parameters
@NonNull MediaLibraryService.MediaLibrarySession session

the session for this event

@NonNull MediaSession.ControllerInfo controller

information of the controller requesting access to browse media.

@Nullable MediaLibraryService.LibraryParams params

An optional library params of service-specific arguments to send to the media library service when connecting and retrieving the root id for browsing, or null if none.

Returns
@NonNull LibraryResult

a library result with the root media item with the id. A runtime exception will be thrown if an invalid result is returned.

onGetSearchResult

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull LibraryResult onGetSearchResult(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String query,
    @IntRange(from = 0) int page,
    @IntRange(from = 1) int pageSize,
    @Nullable MediaLibraryService.LibraryParams params
)

Called to get the search result.

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

This may be called with a query that hasn't called with onSearch, especially when search is used.

Parameters
@NonNull MediaLibraryService.MediaLibrarySession session

the session for this event

@NonNull MediaSession.ControllerInfo controller

controller

@NonNull String query

The non-empty search query which was previously sent through onSearch.

@IntRange(from = 0) int page

page number. Starts from 0.

@IntRange(from = 1) int pageSize

page size. Should be greater or equal to 1.

@Nullable MediaLibraryService.LibraryParams params

library params

Returns
@NonNull LibraryResult

a library result with a list of media item with the id. A runtime exception will be thrown if an invalid result is returned.

onSearch

Added in 1.0.0
Deprecated in 1.3.0
public int onSearch(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String query,
    @Nullable MediaLibraryService.LibraryParams params
)

Called when a controller requests search.

Return immediately with the result of the attempt to search with the query. Notify the number of search result through notifySearchResultChanged. MediaBrowser will ask the search result with the pagination later.

Parameters
@NonNull MediaLibraryService.MediaLibrarySession session

the session for this event

@NonNull MediaSession.ControllerInfo controller

controller

@NonNull String query

The non-empty search query sent from the media browser. It contains keywords separated by space.

@Nullable MediaLibraryService.LibraryParams params

library params

Returns
int

result code

onSubscribe

Added in 1.0.0
Deprecated in 1.3.0
public int onSubscribe(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String parentId,
    @Nullable MediaLibraryService.LibraryParams params
)

Called when a controller subscribes to the parent.

It's your responsibility to keep subscriptions by your own and call notifyChildrenChanged when the parent is changed until it's unsubscribed.

Interoperability: This will be called when subscribe is called. However, this won't be called when subscribe is called.

Parameters
@NonNull MediaLibraryService.MediaLibrarySession session

the session for this event

@NonNull MediaSession.ControllerInfo controller

controller

@NonNull String parentId

non-empty parent id

@Nullable MediaLibraryService.LibraryParams params

library params

Returns
int

result code

onUnsubscribe

Added in 1.0.0
Deprecated in 1.3.0
public int onUnsubscribe(
    @NonNull MediaLibraryService.MediaLibrarySession session,
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull String parentId
)

Called when a controller unsubscribes to the parent.

Interoperability: This wouldn't be called if unsubscribe is called while works well with unsubscribe.

Parameters
@NonNull MediaLibraryService.MediaLibrarySession session

the session for this event

@NonNull MediaSession.ControllerInfo controller

controller

@NonNull String parentId

non-empty parent id

Returns
int

result code