MediaSessionManager


@UnstableApi
class MediaSessionManager


Provides support for interacting with media sessions.

This class provides similar functionality to android.media.session.MediaSessionManager but returns Media3 SessionToken instances instead of platform classes.

All methods are static and take a Context to access the system service.

Summary

Nested types

Listener for changes to the active sessions.

Public functions

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

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

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

Retrieves a list of SessionToken instances for active sessions, optionally filtered by package.

java-static ListenableFuture<@NullableType SessionToken!>!

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

java-static Unit

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 SecurityException is thrown.

Parameters
context: Context!

The context to use to access the system service.

packageName: String?

The package name to filter by, or null for all packages.

listener: MediaSessionManager.OnActiveSessionsChangedListener!

The listener to add.

executor: Executor!

The executor to execute the callback on.

notificationListener: ComponentName?

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 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 SecurityException is thrown.

Parameters
context: Context!

The context to use to access the system service.

packageName: String?

The package name to filter by, or null for all packages.

notificationListener: ComponentName?

The component name of the enabled notification listener, or null.

Returns
ListenableFuture<(Mutable)List<SessionToken!>!>!

A ListenableFuture for the list of SessionToken instances.

getMediaKeyEventSession

java-static fun getMediaKeyEventSession(context: Context!): ListenableFuture<@NullableType 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: Context!

The context to use to access the system service.

Returns
ListenableFuture<@NullableType SessionToken!>!

A ListenableFuture for the 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: Context!

The context to use to access the system service.

listener: MediaSessionManager.OnActiveSessionsChangedListener!

The listener to remove.