MediaSession.Callback


interface MediaSession.Callback


A callback to handle incoming commands from MediaController.

The callback methods will be called from the application thread associated with the application looper of the underlying Player.

If it's not set by setCallback, the session will accept all controllers and all incoming commands by default.

Summary

Public functions

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
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

onAddMediaItems

fun onAddMediaItems(
    mediaSession: MediaSession!,
    controller: MediaSession.ControllerInfo!,
    mediaItems: (Mutable)List<MediaItem!>!
): ListenableFuture<(Mutable)List<MediaItem!>!>!

Called when a controller requested to add new media items to the playlist via one of the Player.addMediaItem(s) methods. Unless overridden, onSetMediaItems will direct Player.setMediaItem(s) to this method as well.

In addition, unless onSetMediaItems is overridden, this callback is also called when an app is using a legacy 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).

By default, if and only if each of the provided media items has a set MediaItem.LocalConfiguration (for example, a URI), then the callback returns the list unaltered. Otherwise, the default implementation returns an .

If the requested media items don't have a , they need to be updated to make them playable by the underlying Player. Typically, this callback would be overridden with implementation that identifies the correct item by its mediaId and/or the requestMetadata.

Return a ListenableFuture with the resolved media items. You can also return the items directly by using Guava's immediateFuture. Once the media items have been resolved, the session will call setMediaItems or addMediaItems as requested.

Interoperability: This method will be called, unless onSetMediaItems is overridden, in response to the following MediaControllerCompat methods:

The values of mediaId, mediaUri, searchQuery and extras will be set to match the legacy method call. The session will call setMediaItems or addMediaItems, followed by prepare and play as appropriate once the MediaItem has been resolved.
Parameters
mediaSession: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

mediaItems: (Mutable)List<MediaItem!>!

The list of requested media items.

Returns
ListenableFuture<(Mutable)List<MediaItem!>!>!

A ListenableFuture for the list of resolved media items that are playable by the underlying Player.

onConnect

fun onConnect(session: MediaSession!, controller: MediaSession.ControllerInfo!): MediaSession.ConnectionResult!

Called when a controller is about to connect to this session. Return a result for the controller by using accept or the .

If this callback is not overridden, it allows all controllers to connect that can access the session. All session and player commands are made available and the custom layout of the session is included.

Note that the player commands in availablePlayerCommands will be intersected with the available commands of the underlying Player and the controller will only be able to call the commonly available commands.

Returning reject rejects the connection. In that case, the controller will get SecurityException when resolving the ListenableFuture returned by buildAsync.

The controller isn't connected yet, so calls to the controller (e.g. sendCustomCommand, setCustomLayout) will be ignored. Use onPostConnect for custom initialization of the controller instead.

Interoperability: If a legacy controller is connecting to the session then this callback may block the main thread, even if it's called on a different application thread. If it's possible that legacy controllers will connect to the session, you should ensure that the callback returns quickly to avoid blocking the main thread for a long period of time.

Parameters
session: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

onCustomCommand

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

Called when a controller sent a custom command through sendCustomCommand.

MediaController instances are only allowed to send a command if the command has been added to the list of available session commands in onConnect or set via setAvailableCommands.

Interoperability: This will be also called by sendCustomAction. If so, extras from sendCustomAction will be considered as args and the custom command will have nullcustomExtras.

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

Parameters
session: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

customCommand: SessionCommand!

The custom command.

args: Bundle!

A Bundle for additional arguments. May be empty.

Returns
ListenableFuture<SessionResult!>!

The result of handling the custom command.

onDisconnected

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

Called when a controller is disconnected.

Interoperability: For legacy controllers, this is called when the controller doesn't send any command for a while. It's because there were no explicit disconnection in legacy controller APIs.

Parameters
session: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

onMediaButtonEvent

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

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

Media3 handles media button events internally. An app can override the default behaviour by overriding this method.

Return true to stop propagating the event any further. When false is returned, Media3 handles the event and calls the session player accordingly.

