public final class MediaControllerCompat extends Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
MediaControllerCompat.Callback
Callback for receiving updates on from the session.
|
static class |
MediaControllerCompat.PlaybackInfo
Holds information about the way volume is handled for this session.
|
static class |
MediaControllerCompat.TransportControls
Interface for controlling media playback on a session.
|
Constructor and Description |
---|
MediaControllerCompat(Context context,
MediaSessionCompat.Token sessionToken)
Creates a media controller from a session token which may have
been obtained from another process.
|
MediaControllerCompat(Context context,
MediaSessionCompat session)
Creates a media controller from a session.
|
Modifier and Type | Method and Description |
---|---|
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.
|
Object |
getMediaController()
Gets the underlying framework
MediaController object. |
MediaMetadataCompat |
getMetadata()
Get the current metadata for this session.
|
String |
getPackageName()
Get the session owner's package name.
|
MediaControllerCompat.PlaybackInfo |
getPlaybackInfo()
Get the current playback info for this session.
|
PlaybackStateCompat |
getPlaybackState()
Get the current playback state for this session.
|
List<MediaSessionCompat.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.
|
PendingIntent |
getSessionActivity()
Get an intent for launching UI associated with this session if one
exists.
|
MediaSessionCompat.Token |
getSessionToken()
Get the token for the session this controller is connected to.
|
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 |
setVolumeTo(int value,
int flags)
Set the volume of the output this session is playing on.
|
void |
unregisterCallback(MediaControllerCompat.Callback callback)
Stop receiving updates on the specified callback.
|
public MediaControllerCompat(Context context, MediaSessionCompat session)
session
- The session to be controlled.public MediaControllerCompat(Context context, MediaSessionCompat.Token sessionToken) throws RemoteException
sessionToken
- The token of the session to be controlled.RemoteException
- if the session is not accessible.public MediaControllerCompat.TransportControls getTransportControls()
MediaControllerCompat.TransportControls
instance for this session.public boolean dispatchMediaButtonEvent(KeyEvent keyEvent)
keyEvent
- The media button event to dispatch.public PlaybackStateCompat getPlaybackState()
public MediaMetadataCompat getMetadata()
public List<MediaSessionCompat.QueueItem> getQueue()
getMetadata()
should be used.public CharSequence getQueueTitle()
public Bundle getExtras()
public int getRatingType()
public long getFlags()
MediaSessionCompat
.public MediaControllerCompat.PlaybackInfo getPlaybackInfo()
public PendingIntent getSessionActivity()
PendingIntent
to launch UI or null.public MediaSessionCompat.Token getSessionToken()
public void setVolumeTo(int value, int flags)
VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE
. The flags in
AudioManager
may be used to affect the handling.value
- The value to set it to, between 0 and the reported max.flags
- Flags from AudioManager
to include with the volume
request.getPlaybackInfo()
public void adjustVolume(int direction, int flags)
AudioManager.ADJUST_LOWER
,
AudioManager.ADJUST_RAISE
, or AudioManager.ADJUST_SAME
.
The command will be ignored if the session does not support
VolumeProviderCompat.VOLUME_CONTROL_RELATIVE
or
VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE
. The flags in
AudioManager
may be used to affect the handling.direction
- The direction to adjust the volume in.flags
- Any flags to pass with the command.getPlaybackInfo()
public void registerCallback(MediaControllerCompat.Callback callback)
callback
- The callback object, must not be null.public void registerCallback(MediaControllerCompat.Callback callback, Handler handler)
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 unregisterCallback(MediaControllerCompat.Callback callback)
callback
- The callback to removepublic void sendCommand(String command, Bundle params, ResultReceiver cb)
command
- The command to sendparams
- Any parameters to include with the commandcb
- The callback to receive the result onpublic String getPackageName()
public Object getMediaController()
MediaController
object.
This method is only supported on API 21+.
MediaController
object, or null if none.