public interface

MediaSession.SessionCallback

 androidx.media3.session.MediaSession.SessionCallback

Subclasses:

MediaLibraryService.MediaLibrarySession.MediaLibrarySessionCallback

Overview

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 MediaSession.Builder, the session will accept all controllers and all incoming commands by default.

Summary

Methods
public MediaSession.ConnectionResultonConnect(MediaSession session, MediaSession.ControllerInfo controller)

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

public <any>onCustomCommand(MediaSession session, MediaSession.ControllerInfo controller, SessionCommand customCommand, Bundle args)

Called when a controller sent a custom command through MediaController.sendCustomCommand(SessionCommand, Bundle).

public voidonDisconnected(MediaSession session, MediaSession.ControllerInfo controller)

Called when a controller is disconnected.

public intonPlayerCommandRequest(MediaSession session, MediaSession.ControllerInfo controller, int playerCommand)

Called when a controller sent a command which will be sent directly to the underlying Player.

public voidonPostConnect(MediaSession session, MediaSession.ControllerInfo controller)

Called immediately after a controller is connected.

public intonSetMediaUri(MediaSession session, MediaSession.ControllerInfo controller, Uri uri, Bundle extras)

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

public <any>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 MediaController.setRating(Rating).

public <any>onSetRating(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Rating rating)

Called when a controller requested to set a rating to a media for the current user by MediaController.setRating(String, Rating).

Methods

Called when a controller is about to connect to this session. Return a result containing available commands for the controller by using MediaSession.ConnectionResult.accept(SessionCommands, Player.Commands). By default it allows all connection requests and commands.

Note that the player commands in MediaSession.ConnectionResult.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.

You can reject the connection by returning MediaSession.ConnectionResult.reject()}. In that case, the controller will get java.lang.SecurityException when resolving the returned by MediaController.Builder.buildAsync().

The controller isn't connected yet, so calls to the controller (e.g. MediaSession.sendCustomCommand(MediaSession.ControllerInfo, SessionCommand, Bundle), MediaSession.setCustomLayout(MediaSession.ControllerInfo, List)) will be ignored. Use MediaSession.SessionCallback.onPostConnect(MediaSession, MediaSession.ControllerInfo) 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: The session for this event.
controller: The controller information.

Returns:

The MediaSession.ConnectionResult.

public 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. MediaSession.sendCustomCommand(MediaSession.ControllerInfo, SessionCommand, Bundle), MediaSession.setCustomLayout(MediaSession.ControllerInfo, List)) work here but don't work in MediaSession.SessionCallback.onConnect(MediaSession, MediaSession.ControllerInfo) because the controller isn't connected yet in MediaSession.SessionCallback.onConnect(MediaSession, MediaSession.ControllerInfo).

Parameters:

session: The session for this event.
controller: The controller information.

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

session: The session for this event.
controller: The controller information.

public int onPlayerCommandRequest(MediaSession session, MediaSession.ControllerInfo controller, int playerCommand)

Called when a controller sent a command which will be sent directly to the underlying Player.

Return SessionResult.RESULT_SUCCESS to proceed the command. Otherwise, the command won't be sent and the controller will receive the code. This method will be called for every single command.

Parameters:

session: The session for this event.
controller: The controller information.
playerCommand: A .

Returns:

SessionResult.RESULT_SUCCESS to proceed, or another code to ignore.

See also:

public <any> onSetRating(MediaSession session, MediaSession.ControllerInfo controller, java.lang.String mediaId, Rating rating)

Called when a controller requested to set a rating to a media for the current user by MediaController.setRating(String, Rating).

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 to send a SessionResult back to the controller asynchronously. You can also return a SessionResult directly by using Guava's .

Parameters:

session: The session for this event.
controller: The controller information.
mediaId: The media id.
rating: The new rating from the controller.

See also: SessionCommand.COMMAND_CODE_SESSION_SET_RATING

public <any> 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 MediaController.setRating(Rating).

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 to send a SessionResult back to the controller asynchronously. You can also return a SessionResult directly by using Guava's .

Parameters:

session: The session for this event.
controller: The controller information.
rating: The new rating from the controller.

See also: SessionCommand.COMMAND_CODE_SESSION_SET_RATING

public int onSetMediaUri(MediaSession session, MediaSession.ControllerInfo controller, Uri uri, Bundle extras)

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

The implementation should create proper media item(s) for the given uri and call Player.setMediaItems(List).

When android.support.v4.media.session.MediaControllerCompat is connected and sends commands with following methods, the uri will have the following patterns:

Uri patterns corresponding to MediaControllerCompat command methods
Method Uri pattern
The uri passed as argument
androidx://media3-session/prepareFromMediaId?id=[mediaId]
androidx://media3-session/prepareFromSearch?query=[query]
The uri passed as argument
androidx://media3-session/playFromMediaId?id=[mediaId]
androidx://media3-session/playFromSearch?query=[query]

Player.prepare() or Player.play() should follow if this is called by above methods.

Parameters:

session: The session for this event.
controller: The controller information.
uri: The uri.
extras: An extra . May be empty.

Returns:

A result code.

public <any> onCustomCommand(MediaSession session, MediaSession.ControllerInfo controller, SessionCommand customCommand, Bundle args)

Called when a controller sent a custom command through MediaController.sendCustomCommand(SessionCommand, Bundle).

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

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

Parameters:

session: The session for this event.
controller: The controller information.
customCommand: The custom command.
args: A for additional arguments. May be empty.

Returns:

The result of handling the custom command.

See also: SessionCommand.COMMAND_CODE_CUSTOM