Android APIs
public final class

MediaController

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

Class Overview

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 directly if you have a MediaSessionToken from the session owner.

MediaController objects are thread-safe.

Summary

Nested Classes
class MediaController.Callback Callback for receiving updates on from the session. 
class MediaController.TransportControls Interface for controlling media playback on a session. 
Public Methods
void addCallback(MediaController.Callback cb, Handler handler)
Adds a callback to receive updates from the session.
void addCallback(MediaController.Callback cb)
Adds a callback to receive updates from the Session.
boolean dispatchMediaButtonEvent(KeyEvent keyEvent)
Send the specified media button event to the session.
static MediaController fromToken(MediaSessionToken token)
Get a new MediaController for a MediaSessionToken.
MediaMetadata getMetadata()
Get the current metadata for this session.
PlaybackState getPlaybackState()
Get the current playback state for this session.
int getRatingType()
Get the rating type supported by the session.
MediaController.TransportControls getTransportControls()
Get a MediaController.TransportControls instance for this session.
void removeCallback(MediaController.Callback cb)
Stop receiving updates on the specified callback.
void sendControlCommand(String command, Bundle params, ResultReceiver cb)
Sends a generic command to the session.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void addCallback (MediaController.Callback cb, Handler handler)

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

Parameters
cb Cannot be null.
handler The handler to post updates on. If null the callers thread will be used

public void addCallback (MediaController.Callback cb)

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

Parameters
cb The callback object, must not be null

public 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 The media button event to dispatch.
Returns
  • true if the event was sent to the session, false otherwise.

public static MediaController fromToken (MediaSessionToken token)

Get a new MediaController for a MediaSessionToken. If successful the controller returned will be connected to the session that generated the token.

Parameters
token The session token to use
Returns
  • A controller for the session or null

public MediaMetadata getMetadata ()

Get the current metadata for this session.

Returns
  • The current MediaMetadata or null.

public PlaybackState getPlaybackState ()

Get the current playback state for this session.

Returns
  • The current PlaybackState or null

public int getRatingType ()

Returns
  • The supported rating type

public MediaController.TransportControls getTransportControls ()

Get a MediaController.TransportControls instance for this session.

Returns
  • A controls instance

public void removeCallback (MediaController.Callback cb)

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

Parameters
cb The callback to remove

public void sendControlCommand (String command, Bundle params, 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 The command to send
params Any parameters to include with the command
cb The callback to receive the result on