# 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
public final 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/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 |
|---|
| `public interface https://developer.android.com/reference/androidx/media3/session/MediaSessionManager.OnActiveSessionsChangedListener` Listener for changes to the active sessions. |

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

## Public methods

### addOnActiveSessionsChangedListener

```
public static void addOnActiveSessionsChangedListener(
    Context context,
    @Nullable String packageName,
    MediaSessionManager.OnActiveSessionsChangedListener listener,
    Executor executor,
    @Nullable ComponentName notificationListener
)
```

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 |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context to use to access the system service. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html packageName` | The package name to filter by, or `null` for all packages. |
| `https://developer.android.com/reference/androidx/media3/session/MediaSessionManager.OnActiveSessionsChangedListener listener` | The listener to add. |
| `https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | The executor to execute the callback on. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/content/ComponentName.html notificationListener` | The component name of the enabled notification listener, or ` null`. |

### getActiveSessions

```
public static ListenableFuture<List<SessionToken>> getActiveSessions(
    Context context,
    @Nullable String packageName,
    @Nullable ComponentName notificationListener
)
```

Retrieves a list of `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context to use to access the system service. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html packageName` | The package name to filter by, or `null` for all packages. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/content/ComponentName.html notificationListener` | 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://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/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/androidx/media3/session/SessionToken` instances. |

### getMediaKeyEventSession

```
public static ListenableFuture<@NullableType SessionToken> getMediaKeyEventSession(Context context)
```

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 |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | 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/androidx/media3/common/util/NullableType https://developer.android.com/reference/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/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

```
public static void removeOnActiveSessionsChangedListener(
    Context context,
    MediaSessionManager.OnActiveSessionsChangedListener listener
)
```

Removes a listener for changes to the active sessions.

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