MediaController
class MediaController
kotlin.Any | |
↳ | 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 | |
---|---|
abstract |
Callback for receiving updates from the session. |
Holds information about the current playback and how audio is handled for this session. |
|
Interface for controlling media playback on a session. |
Public constructors | |
---|---|
MediaController(context: Context, token: MediaSession.Token) Create a new MediaController from a session's token. |
Public methods | |
---|---|
Unit |
adjustVolume(direction: Int, flags: Int) 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? |
Get the extras for this session. |
Long |
getFlags() Get the flags for this session. |
MediaMetadata? |
Get the current metadata for this session. |
String! |
Get the session owner's package name. |
MediaController.PlaybackInfo |
Returns the current playback info for this session. |
PlaybackState? |
Get the current playback state for this session. |
MutableList<MediaSession.QueueItem!>? |
getQueue() Get the current play queue for this session if one is set. |
CharSequence? |
Get the queue title for this session. |
Int |
Get the rating type supported by the session. |
PendingIntent? |
Get an intent for launching UI associated with this session if one exists. |
Bundle |
Gets the additional session information which was set when the session was created. |
MediaSession.Token |
Get the token for the session this is connected to. |
String |
getTag() Get the session's tag for debugging purposes. |
MediaController.TransportControls |
Get a |
Unit |
registerCallback(callback: MediaController.Callback) Registers a callback to receive updates from the Session. |
Unit |
registerCallback(callback: MediaController.Callback, handler: Handler?) Registers a callback to receive updates from the session. |
Unit |
sendCommand(command: String, args: Bundle?, cb: ResultReceiver?) Sends a generic command to the session. |
Unit |
setVolumeTo(value: Int, flags: Int) Set the volume of the output this session is playing on. |
Unit |
unregisterCallback(callback: MediaController.Callback) Unregisters the specified callback. |
Public constructors
MediaController
MediaController(
context: Context,
token: MediaSession.Token)
Create a new MediaController from a session's token.
Parameters | |
---|---|
context |
Context: The caller's context. This value cannot be null . |
token |
MediaSession.Token: The token for the session. This value cannot be null . |
Public methods
adjustVolume
fun adjustVolume(
direction: Int,
flags: Int
): Unit
Adjust the volume of the output this session is playing on. The direction must be one of AudioManager.ADJUST_LOWER
, AudioManager.ADJUST_RAISE
, or AudioManager.ADJUST_SAME
. The command will be ignored if the session does not support VolumeProvider.VOLUME_CONTROL_RELATIVE
or VolumeProvider.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
fun dispatchMediaButtonEvent(keyEvent: KeyEvent): Boolean
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. This value cannot be null . |
Return | |
---|---|
Boolean |
true if the event was sent to the session, false otherwise. |
getExtras
fun getExtras(): Bundle?
Get the extras for this session.
Return | |
---|---|
Bundle? |
This value may be null . |
getFlags
fun getFlags(): Long
Get the flags for this session. Flags are defined in MediaSession
.
Return | |
---|---|
Long |
The current set of flags for the session. |
getMetadata
fun getMetadata(): MediaMetadata?
Get the current metadata for this session.
Return | |
---|---|
MediaMetadata? |
The current MediaMetadata or null. |
getPackageName
fun getPackageName(): String!
Get the session owner's package name.
Return | |
---|---|
String! |
The package name of the session owner. |
getPlaybackInfo
fun getPlaybackInfo(): MediaController.PlaybackInfo
Returns the current playback info for this session.
Return | |
---|---|
MediaController.PlaybackInfo |
This value cannot be null . |
getPlaybackState
fun getPlaybackState(): PlaybackState?
Get the current playback state for this session.
Return | |
---|---|
PlaybackState? |
The current PlaybackState or null |
getQueue
fun getQueue(): MutableList<MediaSession.QueueItem!>?
Get the current play queue for this session if one is set. If you only care about the current item getMetadata()
should be used.
Return | |
---|---|
MutableList<MediaSession.QueueItem!>? |
The current play queue or null. |
getQueueTitle
fun getQueueTitle(): CharSequence?
Get the queue title for this session.
Return | |
---|---|
CharSequence? |
This value may be null . |
getRatingType
fun getRatingType(): Int
Get the rating type supported by the session. One of:
Rating.RATING_NONE
Rating.RATING_HEART
Rating.RATING_THUMB_UP_DOWN
Rating.RATING_3_STARS
Rating.RATING_4_STARS
Rating.RATING_5_STARS
Rating.RATING_PERCENTAGE
Return | |
---|---|
Int |
The supported rating type |
getSessionActivity
fun getSessionActivity(): PendingIntent?
Get an intent for launching UI associated with this session if one exists.
Return | |
---|---|
PendingIntent? |
A PendingIntent to launch UI or null. |
getSessionInfo
fun getSessionInfo(): Bundle
Gets the additional session information which was set when the session was created.
Return | |
---|---|
Bundle |
The additional session information, or an empty Bundle if not set. This value cannot be null . |
getSessionToken
fun getSessionToken(): MediaSession.Token
Get the token for the session this is connected to.
Return | |
---|---|
MediaSession.Token |
The token for the connected session. This value cannot be null . |
getTag
fun getTag(): String
Get the session's tag for debugging purposes.
Return | |
---|---|
String |
The session's tag. This value cannot be null . |
getTransportControls
fun getTransportControls(): MediaController.TransportControls
Get a TransportControls
instance to send transport actions to the associated session.
Return | |
---|---|
MediaController.TransportControls |
A transport controls instance. This value cannot be null . |
registerCallback
fun registerCallback(callback: MediaController.Callback): Unit
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
fun registerCallback(
callback: MediaController.Callback,
handler: Handler?
): Unit
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. |
sendCommand
fun sendCommand(
command: String,
args: Bundle?,
cb: ResultReceiver?
): Unit
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 This value cannot be null . |
args |
Bundle?: Any parameters to include with the command This value may be null . |
cb |
ResultReceiver?: The callback to receive the result on This value may be null . |
setVolumeTo
fun setVolumeTo(
value: Int,
flags: Int
): Unit
Set the volume of the output this session is playing on. The command will be ignored if it does not support VolumeProvider.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
fun unregisterCallback(callback: MediaController.Callback): Unit
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 value cannot be null . |