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

# MediaController2

*** ** * ** ***

` public class MediaController2 `   
` 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.MediaController2 |

<br />

*** ** * ** ***

<br />

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

Use the Media3 support library's `androidx.media3.session.MediaController` 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 an app to interact with an active `https://developer.android.com/reference/android/media/MediaSession2` or a `https://developer.android.com/reference/android/media/MediaSession2Service` which would provide `https://developer.android.com/reference/android/media/MediaSession2`.

## Summary

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

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/MediaController2#cancelSessionCommand(java.lang.Object)(https://developer.android.com/reference/java/lang/Object token) ` Cancels the session command previously sent. |
| ` void` | ` https://developer.android.com/reference/android/media/MediaController2#close()() ` Closes this resource, relinquishing any underlying resources. |
| ` https://developer.android.com/reference/android/media/Session2Token` | ` https://developer.android.com/reference/android/media/MediaController2#getConnectedToken()() ` Returns `https://developer.android.com/reference/android/media/Session2Token` of the connected session. |
| ` boolean` | ` https://developer.android.com/reference/android/media/MediaController2#isPlaybackActive()() ` Returns whether the session's playback is active. |
| ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/android/media/MediaController2#sendSessionCommand(android.media.Session2Command,%20android.os.Bundle)(https://developer.android.com/reference/android/media/Session2Command command, https://developer.android.com/reference/android/os/Bundle args) ` Sends a session command to the session <br /> |

### 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

### cancelSessionCommand

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

```
public void cancelSessionCommand (Object token)
```

Cancels the session command previously sent.

<br />

| Parameters ||
|---|---|
| `token` | `Object`: the token which is returned from `https://developer.android.com/reference/android/media/MediaController2#sendSessionCommand(android.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 />

### getConnectedToken

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

```
public Session2Token getConnectedToken ()
```

Returns `https://developer.android.com/reference/android/media/Session2Token` of the connected session. If it is not connected yet, it returns `null`.

This may differ with the `https://developer.android.com/reference/android/media/Session2Token` from the constructor. For example, if the controller is created with the token for `https://developer.android.com/reference/android/media/MediaSession2Service`, this would return token for the `https://developer.android.com/reference/android/media/MediaSession2` in the service.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/Session2Token` | Session2Token of the connected session, or `null` if not connected <br /> |

### isPlaybackActive

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

```
public boolean isPlaybackActive ()
```

Returns whether the session's playback is active.

<br />

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

**See also:**

- `https://developer.android.com/reference/android/media/MediaController2.ControllerCallback#onPlaybackActiveChanged(android.media.MediaController2,%20boolean)`

### sendSessionCommand

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

```
public Object sendSessionCommand (Session2Command command, 
                Bundle args)
```

Sends a session command to the session

<br />

<br />

| Parameters ||
|---|---|
| `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/MediaController2.ControllerCallback#onCommandResult(android.media.MediaController2,%20java.lang.Object,%20android.media.Session2Command,%20android.media.Session2Command.Result)` when its result is received. This value cannot be `null`. <br /> |