Apps normally don't need to override this method. When overriding this method, an app can/needs to handle all API-level specifics on its own. The intent passed to this method can come directly from the system that routed a media key event (for instance sent by Bluetooth) to your session.

Parameters
session: MediaSession!

The session that received the media button event.

controllerInfo: MediaSession.ControllerInfo!

The controller to which the media button event is attributed to.

intent: Intent!

The media button intent.

Returns
Boolean

True if the event was handled, false otherwise.

onPlaybackResumption

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

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

This happens, for example, if playback resumption is requested from a media button receiver or the System UI notification.

The method will only be called if the Player has COMMAND_GET_CURRENT_MEDIA_ITEM and either COMMAND_SET_MEDIA_ITEM or COMMAND_CHANGE_MEDIA_ITEMS available.

Parameters
mediaSession: MediaSession!

The media session for which playback resumption is requested.

controller: MediaSession.ControllerInfo!

The controller that requests the playback resumption. This may be a short living controller created only for issuing a play command for resuming playback.

Returns
ListenableFuture<MediaSession.MediaItemsWithStartPosition!>!

The playlist to resume playback with.

onPlayerCommandRequest

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

onPostConnect

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

Called immediately after a controller is connected. This is for custom initialization of the controller.

Note that calls to the controller (e.g. sendCustomCommand, setCustomLayout) work here but don't work in onConnect because the controller isn't connected yet in onConnect.

Parameters
session: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

onSetMediaItems

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

Called when a controller requested to set media items to the playlist via one of the Player.setMediaItem(s) methods. The default implementation calls onAddMediaItems. Override this method if you want to modify/set the starting index/position for the Player.setMediaItem(s) methods.

This callback is also called when an app is using a legacy 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).

By default, if and only if each of the provided media items has a set MediaItem.LocalConfiguration (for example, a URI), then the callback returns the list unaltered. Otherwise, the default implementation returns an .

If the requested media items don't have a , they need to be updated to make them playable by the underlying Player. Typically, this callback would be overridden with implementation that identifies the correct item by its mediaId and/or the requestMetadata.

Return a ListenableFuture with the resolved media items and starting index and position. You can also return the items directly by using Guava's immediateFuture. Once the has been resolved, the session will call setMediaItems as requested. If the resolved startIndex is C.INDEX_UNSET then the session will call setMediaItem with resetPosition set to true.

Interoperability: This method will be called in response to the following methods:

The values of mediaId, mediaUri, searchQuery and extras will be set to match the legacy method call. The session will call setMediaItems or addMediaItems, followed by prepare and play as appropriate once the MediaItem has been resolved.
Parameters
mediaSession: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

mediaItems: (Mutable)List<MediaItem!>!

The list of requested media items.

startIndex: Int

The start index in the MediaItem list from which to start playing, or C.INDEX_UNSET to start playing from the default index in the playlist.

startPositionMs: Long

The starting position in the media item from where to start playing, or C.TIME_UNSET to start playing from the default position in the media item. This value is ignored if startIndex is C.INDEX_UNSET

Returns
ListenableFuture<MediaSession.MediaItemsWithStartPosition!>!

A ListenableFuture with a MediaItemsWithStartPosition containing a list of resolved media items, and a starting index and position that are playable by the underlying Player. If returned startIndex is C.INDEX_UNSET and startPositionMs is C.TIME_UNSET, then Player#setMediaItems(List, true) will be called to set media items with default index and position.

onSetRating

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

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

To allow setting the user rating for the current MediaItem, the item's metadata should have the Rating field in order to provide possible rating style for controllers. Controllers will follow the rating style.

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

Parameters
session: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

rating: Rating!

The new rating from the controller.

onSetRating

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

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

To allow setting the user rating for a MediaItem, the item's metadata should have the Rating field in order to provide possible rating style for controllers. Controllers will follow the rating style.

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

Parameters
session: MediaSession!

The session for this event.

controller: MediaSession.ControllerInfo!

The controller information.

mediaId: String!

The media id.

rating: Rating!

The new rating from the controller.