MediaSession2.SessionCallback


public static abstract class MediaSession2.SessionCallback
extends Object

java.lang.Object
   ↳ 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

SessionCallback()

Public methods

void onCommandResult(MediaSession2 session, MediaSession2.ControllerInfo controller, Object token, Session2Command command, Session2Command.Result result)

Called when the command sent to the controller is finished.

Session2CommandGroup onConnect(MediaSession2 session, MediaSession2.ControllerInfo controller)

Called when a controller is created for this session.

void onDisconnected(MediaSession2 session, MediaSession2.ControllerInfo controller)

Called when a controller is disconnected

void onPostConnect(MediaSession2 session, MediaSession2.ControllerInfo controller)

Called immediately after a controller is connected.

Session2Command.Result onSessionCommand(MediaSession2 session, MediaSession2.ControllerInfo controller, Session2Command command, Bundle args)

Called when a controller sent a session command.

Inherited methods

Public constructors

SessionCallback

Added in API level 29
public SessionCallback ()

Public methods

onCommandResult

Added in API level 29
public void onCommandResult (MediaSession2 session, 
                MediaSession2.ControllerInfo controller, 
                Object token, 
                Session2Command command, 
                Session2Command.Result result)

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 Object: 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
public Session2CommandGroup onConnect (MediaSession2 session, 
                MediaSession2.ControllerInfo controller)

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. MediaSession2.sendSessionCommand(ControllerInfo, Session2Command, Bundle)) would be ignored. Override onPostConnect(MediaSession2, ControllerInfo) 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.

Returns
Session2CommandGroup allowed commands. Can be null to reject connection.

onDisconnected

Added in API level 29
public void onDisconnected (MediaSession2 session, 
                MediaSession2.ControllerInfo controller)

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
public void onPostConnect (MediaSession2 session, 
                MediaSession2.ControllerInfo controller)

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. MediaSession2.sendSessionCommand(ControllerInfo, Session2Command, Bundle)) work here but don't work in onConnect(MediaSession2, ControllerInfo) because the controller hasn't connected yet in onConnect(MediaSession2, ControllerInfo).

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
public Session2Command.Result onSessionCommand (MediaSession2 session, 
                MediaSession2.ControllerInfo controller, 
                Session2Command command, 
                Bundle args)

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.

Returns
Session2Command.Result the result for the session command. If null, RESULT_INFO_SKIPPED will be sent to the session.