MediaSessionCompat

public class MediaSessionCompat
extends Object

java.lang.Object
   ↳ android.support.v4.media.session.MediaSessionCompat


Allows interaction with media controllers, volume keys, media buttons, and transport controls.

A MediaSession should be created when an app wants to publish media playback information or handle media keys. In general an app only needs one session for all playback, though multiple sessions can be created to provide finer grain controls of media.

Once a session is created the owner of the session may pass its session token to other processes to allow them to create a MediaControllerCompat to interact with the session.

To receive commands, media keys, and other events a MediaSessionCompat.Callback must be set with setCallback(Callback).

When an app is finished performing playback it must call release() to clean up the session and notify any controllers.

MediaSessionCompat objects are not thread safe and all calls should be made from the same thread.

This is a helper for accessing features in MediaSession introduced after API level 4 in a backwards compatible fashion.

Developer Guides

For information about building your media application, read the Media Apps developer guide.

Summary

Nested classes

class MediaSessionCompat.Callback

Receives transport controls, media buttons, and commands from controllers and the system. 

interface MediaSessionCompat.OnActiveChangeListener

 

class MediaSessionCompat.QueueItem

A single item that is part of the play queue. 

class MediaSessionCompat.Token

Represents an ongoing session. 

Constants

String ACTION_FLAG_AS_INAPPROPRIATE

Predefined custom action to flag the media that is currently playing as inappropriate.

String ACTION_FOLLOW

Predefined custom action to follow an artist, album, or playlist.

String ACTION_SKIP_AD

Predefined custom action to skip the advertisement that is currently playing.

String ACTION_UNFOLLOW

Predefined custom action to unfollow an artist, album, or playlist.

String ARGUMENT_MEDIA_ATTRIBUTE

Argument to indicate the media attribute.

String ARGUMENT_MEDIA_ATTRIBUTE_VALUE

String argument to indicate the value of the media attribute (e.g., the name of the artist).

int FLAG_HANDLES_MEDIA_BUTTONS

This constant is deprecated. This flag is no longer used. All media sessions are expected to handle media button events now. For backward compatibility, this flag will be always set.

int FLAG_HANDLES_QUEUE_COMMANDS

Sets this flag on the session to indicate that it handles queue management commands through its MediaSessionCompat.Callback.

int FLAG_HANDLES_TRANSPORT_CONTROLS

This constant is deprecated. This flag is no longer used. All media sessions are expected to handle transport controls now. For backward compatibility, this flag will be always set.

int MEDIA_ATTRIBUTE_ALBUM

The value of ARGUMENT_MEDIA_ATTRIBUTE indicating the album.

int MEDIA_ATTRIBUTE_ARTIST

The value of ARGUMENT_MEDIA_ATTRIBUTE indicating the artist.

int MEDIA_ATTRIBUTE_PLAYLIST

The value of ARGUMENT_MEDIA_ATTRIBUTE indicating the playlist.

Public constructors

MediaSessionCompat(Context context, String tag)

Creates a new session.

MediaSessionCompat(Context context, String tag, ComponentName mbrComponent, PendingIntent mbrIntent)

Creates a new session with a specified media button receiver (a component name and/or a pending intent).

MediaSessionCompat(Context context, String tag, ComponentName mbrComponent, PendingIntent mbrIntent, Bundle sessionInfo)

Creates a new session with a specified media button receiver (a component name and/or a pending intent).

Public methods

void addOnActiveChangeListener(MediaSessionCompat.OnActiveChangeListener listener)

Adds a listener to be notified when the active status of this session changes.

static MediaSessionCompat fromMediaSession(Context context, Object mediaSession)

Creates an instance from a framework MediaSession object.

MediaControllerCompat getController()

Gets a controller for this session.

final MediaSessionManager.RemoteUserInfo getCurrentControllerInfo()

Gets the controller information who sent the current request.

Object getMediaSession()

Gets the underlying framework MediaSession object.

Object getRemoteControlClient()

Gets the underlying framework RemoteControlClient object.

MediaSessionCompat.Token getSessionToken()

Retrieves a token object that can be used by apps to create a MediaControllerCompat for interacting with this session.

boolean isActive()

Gets the current active state of this session.

void release()

This must be called when an app has finished performing playback.

void removeOnActiveChangeListener(MediaSes