Skip to content

Most visited

Recently visited

navigation

MediaController

public final class MediaController
extends Object

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


Allows an app to interact with an ongoing media session. Media buttons and other commands can be sent to the session. A callback may be registered to receive updates from the session, such as metadata and play state changes.

A MediaController can be created through MediaSessionManager if you hold the "android.permission.MEDIA_CONTENT_CONTROL" permission or are an enabled notification listener or by getting a MediaSession.Token directly from the session owner.

MediaController objects are thread-safe.

Summary

Nested classes

class MediaController.Callback

Callback for receiving updates from the session. 

class MediaController.PlaybackInfo

Holds information about the current playback and how audio is handled for this session. 

class MediaController.TransportControls

Interface for controlling media playback on a session. 

Public constructors

MediaController(Context context, MediaSession.Token token)

Create a new MediaController from a session's token.

Public methods

void addQueueItem(MediaDescription description)

Add a queue item from the given description at the end of the play queue of this session.

void addQueueItem(MediaDescription description, int index)

Add a queue item from the given description at the specified position in the play queue of this session.

void adjustVolume(int direction, int flags)

Adjust the volume of the output this session is playing on.

boolean dispatchMediaButtonEvent(KeyEvent keyEvent)

Send the specified media button event to the session.

Bundle getExtras()

Get the extras for this session.

long getFlags()

Get the flags for this session.

MediaMetadata getMetadata()

Get the current metadata for this session.

String getPackageName()

Get the session owner's package name.

MediaController.PlaybackInfo getPlaybackInfo()

Get the current playback info for this session.

PlaybackState getPlaybackState()

Get the current playback state for this session.

List<MediaSession.QueueItem> getQueue()

Get the current play queue for this session if one is set.

CharSequence getQueueTitle()

Get the queue title for this session.

int getRatingType()

Get the rating type supported by the session.

int getRepeatMode()

Get the repeat mode for this session.

PendingIntent getSessionActivity()

Get an intent for launching UI associated with this session if one exists.

MediaSession.Token getSessionToken()

Get the token for the session this is connected to.

MediaController.TransportControls getTransportControls()

Get a MediaController.TransportControls instance to send transport actions to this session.

boolean isShuffleModeEnabled()

Return whether the shuffle mode is enabled for this session.

void registerCallback(MediaController.Callback callback)

Registers a callback to receive updates from the Session.

void registerCallback(MediaController.Callback callback, Handler handler)

Registers a callback to receive updates from the session.

void removeQueueItem(MediaDescription description)

Remove the first occurrence of the specified MediaSession.QueueItem with the given description in the play queue of the associated session.

void removeQueueItemAt(int index)

Remove an queue item at the specified position in the play queue of this session.

void sendCommand(String command, Bundle args, ResultReceiver cb)

Sends a generic command to the session.

void setVolumeTo(int value, int flags)

Set the volume of the output this session is playing on.

void unregisterCallback(MediaController.Callback callback)

Unregisters the specified callback.

Inherited methods

From class java.lang.Object

Public constructors

MediaController

added in API level 21
MediaController (Context context, 
                MediaSession.Token token)

Create a new MediaController from a session's token.

Parameters
context Context: The caller's context.
token MediaSession.Token: The token for the session.

Public methods

addQueueItem

void addQueueItem (MediaDescription description)

Add a queue item from the given description at the end of the play queue of this session. Not all sessions may support this.

Parameters
description MediaDescription: The MediaDescription for creating the MediaSession.QueueItem to be inserted.
Throws
UnsupportedOperationException If this session doesn't support this.

See also:

addQueueItem

void addQueueItem (MediaDescription description, 
                int index)

Add a queue item from the given description at the specified position in the play queue of this session. Shifts the queue item currently at that position (if any) and any subsequent queue items to the right (adds one to their indices). Not all sessions may support this.

Parameters
description MediaDescription: The MediaDescription for creating the MediaSession.QueueItem to be inserted.
index int: The index at which the created MediaSession.QueueItem is to be inserted.
Throws
UnsupportedOperationException If this session doesn't support this.

See also:

adjustVolume

added in API level 21
void adjustVolume (int direction, 
                int flags)

Adjust the volume of the output this session is playing on. The direction must be one of ADJUST_LOWER, ADJUST_RAISE, or ADJUST_SAME. The command will be ignored if the session does not support VOLUME_CONTROL_RELATIVE or VOLUME_CONTROL_ABSOLUTE. The flags in AudioManager may be used to affect the handling.

Parameters
direction int: The direction to adjust the volume in.
flags int: Any flags to pass with the command.

See also:

dispatchMediaButtonEvent

added in API level 21
boolean dispatchMediaButtonEvent (KeyEvent keyEvent)

