MediaSession.Callback


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

default ListenableFuture<List<MediaItem>>
onAddMediaItems(
    MediaSession mediaSession,
    MediaSession.ControllerInfo controller,
    List<MediaItem> mediaItems
)

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

default MediaSession.ConnectionResult

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

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

Called when a controller sent a custom command through sendCustomCommand.

default void
onDisconnected(
    MediaSession session,
    MediaSession.ControllerInfo controller
)

Called when a controller is disconnected.

default boolean
@UnstableApi
onMediaButtonEvent(
    MediaSession session,
    MediaSession.ControllerInfo controllerInfo,
    Intent intent
)

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

default ListenableFuture<MediaSession.MediaItemsWithStartPosition>

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

default int
@SessionResult.Code
onPlayerCommandRequest(
    MediaSession session,
    MediaSession.ControllerInfo controller,
    @Player.Command int playerCommand
)

This method is deprecated.

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

default void
@UnstableApi
onPlayerInteractionFinished(
    MediaSession session,
    MediaSession.ControllerInfo controllerInfo,
    Player.Commands playerCommands
)

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

default void
onPostConnect(
    MediaSession session,
    MediaSession.ControllerInfo controller
)

Called immediately after a controller is connected.

default ListenableFuture<MediaSession.MediaItemsWithStartPosition>
@UnstableApi
onSetMediaItems(
    MediaSession mediaSession,
    MediaSession.ControllerInfo controller,
    List<MediaItem> mediaItems,
    int startIndex,
    long startPositionMs
)

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

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

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

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

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

Public methods

onAddMediaItems

default ListenableFuture<List<MediaItem>> onAddMediaItems(
    MediaSession mediaSession,
    MediaSession.ControllerInfo controller,
    List<MediaItem> mediaItems
)

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

  • prepareFromUri
  • playFromUri
  • prepareFromMediaId
  • playFromMediaId
  • prepareFromSearch
  • playFromSearch
  • addQueueItem
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.

MediaSession.ControllerInfo controller

The controller information.

List<MediaItem> mediaItems

The list of requested media items.

Returns
ListenableFuture<List<MediaItem>>

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

onConnect

default MediaSession.ConnectionResult onConnect(MediaSession session, MediaSession.ControllerInfo controller)

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 media button preferences of the session are 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, setMediaButtonPreferences) 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
MediaSession session

The session for this event.

MediaSession.ControllerInfo controller

The controller information.

onCustomCommand

default ListenableFuture<SessionResultonCustomCommand(
    MediaSession session,
    MediaSession.ControllerInfo controller,
    SessionCommand customCommand,
    Bundle args
)

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 android.support.v4.media.MediaBrowserCompat.sendCustomAction(). If so, extras from android.support.v4.media.MediaBrowserCompat.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
MediaSession session

The session for this event.

MediaSession.ControllerInfo controller

The controller information.

SessionCommand customCommand

The custom command.

Bundle args

A Bundle for additional arguments. May be empty.

Returns
ListenableFuture<SessionResult>

The result of handling the custom command.

onDisconnected

default void onDisconnected(
    MediaSession session,
    MediaSession.ControllerInfo controller
)

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

The session for this event.

MediaSession.ControllerInfo controller

The controller information.

onMediaButtonEvent

@UnstableApi
default boolean onMediaButtonEvent(
    MediaSession session,
    MediaSession.ControllerInfo controllerInfo,
    Intent intent
)

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

The session that received the media button event.

MediaSession.ControllerInfo 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
default ListenableFuture<MediaSession.MediaItemsWithStartPositiononPlaybackResumption(
    MediaSession mediaSession,
    MediaSession.ControllerInfo controller
)

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.

MediaSession.ControllerInfo controller

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

onPlayerCommandRequest

@SessionResult.Code
default int onPlayerCommandRequest(
    MediaSession session,
    MediaSession.ControllerInfo controller,
    @Player.Command int playerCommand
)

onPlayerInteractionFinished

@UnstableApi
default void onPlayerInteractionFinished(
    MediaSession session,
    MediaSession.ControllerInfo controllerInfo,
    Player.Commands playerCommands
)

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

A controller may call multiple Player methods within a single Looper message. Those Player method calls are batched together and once finished, this callback is called to signal that no further Player interactions coming from this controller are expected for now.

Apps can use this callback if they need to trigger different logic depending on whether certain methods are called together, for example just setMediaItems, or setMediaItems and play together.

Parameters
MediaSession session

The MediaSession that received the Player calls.

MediaSession.ControllerInfo controllerInfo

The controller sending the calls.

Player.Commands playerCommands

The set of Player.Commands used to send these calls.

onPostConnect

default void onPostConnect(
    MediaSession session,
    MediaSession.ControllerInfo controller
)

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

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

Parameters
MediaSession session

The session for this event.

MediaSession.ControllerInfo controller

The controller information.

onSetMediaItems

@UnstableApi
default ListenableFuture<MediaSession.MediaItemsWithStartPositiononSetMediaItems(
    MediaSession mediaSession,
    MediaSession.ControllerInfo controller,
    List<MediaItem> mediaItems,
    int startIndex,
    long startPositionMs
)

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.

MediaSession.ControllerInfo controller

The controller information.

List<MediaItem> mediaItems

The list of requested media items.

int startIndex

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.

long startPositionMs

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

default ListenableFuture<SessionResultonSetRating(
    MediaSession session,
    MediaSession.ControllerInfo controller,
    Rating rating
)

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

The session for this event.

MediaSession.ControllerInfo controller

The controller information.

Rating rating

The new rating from the controller.

onSetRating

default ListenableFuture<SessionResultonSetRating(
    MediaSession session,
    MediaSession.ControllerInfo controller,
    String mediaId,
    Rating rating
)

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

The session for this event.

MediaSession.ControllerInfo controller

The controller information.

String mediaId

The media id.

Rating rating

The new rating from the controller.