Stay organized with collections
Save and categorize content based on your preferences.
open class MediaController2 : AutoCloseable
This API is not generally intended for third party application developers. Use the AndroidX Media3 session Library for consistent behavior across all devices. Allows an app to interact with an active MediaSession2
or a MediaSession2Service
which would provide MediaSession2
. Media buttons and other commands can be sent to the session.
Summary
Nested classes |
|
This API is not generally intended for third party application developers.
|
abstract |
This API is not generally intended for third party application developers.
|
Public methods |
open Unit |
Cancels the session command previously sent.
|
open Unit |
|
open Session2Token? |
Returns Session2Token of the connected session.
|
open Boolean |
Returns whether the session's playback is active.
|
open Any |
Sends a session command to the session
|
Public methods
cancelSessionCommand
open fun cancelSessionCommand(token: Any): Unit
Cancels the session command previously sent.
close
open fun close(): Unit
Exceptions |
java.lang.Exception |
if this resource cannot be closed |
getConnectedToken
open fun getConnectedToken(): Session2Token?
Returns Session2Token
of the connected session. If it is not connected yet, it returns null
.
This may differ with the Session2Token
from the constructor. For example, if the controller is created with the token for MediaSession2Service
, this would return token for the MediaSession2
in the service.
Return |
Session2Token? |
Session2Token of the connected session, or null if not connected |
isPlaybackActive
open fun isPlaybackActive(): Boolean
Returns whether the session's playback is active.
Return |
Boolean |
true if playback active. false otherwise. |
sendSessionCommand
open fun sendSessionCommand(
command: Session2Command,
args: Bundle?
): Any
Sends a session command to the session
Parameters |
command |
Session2Command: the session command This value cannot be null . |
args |
Bundle?: optional arguments This value may be null . |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# MediaController2\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nMediaController2\n================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/media/MediaController2 \"View this page in Java\") \n\n```\nopen class MediaController2 : AutoCloseable\n```\n\n|---|-------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.media.MediaController2](#) |\n\nThis API is not generally intended for third party application developers. Use the [AndroidX](https://developer.android.com/jetpack/androidx.html) [Media3 session Library](https://developer.android.com/media/media3/session/control-playback) for consistent behavior across all devices. Allows an app to interact with an active [MediaSession2](/reference/kotlin/android/media/MediaSession2) or a [MediaSession2Service](/reference/kotlin/android/media/MediaSession2Service) which would provide [MediaSession2](/reference/kotlin/android/media/MediaSession2). Media buttons and other commands can be sent to the session.\n\nSummary\n-------\n\n| Nested classes ||\n|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | [Builder](/reference/kotlin/android/media/MediaController2.Builder) This API is not generally intended for third party application developers. |\n| abstract | [ControllerCallback](/reference/kotlin/android/media/MediaController2.ControllerCallback) This API is not generally intended for third party application developers. |\n\n| Public methods ||\n|-----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [cancelSessionCommand](#cancelSessionCommand(kotlin.Any))`(`token:` `[Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)`)` Cancels the session command previously sent. |\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [close](#close())`()` \u003cbr /\u003e |\n| open [Session2Token](/reference/kotlin/android/media/Session2Token)? | [getConnectedToken](#getConnectedToken())`()` Returns [Session2Token](/reference/kotlin/android/media/Session2Token) of the connected session. |\n| open [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [isPlaybackActive](#isPlaybackActive())`()` Returns whether the session's playback is active. |\n| open [Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) | [sendSessionCommand](#sendSessionCommand(android.media.Session2Command,%20android.os.Bundle))`(`command:` `[Session2Command](/reference/kotlin/android/media/Session2Command)`, `args:` `[Bundle](../os/Bundle.html#)?`)` Sends a session command to the session |\n\nPublic methods\n--------------\n\n### cancelSessionCommand\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun cancelSessionCommand(token: Any): Unit\n```\n\nCancels the session command previously sent.\n\n| Parameters ||\n|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `token` | [Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html): the token which is returned from [sendSessionCommand](#sendSessionCommand(android.media.Session2Command,%20android.os.Bundle)). This value cannot be `null`. |\n\n### close\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun close(): Unit\n```\n\n| Exceptions ||\n|-----------------------|-----------------------------------|\n| `java.lang.Exception` | if this resource cannot be closed |\n\n### getConnectedToken\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun getConnectedToken(): Session2Token?\n```\n\nReturns [Session2Token](/reference/kotlin/android/media/Session2Token) of the connected session. If it is not connected yet, it returns `null`.\n\nThis may differ with the [Session2Token](/reference/kotlin/android/media/Session2Token) from the constructor. For example, if the controller is created with the token for [MediaSession2Service](/reference/kotlin/android/media/MediaSession2Service), this would return token for the [MediaSession2](/reference/kotlin/android/media/MediaSession2) in the service.\n\n| Return ||\n|-----------------------------------------------------------------|--------------------------------------------------------------------|\n| [Session2Token](/reference/kotlin/android/media/Session2Token)? | Session2Token of the connected session, or `null` if not connected |\n\n### isPlaybackActive\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun isPlaybackActive(): Boolean\n```\n\nReturns whether the session's playback is active.\n\n| Return ||\n|------------------------------------------------------------------------------------|-----------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true` if playback active. `false` otherwise. |\n\n**See Also**\n\n- [android.media.MediaController2.ControllerCallback#onPlaybackActiveChanged(MediaController2, boolean)](/reference/kotlin/android/media/MediaController2.ControllerCallback#onPlaybackActiveChanged(android.media.MediaController2,%20kotlin.Boolean)) \n\n### sendSessionCommand\n\nAdded in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun sendSessionCommand(\n command: Session2Command, \n args: Bundle?\n): Any\n```\n\nSends a session command to the session\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----------|----------------------------------------------------------------------------------------------------------------------|\n| `command` | [Session2Command](/reference/kotlin/android/media/Session2Command): the session command This value cannot be `null`. |\n| `args` | [Bundle](../os/Bundle.html#)?: optional arguments This value may be `null`. |\n\n| Return ||\n|----------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) | a token which will be sent together in [ControllerCallback.onCommandResult](/reference/kotlin/android/media/MediaController2.ControllerCallback#onCommandResult(android.media.MediaController2,%20kotlin.Any,%20android.media.Session2Command,%20android.media.Session2Command.Result)) when its result is received. This value cannot be `null`. |"]]