Android APIs
public final class

MediaControllerCompat

extends Object
java.lang.Object
   ↳ android.support.v4.media.session.MediaControllerCompat

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

MediaController objects are thread-safe.

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

Summary

Nested Classes
class MediaControllerCompat.Callback Callback for receiving updates on from the session. 
class MediaControllerCompat.PlaybackInfo Holds information about the way volume is handled for this session. 
class MediaControllerCompat.TransportControls Interface for controlling media playback on a session. 
Public Constructors
MediaControllerCompat(Context context, MediaSessionCompat session)
Creates a media controller from a session.
MediaControllerCompat(Context context, MediaSessionCompat.Token sessionToken)
Creates a media controller from a session token which may have been obtained from another process.
Public Methods
boolean dispatchMediaButtonEvent(KeyEvent keyEvent)
Send the specified media button event to the session.
Object getMediaController()
Gets the underlying framework MediaController object.
MediaMetadataCompat getMetadata()
Get the current metadata for this session.
MediaControllerCompat.PlaybackInfo getPlaybackInfo()
Get the current playback info for this session.
PlaybackStateCompat getPlaybackState()
Get the current playback state for this session.
int getRatingType()
Get the rating type supported by the session.
MediaControllerCompat.TransportControls getTransportControls()
Get a MediaControllerCompat.TransportControls instance for this session.
void registerCallback(MediaControllerCompat.Callback callback)
Adds a callback to receive updates from the Session.
void registerCallback(MediaControllerCompat.Callback callback, Handler handler)
Adds a callback to receive updates from the session.
void sendCommand(String command, Bundle params, ResultReceiver cb)
Sends a generic command to the session.
void unregisterCallback(MediaControllerCompat.Callback callback)
Stop receiving updates on the specified callback.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public MediaControllerCompat (Context context, MediaSessionCompat session)

Creates a media controller from a session.

Parameters
session The session to be controlled.

public MediaControllerCompat (Context context, MediaSessionCompat.Token sessionToken)

Creates a media controller from a session token which may have been obtained from another process.

Parameters
sessionToken The token of the session to be controlled.
Throws
RemoteException if the session is not accessible.

Public Methods

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 Object getMediaController ()

Gets the underlying framework MediaController object.

This method is only supported on API 21+.

Returns

public MediaMetadataCompat getMetadata ()

Get the current metadata for this session.

Returns
  • The current MediaMetadata or null.

public MediaControllerCompat.PlaybackInfo getPlaybackInfo ()

Get the current playback info for this session.

Returns
  • The current playback info or null.

public PlaybackStateCompat getPlaybackState ()

Get the current playback state for this session.

Returns
  • The current PlaybackState or null

public int getRatingType ()

Returns
  • The supported rating type

public MediaControllerCompat.TransportControls getTransportControls ()

Get a MediaControllerCompat.TransportControls instance for this session.

Returns
  • A controls instance

public void registerCallback (MediaControllerCompat.Callback callback)

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

Parameters
callback The callback object, must not be null.

public void registerCallback (MediaControllerCompat.Callback callback, Handler handler)

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

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

public void sendCommand (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

public void unregisterCallback (MediaControllerCompat.Callback callback)

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

Parameters
callback The callback to remove