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: [Nested Classes](https://developer.android.com/reference/android/media/MediaSession2#nestedclasses) \| [Methods](https://developer.android.com/reference/android/media/MediaSession2#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/MediaSession2#inhmethods)

# MediaSession2

*** ** * ** ***


`
public


class
MediaSession2
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/java/lang/AutoCloseable


`

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

<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/media/media3/session/control-playback) for consistent behavior across all devices.


Allows a media app to expose its transport controls and playback information in a process to
other processes including the Android framework and other apps.

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/media/MediaSession2.Builder` *This class was deprecated in API level 37. Use the Media3 support library's `androidx.media3.session.MediaSession` instead.* |
| ` class` | `https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo` *This class was deprecated in API level 37. Use the Media3 support library's `androidx.media3.session.MediaSession` instead.* |
| ` class` | `https://developer.android.com/reference/android/media/MediaSession2.SessionCallback` *This class was deprecated in API level 37. Use the Media3 support library's `androidx.media3.session.MediaSession` instead.* |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/MediaSession2#broadcastSessionCommand(android.media.Session2Command,%20android.os.Bundle)(https://developer.android.com/reference/android/media/Session2Command command, https://developer.android.com/reference/android/os/Bundle args) ` Broadcasts a session command to all the connected controllers |
| ` void` | ` https://developer.android.com/reference/android/media/MediaSession2#cancelSessionCommand(android.media.MediaSession2.ControllerInfo,%20java.lang.Object)(https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo controller, https://developer.android.com/reference/java/lang/Object token) ` Cancels the session command previously sent. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaSession2#close()() ` Closes this resource, relinquishing any underlying resources. |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo>` | ` https://developer.android.com/reference/android/media/MediaSession2#getConnectedControllers()() ` Gets the list of the connected controllers |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/media/MediaSession2#getId()() ` Returns the session ID |
| ` https://developer.android.com/reference/android/media/Session2Token` | ` https://developer.android.com/reference/android/media/MediaSession2#getToken()() ` Returns the `https://developer.android.com/reference/android/media/Session2Token` for creating `https://developer.android.com/reference/android/media/MediaController2`. |
| ` boolean` | ` https://developer.android.com/reference/android/media/MediaSession2#isPlaybackActive()() ` Returns whether the playback is active (i.e. |
| ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/android/media/MediaSession2#sendSessionCommand(android.media.MediaSession2.ControllerInfo,%20android.media.Session2Command,%20android.os.Bundle)(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) ` Sends a session command to a specific controller |
| ` void` | ` https://developer.android.com/reference/android/media/MediaSession2#setPlaybackActive(boolean)(boolean playbackActive) ` Sets whether the playback is active (i.e. |

| ### 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*. | ||
| From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() ` Closes this resource, relinquishing any underlying resources. | ||

## Public methods

### broadcastSessionCommand

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

```
public void broadcastSessionCommand (Session2Command command, 
                Bundle args)
```

Broadcasts a session command to all the connected controllers

<br />

<br />

| Parameters ||
|---|---|
| `command` | `Session2Command`: the session command. This value cannot be `null`. <br /> |
| `args` | `Bundle`: optional arguments. This value may be `null`. <br /> |

### cancelSessionCommand

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

```
public void cancelSessionCommand (MediaSession2.ControllerInfo controller, 
                Object token)
```

Cancels the session command previously sent.

<br />

| Parameters ||
|---|---|
| `controller` | `MediaSession2.ControllerInfo`: the controller to get the session command. This value cannot be `null`. <br /> |
| `token` | `Object`: the token which is returned 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 /> |

### close

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

```
public void close ()
```

Closes this resource, relinquishing any underlying resources.
This method is invoked automatically on objects managed by the
`try`-with-resources statement.

<br />

### getConnectedControllers

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

```
public List<MediaSession2.ControllerInfo> getConnectedControllers ()
```

Gets the list of the connected controllers

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/MediaSession2.ControllerInfo>` | list of the connected controllers. This value cannot be `null`. <br /> |

### getId

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

```
public String getId ()
```

Returns the session ID

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | This value cannot be `null`. <br /> |

### getToken

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

```
public Session2Token getToken ()
```

Returns the `https://developer.android.com/reference/android/media/Session2Token` for creating `https://developer.android.com/reference/android/media/MediaController2`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/Session2Token` | This value cannot be `null`. <br /> |

### isPlaybackActive

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

```
public boolean isPlaybackActive ()
```

Returns whether the playback is active (i.e. playing something)

<br />

| Returns ||
|---|---|
| `boolean` | `true` if the playback active, `false` otherwise. <br /> |

### sendSessionCommand

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

```
public Object sendSessionCommand (MediaSession2.ControllerInfo controller, 
                Session2Command command, 
                Bundle args)
```

Sends a session command to a specific controller

<br />

<br />

| Parameters ||
|---|---|
| `controller` | `MediaSession2.ControllerInfo`: the controller to get the session command. 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/java/lang/Object` | a token which will be sent together in `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)` when its result is received. This value cannot be `null`. <br /> |

### setPlaybackActive

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

```
public void setPlaybackActive (boolean playbackActive)
```

Sets whether the playback is active (i.e. playing something)

<br />

| Parameters ||
|---|---|
| `playbackActive` | `boolean`: `true` if the playback active, `false` otherwise. <br /> |