MediaSession
open class MediaSession : Closeable
kotlin.Any | |
↳ | androidx.media2.session.MediaSession |
Allows a media app to expose its transport controls and playback information in a process to other processes including the Android framework and other apps. Common use cases are as follows.
- Bluetooth/wired headset key events support
- Android Auto/Wearable support
- Separating UI process and playback process
A MediaSession should be created when an app wants to publish media playback information or handle media keys. In general an app only needs one session for all playback, though multiple sessions can be created to provide finer grain controls of media. See Supporting Multiple Sessions for detail.
If you want to support background playback, MediaSessionService
is preferred instead. With it, your playback can be revived even after playback is finished. See MediaSessionService
for details.
Topics covered here:
- Session Lifecycle
- Thread
- Media key events mapping
- Supporting Multiple Sessions
- Backward compatibility with legacy session APIs
- Backward compatibility with legacy controller APIs
Session Lifecycle
A session can be obtained by Builder
. The owner of the session may pass its session token to other processes to allow them to create a MediaController
to interact with the session.
When a session receive transport control commands, the session sends the commands directly to the underlying media player set by Builder
or updatePlayer
.
When an app is finished performing playback it must call #close() to clean up the session and notify any controllers. The app is responsible for closing the underlying player after closing the session. is closed.
Thread
MediaSession
objects are thread safe, but should be used on the thread on the looper.
Media key events mapping
Here's the table of per key event.
Supporting Multiple Sessions
Generally speaking, multiple sessions aren't necessary for most media apps. One exception is if your app can play multiple media content at the same time, but only for the playback of video-only media or remote playback, since audio focus policy recommends not playing multiple audio content at the same time. Also keep in mind that multiple media sessions would make Android Auto and Bluetooth device with display to show your apps multiple times, because they list up media sessions, not media apps.Backward compatibility with legacy session APIs
An activeMediaSessionCompat
is internally created with the MediaSession for the backward compatibility. It's used to handle incoming connection and command from MediaControllerCompat
. And helps to utilize existing APIs that are built with legacy media session APIs. Use getSessionCompatToken
for getting the token for the underlying MediaSessionCompat.
Backward compatibility with legacy controller APIs
In addition to themedia2 controller
API, session also supports connection from the legacy controller API - framework controller
and AndroidX controller compat
. However, ControllerInfo
may not be precise for legacy controller. See ControllerInfo
for the details.
Unknown package name nor UID doesn't mean that you should disallow connection nor commands. For SDK levels where such issue happen, session tokens could only be obtained by trusted apps (e.g. Bluetooth, Auto, ...), so it may be better for you to allow them as you did with legacy session.
Summary
Nested classes | |
---|---|
Builder for |
|
Button for a |
|
Information of a controller. |
|
abstract |
Callback to be called for all incoming commands from |
Public methods | |
---|---|
open Unit |
broadcastCustomCommand(@NonNull command: SessionCommand, @Nullable args: Bundle?) Broadcasts a custom command to all connected controllers. |
open Unit |
close() |
open MutableList<MediaSession.ControllerInfo!> |
Returns the list of connected controller. |
open String |
getId() Gets the session ID |
open SessionPlayer |
Gets the underlying |
open MediaSessionCompat.Token |
Gets the |
open SessionToken |
getToken() Returns the |
open ListenableFuture<SessionResult!> |
sendCustomCommand(@NonNull controller: MediaSession.ControllerInfo, @NonNull command: SessionCommand, @Nullable args: Bundle?) Sends a custom command to a specific controller. |
open Unit |
setAllowedCommands(@NonNull controller: MediaSession.ControllerInfo, @NonNull commands: SessionCommandGroup) Sets the new allowed command group for the controller. |
open ListenableFuture<SessionResult!> |
setCustomLayout(@NonNull controller: MediaSession.ControllerInfo, @NonNull layout: MutableList<MediaSession.CommandButton!>) Sets ordered list of |
open Unit |
updatePlayer(@NonNull player: SessionPlayer) Updates the underlying |
Public methods
broadcastCustomCommand
open fun broadcastCustomCommand(
@NonNull command: SessionCommand,
@Nullable args: Bundle?
): Unit
Broadcasts a custom command to all connected controllers.
This is synchronous call and doesn't wait for result from the controller. Use sendCustomCommand(ControllerInfo, SessionCommand, Bundle)
for getting the result.
A command is not accepted if it is not a custom command.
Parameters | |
---|---|
command |
SessionCommand: a command |
args |
Bundle?: optional argument |
close
open fun close(): Unit
getConnectedControllers
@NonNull open fun getConnectedControllers(): MutableList<MediaSession.ControllerInfo!>
Returns the list of connected controller.
Return | |
---|---|
MutableList<MediaSession.ControllerInfo!> |
list of ControllerInfo |
getPlayer
@NonNull open fun getPlayer(): SessionPlayer
Gets the underlying SessionPlayer
.
When the session is closed, it returns the lastly set player.
Return | |
---|---|
SessionPlayer |
player. |
getSessionCompatToken
@NonNull open fun getSessionCompatToken(): MediaSessionCompat.Token
Gets the MediaSessionCompat.Token
for the MediaSessionCompat created internally by this session.
Return | |
---|---|
MediaSessionCompat.Token |
MediaSessionCompat.Token |
getToken
@NonNull open fun getToken(): SessionToken
Returns the SessionToken
for creating MediaController
.
sendCustomCommand
@NonNull open fun sendCustomCommand(
@NonNull controller: MediaSession.ControllerInfo,
@NonNull command: SessionCommand,
@Nullable args: Bundle?
): ListenableFuture<SessionResult!>
Sends a custom command to a specific controller.
A command is not accepted if it is not a custom command.
Parameters | |
---|---|
command |
SessionCommand: a command |
args |
Bundle?: optional argument |
setAllowedCommands
open fun setAllowedCommands(
@NonNull controller: MediaSession.ControllerInfo,
@NonNull commands: SessionCommandGroup
): Unit
Sets the new allowed command group for th