Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

MediaSessionManager

public final class MediaSessionManager
extends Object

java.lang.Object
   ↳ android.media.session.MediaSessionManager


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

Summary

Nested classes

interface MediaSessionManager.OnActiveSessionsChangedListener

Listens for changes to the list of active sessions. 

interface MediaSessionManager.OnSession2TokensChangedListener

Listens for changes to the MediaSessionManager.getSession2Tokens()

class MediaSessionManager.RemoteUserInfo

Information of a remote user of MediaSession or MediaBrowserService

Public methods

void addOnActiveSessionsChangedListener(MediaSessionManager.OnActiveSessionsChangedListener sessionListener, ComponentName notificationListener)

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.

void addOnActiveSessionsChangedListener(MediaSessionManager.OnActiveSessionsChangedListener sessionListener, ComponentName notificationListener, Handler handler)

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.

void addOnSession2TokensChangedListener(MediaSessionManager.OnSession2TokensChangedListener listener)

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

void addOnSession2TokensChangedListener(MediaSessionManager.OnSession2TokensChangedListener listener, Handler handler)

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

List<MediaController> getActiveSessions(ComponentName notificationListener)

Get a list of controllers for all ongoing sessions.

List<Session2Token> getSession2Tokens()

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

boolean isTrustedForMediaControl(MediaSessionManager.RemoteUserInfo userInfo)

Checks whether the remote user is a trusted app.

void notifySession2Created(Session2Token token)

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

void removeOnActiveSessionsChangedListener(MediaSessionManager.OnActiveSessionsChangedListener listener)

Stop receiving active sessions updates on the specified listener.

void removeOnSession2TokensChangedListener(MediaSessionManager.OnSession2TokensChangedListener listener)

Removes the OnSession2TokensChangedListener to stop receiving session token updates.

Inherited methods

Public methods

addOnActiveSessionsChangedListener

Added in API level 21
public void addOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener, 
                ComponentName notificationListener)

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 thread that registered the listener.

Parameters
sessionListener MediaSessionManager.OnActiveSessionsChangedListener: The listener to add. This value must never be null.

notificationListener ComponentName: The enabled notification listener component. May be null. This value may be null.

addOnActiveSessionsChangedListener

Added in API level 21
public void addOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener, 
                ComponentName notificationListener, 
                Handler handler)

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 must never be null.

notificationListener ComponentName: The enabled notification listener component. May be null. This value may be null.

handler Handler: The handler to post events to. This value may be null.

addOnSession2TokensChangedListener

public void addOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener)

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

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

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

addOnSession2TokensChangedListener

public void addOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener, 
                Handler handler)

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

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

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

handler Handler: The handler to call listener on. This value must never be null.

getActiveSessions

Added in API level 21
public List<MediaController> getActiveSessions (ComponentName notificationListener)

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. This value may be null.

Returns
List<MediaController> A list of controllers for ongoing sessions.

getSession2Tokens

public List<Session2Token> getSession2Tokens ()

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.

Returns
List<Session2Token> A list of Session2Token. This value will never be null.

isTrustedForMediaControl

Added in API level 28
public boolean isTrustedForMediaControl (MediaSessionManager.RemoteUserInfo userInfo)

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 must never be null.

Returns
boolean true if the remote user is trusted and its package name matches with the UID. false otherwise.

notifySession2Created

public void notifySession2Created (Session2Token token)

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 must never be null.

removeOnActiveSessionsChangedListener

Added in API level 21
public void removeOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener listener)

Stop receiving active sessions updates on the specified listener.

Parameters
listener MediaSessionManager.OnActiveSessionsChangedListener: The listener to remove. This value must never be null.

removeOnSession2TokensChangedListener

public void removeOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener)

Removes the OnSession2TokensChangedListener to stop receiving session token updates.

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