Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#pubctors) \| [Methods](https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#inhmethods)

# MediaSession2.SessionCallback

*** ** * ** ***

` public static abstract class MediaSession2.SessionCallback `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.MediaSession2.SessionCallback |

<br />

*** ** * ** ***

<br />

**This class was deprecated in API level 37.**   

Use the Media3 support library's `androidx.media3.session.MediaSession` instead.

This API is not generally intended for third party application developers. Use the [AndroidX](https://developer.android.com/jetpack/androidx) [Media3 session Library](https://developer.android.com/reference/androidx/media3/session/package-summary) for consistent behavior across all devices.

Callback to be called for all incoming commands from `https://developer.android.com/reference/android/media/MediaController2`s.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#SessionCallback()() ` |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onCommandResult(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo,%20java.lang.Object,%20android.media.Session2Command,%20android.media.Session2Command.Result)(https://developer.android.com/reference/android/media/MediaSession2 session, https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo controller, https://developer.android.com/reference/java/lang/Object token, https://developer.android.com/reference/android/media/Session2Command command, https://developer.android.com/reference/android/media/Session2Command.Result result) ` Called when the command sent to the controller is finished. |
| ` https://developer.android.com/reference/android/media/Session2CommandGroup` | ` https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onConnect(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo)(https://developer.android.com/reference/android/media/MediaSession2 session, https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo controller) ` Called when a controller is created for this session. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onDisconnected(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo)(https://developer.android.com/reference/android/media/MediaSession2 session, https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo controller) ` Called when a controller is disconnected |
| ` void` | ` https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onPostConnect(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo)(https://developer.android.com/reference/android/media/MediaSession2 session, https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo controller) ` Called immediately after a controller is connected. |
| ` https://developer.android.com/reference/android/media/Session2Command.Result` | ` https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onSessionCommand(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo,%20android.media.Session2Command,%20android.os.Bundle)(https://developer.android.com/reference/android/media/MediaSession2 session, https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo controller, https://developer.android.com/reference/android/media/Session2Command command, https://developer.android.com/reference/android/os/Bundle args) ` Called when a controller sent a session command. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### SessionCallback

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public SessionCallback ()
```

<br />

<br />

## Public methods

### onCommandResult

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `session` | `MediaSession2`: the session for this event. This value cannot be `null`. <br /> |
| `controller` | `MediaSession2.ControllerInfo`: controller information. This value cannot be `null`. <br /> |
| `token` | `Object`: the token got from `https://developer.android.com/reference/android/media/MediaSession2#sendSessionCommand(android.media.MediaSession2.ControllerInfo,%20android.media.Session2Command,%20android.os.Bundle)` This value cannot be `null`. <br /> |
| `command` | `Session2Command`: the session command. This value cannot be `null`. <br /> |
| `result` | `Session2Command.Result`: the result of the session command. This value cannot be `null`. <br /> |

### onConnect

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/android/media/MediaController2.ControllerCallback#onDisconnected(android.media.MediaController2)` and cannot be used.

The controller hasn't connected yet in this method, so calls to the controller (e.g. `https://developer.android.com/reference/android/media/MediaSession2#sendSessionCommand(android.media.MediaSession2.ControllerInfo,%20android.media.Session2Command,%20android.os.Bundle)`) would be ignored. Override `https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onPostConnect(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo)` for the custom initialization for the controller instead.

<br />

| Parameters ||
|---|---|
| `session` | `MediaSession2`: the session for this event. This value cannot be `null`. <br /> |
| `controller` | `MediaSession2.ControllerInfo`: controller information. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/Session2CommandGroup` | allowed commands. Can be `null` to reject connection. <br /> |

### onDisconnected

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onDisconnected (MediaSession2 session, 
                MediaSession2.ControllerInfo controller)
```

Called when a controller is disconnected

<br />

| Parameters ||
|---|---|
| `session` | `MediaSession2`: the session for this event. This value cannot be `null`. <br /> |
| `controller` | `MediaSession2.ControllerInfo`: controller information. This value cannot be `null`. <br /> |

### onPostConnect

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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. `https://developer.android.com/reference/android/media/MediaSession2#sendSessionCommand(android.media.MediaSession2.ControllerInfo,%20android.media.Session2Command,%20android.os.Bundle)`) work here but don't work in `https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onConnect(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo)` because the controller hasn't connected yet in `https://developer.android.com/reference/android/media/MediaSession2.SessionCallback#onConnect(android.media.MediaSession2,%20android.media.MediaSession2.ControllerInfo)`.

<br />

| Parameters ||
|---|---|
| `session` | `MediaSession2`: the session for this event. This value cannot be `null`. <br /> |
| `controller` | `MediaSession2.ControllerInfo`: controller information. This value cannot be `null`. <br /> |

### onSessionCommand

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

Called when a controller sent a session command.

<br />

| Parameters ||
|---|---|
| `session` | `MediaSession2`: the session for this event. This value cannot be `null`. <br /> |
| `controller` | `MediaSession2.ControllerInfo`: controller information. This value cannot be `null`. <br /> |
| `command` | `Session2Command`: the session command. This value cannot be `null`. <br /> |
| `args` | `Bundle`: optional arguments. This value may be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/Session2Command.Result` | the result for the session command. If `null`, RESULT_INFO_SKIPPED will be sent to the session. <br /> |