Added in API level 21

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

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

Send the specified media button event to the session.

Bundle?

Get the extras for this session.

Long

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!>?

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

Get the session's tag for debugging purposes.

MediaController.TransportControls

Get a TransportControls instance to send transport actions to the associated session.

Unit

Registers a callback to receive updates from the Session.

Unit

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

Unregisters the specified callback.

Public constructors

MediaController

Added in API level 21
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

Added in API level 21
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.

dispatchMediaButtonEvent

Added in API level 21
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

Added in API level 21
fun getExtras(): Bundle?

Get the extras for this session.

Return
Bundle? This value may be null.

getFlags

Added in API level 21
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

Added in API level 21
fun getMetadata(): MediaMetadata?

Get the current metadata for this session.

Return
MediaMetadata? The current MediaMetadata or null.

getPackageName

Added in API level 21
fun getPackageName(): String!

Get the session owner's package name.

Return
String! The package name of the session owner.

getPlaybackInfo

Added in API level 21
fun getPlaybackInfo(): MediaController.PlaybackInfo

Returns the current playback info for this session.

Return
MediaController.PlaybackInfo This value cannot be null.

getPlaybackState

Added in API level 21
fun getPlaybackState(): PlaybackState?

Get the current playback state for this session.

Return
PlaybackState? The current PlaybackState or null

getQueue

Added in API level 21
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

Added in API level 21
fun getQueueTitle(): CharSequence?

Get the queue title for this session.

Return
CharSequence? This value may be null.

getRatingType

Added in API level 21
fun getRatingType(): Int

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

Return
Int The supported rating type

getSessionActivity

Added in API level 21
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

Added in API level 29
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

Added in API level 21
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

Added in API level 30
fun getTag(): String

Get the session's tag for debugging purposes.

Return
String The session's tag. This value cannot be null.

getTransportControls

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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

Added in API level 21
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.

unregisterCallback

Added in API level 21
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.