Added in API level 21

MediaSessionManager


class MediaSessionManager
kotlin.Any
   ↳ android.media.session.MediaSessionManager

Provides support for interacting with media sessions that applications have published to express their ongoing media playback state.

Summary

Nested classes
abstract

Listens for changes to the list of active sessions.

abstract

Listener to receive changes in the media key event session, which would receive a media key event unless specified.

abstract

This API is not generally intended for third party application developers.

Information of a remote user of MediaSession or MediaBrowserService.

Public methods
Unit

Add a listener to be notified when the list of active sessions changes.

Unit

Add a listener to be notified when the list of active sessions changes.

Unit

Add a listener to be notified when the media key session is changed.

Unit

This API is not generally intended for third party application developers.

Unit

This API is not generally intended for third party application developers.

MutableList<MediaController!>
getActiveSessions(notificationListener: ComponentName?)

Get a list of controllers for all ongoing sessions.

MediaSession.Token?

Gets the media key event session, which would receive a media key event unless specified.

String

Gets the package name of the media key event session.

MutableList<Session2Token!>

This API is not generally intended for third party application developers.

Boolean

Checks whether the remote user is a trusted app.

Unit

This API is not generally intended for third party application developers.

Unit

Stop receiving active sessions updates on the specified listener.

Unit

Stop receiving updates on media key event session change on the specified listener.

Unit

This API is not generally intended for third party application developers.

Public methods

addOnActiveSessionsChangedListener

Added in API level 21
fun addOnActiveSessionsChangedListener(
    sessionListener: MediaSessionManager.OnActiveSessionsChangedListener,
    notificationListener: ComponentName?
): Unit

Add a listener to be notified when the list of active sessions changes.

This requires the android.Manifest.permission#MEDIA_CONTENT_CONTROL permission be held by the calling app. You may also retrieve this list if your app is an enabled notificationlistener using the NotificationListenerService APIs, in which case you must pass the ComponentName of your enabled listener.

Parameters
sessionListener MediaSessionManager.OnActiveSessionsChangedListener: The listener to add. This value cannot be null.
notificationListener ComponentName?: The enabled notification listener component. May be null.

addOnActiveSessionsChangedListener

Added in API level 21
fun addOnActiveSessionsChangedListener(
    sessionListener: MediaSessionManager.OnActiveSessionsChangedListener,
    notificationListener: ComponentName?,
    handler: Handler?
): Unit

Add a listener to be notified when the list of active sessions changes.

This requires the android.Manifest.permission#MEDIA_CONTENT_CONTROL permission be held by the calling app. You may also retrieve this list if your app is an enabled notification listener using the NotificationListenerService APIs, in which case you must pass the ComponentName of your enabled listener. Updates will be posted to the handler specified or to the caller's thread if the handler is null.

Parameters
sessionListener MediaSessionManager.OnActiveSessionsChangedListener: The listener to add. This value cannot be null.
notificationListener ComponentName?: The enabled notification listener component. May be null.
handler Handler?: The handler to post events to. This value may be null.

addOnMediaKeyEventSessionChangedListener

Added in API level 33
fun addOnMediaKeyEventSessionChangedListener(
    executor: Executor,
    listener: MediaSessionManager.OnMediaKeyEventSessionChangedListener
): Unit

Add a listener to be notified when the media key session is changed.

This requires the android.Manifest.permission#MEDIA_CONTENT_CONTROL permission be held by the calling app, or the app has an enabled notification listener using the NotificationListenerService APIs. If none of them applies, it will throw a SecurityException.

Parameters
executor Executor: The executor on which the listener should be invoked. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
listener MediaSessionManager.OnMediaKeyEventSessionChangedListener: A OnMediaKeyEventSessionChangedListener. This value cannot be null.

addOnSession2TokensChangedListener

Added in API level 29
fun addOnSession2TokensChangedListener(listener: MediaSessionManager.OnSession2TokensChangedListener): Unit

This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Adds a listener to be notified when the getSession2Tokens() changes.

Parameters
listener MediaSessionManager.OnSession2TokensChangedListener: The listener to add This value cannot be null.

addOnSession2TokensChangedListener

Added in API level 29
fun addOnSession2TokensChangedListener(
    listener: MediaSessionManager.OnSession2TokensChangedListener,
    handler: Handler
): Unit

This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Adds a listener to be notified when the getSession2Tokens() changes.

