Google is committed to advancing racial equity for Black communities. See how.

SessionCallback

abstract class SessionCallback
kotlin.Any
   ↳ androidx.media2.session.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

Callback to be called for all incoming commands from MediaControllers.

Public methods
open Int
onCommandRequest(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo, @NonNull command: SessionCommand)

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

open SessionCommandGroup?
onConnect(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo)

Called when a controller is created for this session.

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

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

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

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

open Unit
onDisconnected(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo)

Called when a controller is disconnected.

open Int
onFastForward(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo)

Called when a controller called MediaController#fastForward().

open Unit
onPostConnect(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo)

Called immediately after a controller is connected.

open Int
onRewind(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo)

Called when a controller called MediaController#rewind().

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

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

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

Called when a controller set rating of a media item through MediaController#setRating(String, Rating).

open Int
onSkipBackward(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo)

Called when a controller called MediaController#skipBackward().

open Int
onSkipForward(@NonNull session: MediaSession, @NonNull controller: MediaSession.ControllerInfo)

Called when a controller called MediaController#skipForward().

Public constructors

<init>

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.

Public methods

onCommandRequest

open fun onCommandRequest(
    @NonNull session: MediaSession,
    @NonNull controller: MediaSession.ControllerInfo,
    @NonNull command: SessionCommand
): Int

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

Return SessionResult#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.
Return
Int RESULT_SUCCESS if you want to proceed with incoming command. Another code for ignore.

See Also

onConnect

@Nullable open fun onConnect(
    @NonNull session: MediaSession,
    @NonNull 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 MediaController.ControllerCallback#onDisconnected(MediaController) 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.
Return
SessionCommandGroup? allowed commands. Can be null to reject connection.

onCreateMediaItem

@Nullable open fun onCreateMediaItem(
    @NonNull session: MediaSession,
    @NonNull controller: MediaSession.ControllerInfo,
    @NonNull mediaId: String
): MediaItem?

Called when a controller has sent a