# MediaSessionManager

Artifact: [androidx.media3:media3-session](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/session/MediaSessionManager.java+class:androidx.media3.session.MediaSessionManager)

*** ** * ** ***

<br />

```
@UnstableApi
class MediaSessionManager
```

<br />

*** ** * ** ***

Provides support for interacting with media sessions.

This class provides similar functionality to `https://developer.android.com/reference/android/media/session/MediaSessionManager.html` but returns Media3 `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken` instances instead of platform classes.

All methods are static and take a `https://developer.android.com/reference/android/content/Context.html` to access the system service.

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager.OnActiveSessionsChangedListener` Listener for changes to the active sessions. |

| ### Public functions |
|---|---|
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager#addOnActiveSessionsChangedListener(android.content.Context,java.lang.String,androidx.media3.session.MediaSessionManager.OnActiveSessionsChangedListener,java.util.concurrent.Executor,android.content.ComponentName)( context: https://developer.android.com/reference/android/content/Context.html!, packageName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, listener: https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager.OnActiveSessionsChangedListener!, executor: https://developer.android.com/reference/java/util/concurrent/Executor.html!, notificationListener: https://developer.android.com/reference/android/content/ComponentName.html? )` Adds a listener for changes to the active sessions, optionally filtered by package. |
| `java-static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken!>!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager#getActiveSessions(android.content.Context,java.lang.String,android.content.ComponentName)( context: https://developer.android.com/reference/android/content/Context.html!, packageName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, notificationListener: https://developer.android.com/reference/android/content/ComponentName.html? )` Retrieves a list of `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken` instances for active sessions, optionally filtered by package. |
| `java-static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager#getMediaKeyEventSession(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html!)` Returns a future that resolves to the token for the session that is currently receiving media key events. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager#removeOnActiveSessionsChangedListener(android.content.Context,androidx.media3.session.MediaSessionManager.OnActiveSessionsChangedListener)( context: https://developer.android.com/reference/android/content/Context.html!, listener: https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager.OnActiveSessionsChangedListener! )` Removes a listener for changes to the active sessions. |

## Public functions

### addOnActiveSessionsChangedListener

```
java-static fun addOnActiveSessionsChangedListener(
    context: Context!,
    packageName: String?,
    listener: MediaSessionManager.OnActiveSessionsChangedListener!,
    executor: Executor!,
    notificationListener: ComponentName?
): Unit
```

Adds a listener for changes to the active sessions, optionally filtered by package.

Requires `android.Manifest.permission.MEDIA_CONTENT_CONTROL` or an enabled notification listener for requesting sessions of other packages.

On API levels 37 and above, it automatically works for sessions of your own package without special permissions if `packageName` is set to your own package.

On API levels below 37, the listener will not be added and will not receive callbacks if the caller does not have the required permissions and requests their own package.

If requesting other packages (or all packages) without required permissions, a `https://developer.android.com/reference/java/lang/SecurityException.html` is thrown.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | The context to use to access the system service. |
| `packageName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The package name to filter by, or `null` for all packages. |
| `listener: https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager.OnActiveSessionsChangedListener!` | The listener to add. |
| `executor: https://developer.android.com/reference/java/util/concurrent/Executor.html!` | The executor to execute the callback on. |
| `notificationListener: https://developer.android.com/reference/android/content/ComponentName.html?` | The component name of the enabled notification listener, or ` null`. |

### getActiveSessions

```
java-static fun getActiveSessions(
    context: Context!,
    packageName: String?,
    notificationListener: ComponentName?
): ListenableFuture<(Mutable)List<SessionToken!>!>!
```

Retrieves a list of `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken` instances for active sessions, optionally filtered by package.

Requires `android.Manifest.permission.MEDIA_CONTENT_CONTROL` or an enabled notification listener for requesting sessions of other packages.

On API levels 37 and above, it automatically works for sessions of your own package without special permissions if `packageName` is set to your own package.

On API levels below 37, an empty list is returned if the caller does not have the required permissions and requests their own package.

If requesting other packages (or all packages) without required permissions, a `https://developer.android.com/reference/java/lang/SecurityException.html` is thrown.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | The context to use to access the system service. |
| `packageName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | The package name to filter by, or `null` for all packages. |
| `notificationListener: https://developer.android.com/reference/android/content/ComponentName.html?` | The component name of the enabled notification listener, or ` null`. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken!>!>!` | A `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` for the list of `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken` instances. |

### getMediaKeyEventSession

```
java-static fun getMediaKeyEventSession(context: Context!): ListenableFuture<SessionToken!>!
```

Returns a future that resolves to the token for the session that is currently receiving media key events.

On API levels before 33, this will return a future that resolves to `null`.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | The context to use to access the system service. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken!>!` | A `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` for the `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken`, or a future that resolves to `null` if no session is receiving key events or if the API is not supported. |

### removeOnActiveSessionsChangedListener

```
java-static fun removeOnActiveSessionsChangedListener(
    context: Context!,
    listener: MediaSessionManager.OnActiveSessionsChangedListener!
): Unit
```

Removes a listener for changes to the active sessions.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | The context to use to access the system service. |
| `listener: https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionManager.OnActiveSessionsChangedListener!` | The listener to remove. |