Parameters
listener MediaSessionManager.OnSession2TokensChangedListener: The listener to add This value cannot be null.
handler Handler: The handler to call listener on. This value cannot be null.

getActiveSessions

Added in API level 21
fun getActiveSessions(notificationListener: ComponentName?): MutableList<MediaController!>

Get a list of controllers for all ongoing sessions. The controllers will be provided in priority order with the most important controller at index 0.

This requires the android.Manifest.permission#MEDIA_CONTENT_CONTROL permission be held by the calling app. You may also retrieve this list if your app is an enabled notification listener using the NotificationListenerService APIs, in which case you must pass the ComponentName of your enabled listener.

Parameters
notificationListener ComponentName?: The enabled notification listener component. May be null.
Return
MutableList<MediaController!> A list of controllers for ongoing sessions. This value cannot be null.

getMediaKeyEventSession

Added in API level 33
fun getMediaKeyEventSession(): MediaSession.Token?

Gets the media key event session, which would receive a media key event unless specified.

This requires the android.Manifest.permission#MEDIA_CONTENT_CONTROL permission be held by the calling app, or the app has an enabled notification listener using the NotificationListenerService APIs. If none of them applies, it will throw a SecurityException.

Return
MediaSession.Token? The media key event session, which would receive key events by default, unless the caller has specified the target. Can be null.

getMediaKeyEventSessionPackageName

Added in API level 33
fun getMediaKeyEventSessionPackageName(): String

Gets the package name of the media key event session.

This requires the android.Manifest.permission#MEDIA_CONTENT_CONTROL permission be held by the calling app, or the app has an enabled notification listener using the NotificationListenerService APIs. If none of them applies, it will throw a SecurityException.

Return
String The package name of the media key event session or the last session's media button receiver if the media key event session is null. Returns an empty string if neither of them exists.

getSession2Tokens

Added in API level 29
fun getSession2Tokens(): MutableList<Session2Token!>

This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Gets a list of Session2Token with type Session2Token#TYPE_SESSION for the current user.

Although this API can be used without any restriction, each session owners can accept or reject your uses of MediaSession2.

Return
MutableList<Session2Token!> A list of Session2Token. This value cannot be null.

isTrustedForMediaControl

Added in API level 28
fun isTrustedForMediaControl(userInfo: MediaSessionManager.RemoteUserInfo): Boolean

Checks whether the remote user is a trusted app.

An app is trusted if the app holds the android.Manifest.permission#MEDIA_CONTENT_CONTROL permission or has an enabled notification listener.

Parameters
userInfo MediaSessionManager.RemoteUserInfo: The remote user info from either MediaSession#getCurrentControllerInfo() or MediaBrowserService#getCurrentBrowserInfo(). This value cannot be null.
Return
Boolean true if the remote user is trusted and its package name matches with the UID. false otherwise.

notifySession2Created

Added in API level 29
Deprecated in API level 31
fun notifySession2Created(token: Session2Token): Unit

Deprecated: Don't use this method. A new media session is notified automatically.

This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Notifies that a new MediaSession2 with type Session2Token#TYPE_SESSION is created.

Do not use this API directly, but create a new instance through the MediaSession2.Builder instead.

Parameters
token Session2Token: newly created session2 token This value cannot be null.

removeOnActiveSessionsChangedListener

Added in API level 21
fun removeOnActiveSessionsChangedListener(sessionListener: MediaSessionManager.OnActiveSessionsChangedListener): Unit

Stop receiving active sessions updates on the specified listener.

Parameters
sessionListener MediaSessionManager.OnActiveSessionsChangedListener: The listener to remove. This value cannot be null.

removeOnMediaKeyEventSessionChangedListener

Added in API level 33
fun removeOnMediaKeyEventSessionChangedListener(listener: MediaSessionManager.OnMediaKeyEventSessionChangedListener): Unit

Stop receiving updates on media key event session change on the specified listener.

Parameters
listener MediaSessionManager.OnMediaKeyEventSessionChangedListener: A OnMediaKeyEventSessionChangedListener. This value cannot be null.

removeOnSession2TokensChangedListener

Added in API level 29
fun removeOnSession2TokensChangedListener(listener: MediaSessionManager.OnSession2TokensChangedListener): Unit

This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Removes the OnSession2TokensChangedListener to stop receiving session token updates.

Parameters
listener MediaSessionManager.OnSession2TokensChangedListener: The listener to remove. This value cannot be null.