Send the specified media button event to the session. Only media keys can be sent by this method, other keys will be ignored.

Parameters
keyEvent KeyEvent: The media button event to dispatch.
Returns
boolean true if the event was sent to the session, false otherwise.

getExtras

added in API level 21
Bundle getExtras ()

Get the extras for this session.

Returns
Bundle

getFlags

added in API level 21
long getFlags ()

Get the flags for this session. Flags are defined in MediaSession.

Returns
long The current set of flags for the session.

getMetadata

added in API level 21
MediaMetadata getMetadata ()

Get the current metadata for this session.

Returns
MediaMetadata The current MediaMetadata or null.

getPackageName

added in API level 21
String getPackageName ()

Get the session owner's package name.

Returns
String The package name of of the session owner.

getPlaybackInfo

added in API level 21
MediaController.PlaybackInfo getPlaybackInfo ()

Get the current playback info for this session.

Returns
MediaController.PlaybackInfo The current playback info or null.

getPlaybackState

added in API level 21
PlaybackState getPlaybackState ()

Get the current playback state for this session.

Returns
PlaybackState The current PlaybackState or null

getQueue

added in API level 21
List<MediaSession.QueueItem> getQueue ()

Get the current play queue for this session if one is set. If you only care about the current item getMetadata() should be used.

Returns
List<MediaSession.QueueItem> The current play queue or null.

getQueueTitle

added in API level 21
CharSequence getQueueTitle ()

Get the queue title for this session.

Returns
CharSequence

getRatingType

added in API level 21
int getRatingType ()

Get the rating type supported by the session. One of:

Returns
int The supported rating type

getRepeatMode

int getRepeatMode ()

Get the repeat mode for this session.

Returns
int The latest repeat mode set to the session, or REPEAT_MODE_NONE if not set.

getSessionActivity

added in API level 21
PendingIntent getSessionActivity ()

Get an intent for launching UI associated with this session if one exists.

Returns
PendingIntent A PendingIntent to launch UI or null.

getSessionToken

added in API level 21
MediaSession.Token getSessionToken ()

Get the token for the session this is connected to.

Returns
MediaSession.Token The token for the connected session.

getTransportControls

added in API level 21
MediaController.TransportControls getTransportControls ()

Get a MediaController.TransportControls instance to send transport actions to this session.

Returns
MediaController.TransportControls A transport controls instance.

isShuffleModeEnabled

boolean isShuffleModeEnabled ()

Return whether the shuffle mode is enabled for this session.

Returns
boolean true if the shuffle mode is enabled, false if disabled or not set.

registerCallback

added in API level 21
void registerCallback (MediaController.Callback callback)

Registers a callback to receive updates from the Session. Updates will be posted on the caller's thread.

Parameters
callback MediaController.Callback: The callback object, must not be null.

registerCallback

added in API level 21
void registerCallback (MediaController.Callback callback, 
                Handler handler)

Registers a callback to receive updates from the session. Updates will be posted on the specified handler's thread.

Parameters
callback MediaController.Callback: The callback object, must not be null.
handler Handler: The handler to post updates on. If null the callers thread will be used.

removeQueueItem

void removeQueueItem (MediaDescription description)

Remove the first occurrence of the specified MediaSession.QueueItem with the given description in the play queue of the associated session. Not all sessions may support this.

Parameters
description MediaDescription: The MediaDescription for denoting the MediaSession.QueueItem to be removed.
Throws
UnsupportedOperationException If this session doesn't support this.

See also:

removeQueueItemAt

void removeQueueItemAt (int index)

Remove an queue item at the specified position in the play queue of this session. Not all sessions may support this.

Parameters
index int: The index of the element to be removed.
Throws
UnsupportedOperationException If this session doesn't support this.

See also:

sendCommand

added in API level 21
void sendCommand (String command, 
                Bundle args, 
                ResultReceiver cb)

Sends a generic command to the session. It is up to the session creator to decide what commands and parameters they will support. As such, commands should only be sent to sessions that the controller owns.

Parameters
command String: The command to send
args Bundle: Any parameters to include with the command
cb ResultReceiver: The callback to receive the result on

setVolumeTo

added in API level 21
void setVolumeTo (int value, 
                int flags)

Set the volume of the output this session is playing on. The command will be ignored if it does not support VOLUME_CONTROL_ABSOLUTE. The flags in AudioManager may be used to affect the handling.

Parameters
value int: The value to set it to, between 0 and the reported max.
flags int: Flags from AudioManager to include with the volume request.

See also:

unregisterCallback

added in API level 21
void unregisterCallback (MediaController.Callback callback)

Unregisters the specified callback. If an update has already been posted you may still receive it after calling this method.

Parameters
callback MediaController.Callback: The callback to remove.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.