MediaSession.SessionCallback

Added in 1.0.0
Deprecated in 1.3.0

abstract class MediaSession.SessionCallback


Callback to be called for all incoming commands from MediaControllers.

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

Summary

Public constructors

Public functions

Int
onCommandRequest(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    command: SessionCommand
)

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

SessionCommandGroup?

Called when a controller is created for this session.

MediaItem?
onCreateMediaItem(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    mediaId: String
)

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

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.

Int
onFastForward(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
)

Called when a controller called fastForward.

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

Called immediately after a controller is connected.

Int

Called when a controller called rewind.

Int
onSetMediaUri(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    uri: Uri,
    extras: Bundle?
)

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

Int
onSetRating(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    mediaId: String,
    rating: Rating
)

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

Int
onSkipBackward(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
)

Called when a controller called skipBackward.

Int
onSkipForward(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
)

Called when a controller called skipForward.

Public constructors

SessionCallback

Added in 1.0.0
Deprecated in 1.3.0
SessionCallback()

Public functions

onCommandRequest

Added in 1.0.0
Deprecated in 1.3.0
fun onCommandRequest(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    command: SessionCommand
): Int

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

Return RESULT_SUCCESS to proceed the command. If something else is returned, command wouldn't be sent and the controller would receive the code with it.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information.

command: SessionCommand

a command. This method will be called for every single command.

Returns
Int

RESULT_SUCCESS if you want to proceed with incoming command. Another code for ignore.

onConnect

Added in 1.0.0
Deprecated in 1.3.0
fun onConnect(session: MediaSession, controller: MediaSession.ControllerInfo): SessionCommandGroup?

Called when a controller is created for this session. Return allowed commands for controller. By default it allows all connection requests and commands.

You can reject the connection by return null. In that case, the controller receives onDisconnected and cannot be used.

The controller hasn't connected yet in this method, so calls to the controller (e.g. sendCustomCommand, setCustomLayout) would be ignored. Override onPostConnect for the custom initialization for the controller instead.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information.

Returns
SessionCommandGroup?

allowed commands. Can be null to reject connection.

See also
onPostConnect

onCreateMediaItem

Added in 1.0.0
Deprecated in 1.3.0
fun onCreateMediaItem(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    mediaId: String
): MediaItem?

Called when a controller has sent a command with a MediaItem to add a new media item to this session. Being specific, this will be called for following APIs.

  1. addPlaylistItem
  2. replacePlaylistItem
  3. setPlaylist
  4. setMediaItem
Override this to translate incoming mediaId to a MediaItem to be understood by your player. For example, a player may only understand androidx.media2.common.FileMediaItem, UriMediaItem, and CallbackMediaItem. Check the documentation of the player that you're using.

If the given media ID is valid, you should return the media item with the given media ID. If the ID doesn't match, an RuntimeException will be thrown. You may return null if the given item is invalid. Here's the behavior when it happens.

Controller command Behavior when is returned
addPlaylistItem Ignore
replacePlaylistItem Ignore
setPlaylist Ignore null items, and build a list with non-null items. Call setPlaylist with the list
setMediaItem Ignore

This will be called on the same thread where onCommandRequest and commands with the media controller will be executed.

Default implementation returns the null.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

mediaId: String

non-empty media id for creating item with

Returns
MediaItem?

translated media item for player with the mediaId. Can be null to ignore.

onCustomCommand

Added in 1.0.0
Deprecated in 1.3.0
fun onCustomCommand(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    customCommand: SessionCommand,
    args: Bundle?
): SessionResult

Called when a controller sent a custom command through sendCustomCommand.

Interoperability: This would be also called by #sendCustomAction(String, Bundle, CustomActionCallback). If so, extra from sendCustomAction will be considered as args and customCommand would have null extra.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

customCommand: SessionCommand

custom command.

args: Bundle?

optional arguments

Returns
SessionResult

result of handling custom command. A runtime exception will be thrown if null is returned.

onDisconnected

Added in 1.0.0
Deprecated in 1.3.0
fun onDisconnected(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
): Unit

Called when a controller is disconnected.

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

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

onFastForward

Added in 1.0.0
Deprecated in 1.3.0
fun onFastForward(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
): Int

Called when a controller called fastForward.

It can be implemented in many ways. For example, it can be implemented by seeking forward once, series of seeking forward, or increasing playback speed.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

onPostConnect

Added in 1.0.0
Deprecated in 1.3.0
fun onPostConnect(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
): Unit

Called immediately after a controller is connected. This is a convenient method to add custom initialization between the session and a controller.

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

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information.

onRewind

Added in 1.0.0
Deprecated in 1.3.0
fun onRewind(session: MediaSession, controller: MediaSession.ControllerInfo): Int

Called when a controller called rewind.

It can be implemented in many ways. For example, it can be implemented by seeking backward once, series of seeking backward, or decreasing playback speed.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

onSetMediaUri

Added in 1.1.0
Deprecated in 1.3.0
fun onSetMediaUri(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    uri: Uri,
    extras: Bundle?
): Int

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

The implementation should create proper media item(s) for the given uri and call setMediaItem or setPlaylist.

When MediaControllerCompat is connected and sends commands with following methods, the uri would have the following patterns:

Method Uri pattern
prepareFromUri The uri passed as argument
prepareFromMediaId androidx://media2-session/prepareFromMediaId?id=[mediaId]
prepareFromSearch androidx://media2-session/prepareFromSearch?query=[query]
playFromUri The uri passed as argument
playFromMediaId androidx://media2-session/playFromMediaId?id=[mediaId]
playFromSearch androidx://media2-session/playFromSearch?query=[query]

prepare or play would be followed if this is called by above methods.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

uri: Uri

uri

extras: Bundle?

optional extra bundle

onSetRating

Added in 1.0.0
Deprecated in 1.3.0
fun onSetRating(
    session: MediaSession,
    controller: MediaSession.ControllerInfo,
    mediaId: String,
    rating: Rating
): Int

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

To allow setting user rating for a MediaItem, the media item's metadata should have Rating with the key METADATA_KEY_USER_RATING, in order to provide possible rating style for controller. Controller will follow the rating style.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

mediaId: String

non-empty media id

rating: Rating

new rating from the controller

onSkipBackward

Added in 1.0.0
Deprecated in 1.3.0
fun onSkipBackward(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
): Int

Called when a controller called skipBackward.

It's recommended to seek backward within the current media item, but its detail may vary. For example, it can be implemented by seeking backward for the fixed amount of seconds, or seeking backward to the nearest bookmark.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information

onSkipForward

Added in 1.0.0
Deprecated in 1.3.0
fun onSkipForward(
    session: MediaSession,
    controller: MediaSession.ControllerInfo
): Int

Called when a controller called skipForward.

It's recommended to seek forward within the current media item, but its detail may vary. For example, it can be implemented by seeking forward for the fixed amount of seconds, or seeking forward to the nearest bookmark.

Parameters
session: MediaSession

the session for this event

controller: MediaSession.ControllerInfo

controller information