Added in API level 29

SessionCallback

abstract class SessionCallback
kotlin.Any
   ↳ android.media.MediaSession2.SessionCallback

This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Callback to be called for all incoming commands from MediaController2s.

Summary

Public constructors

Public methods
open Unit

Called when the command sent to the controller is finished.

open Session2CommandGroup?

Called when a controller is created for this session.

open Unit

Called when a controller is disconnected

open Unit

Called immediately after a controller is connected.

open Session2Command.Result?

Called when a controller sent a session command.

Public constructors

SessionCallback

Added in API level 29
SessionCallback()

Public methods

onCommandResult

Added in API level 29
open fun onCommandResult(
    session: MediaSession2,
    controller: MediaSession2.ControllerInfo,
    token: Any,
    command: Session2Command,
    result: Session2Command.Result
): Unit

Called when the command sent to the controller is finished.

Parameters
session MediaSession2: the session for this event This value cannot be null.
controller MediaSession2.ControllerInfo: controller information This value cannot be null.
token Any: the token got from MediaSession2#sendSessionCommand This value cannot be null.
command Session2Command: the session command This value cannot be null.
result Session2Command.Result: the result of the session command This value cannot be null.

onConnect

Added in API level 29
open fun onConnect(
    session: MediaSession2,
    controller: MediaSession2.ControllerInfo
): Session2CommandGroup?

Called when a controller is created for this session. Return allowed commands for controller. By default it returns null.

You can reject the connection by returning null. In that case, controller receives MediaController2.ControllerCallback#onDisconnected(MediaController2) and cannot be used.

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

Parameters
session MediaSession2: the session for this event This value cannot be null.
controller MediaSession2.ControllerInfo: controller information. This value cannot be null.
Return
Session2CommandGroup? allowed commands. Can be null to reject connection.

onDisconnected

Added in API level 29
open fun onDisconnected(
    session: MediaSession2,
    controller: MediaSession2.ControllerInfo
): Unit

Called when a controller is disconnected

Parameters
session MediaSession2: the session for this event This value cannot be null.
controller MediaSession2.ControllerInfo: controller information This value cannot be null.

onPostConnect

Added in API level 29
open fun onPostConnect(
    session: MediaSession2,
    controller: MediaSession2.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. sendSessionCommand) work here but don't work in onConnect because the controller hasn't connected yet in onConnect.

Parameters
session MediaSession2: the session for this event This value cannot be null.
controller MediaSession2.ControllerInfo: controller information. This value cannot be null.

onSessionCommand

Added in API level 29
open fun onSessionCommand(
    session: MediaSession2,
    controller: MediaSession2.ControllerInfo,
    command: Session2Command,
    args: Bundle?
): Session2Command.Result?

Called when a controller sent a session command.

Parameters
session MediaSession2: the session for this event This value cannot be null.
controller MediaSession2.ControllerInfo: controller information This value cannot be null.
command Session2Command: the session command This value cannot be null.
args Bundle?: optional arguments This value may be null.
Return
Session2Command.Result? the result for the session command. If null, RESULT_INFO_SKIPPED will be sent to the session.