MediaSession

Added in 1.0.0
Deprecated in 1.3.0

public class MediaSession implements Closeable

Known direct subclasses
MediaLibraryService.MediaLibrarySession

This class is deprecated.

androidx.media2 is deprecated.


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 for details.

Topics covered here:

  1. Session Lifecycle
  2. Thread
  3. Media key events mapping
  4. Supporting Multiple Sessions
  5. Backward compatibility with legacy session APIs
  6. 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.

Key code API
KEYCODE_MEDIA_PLAY play
KEYCODE_MEDIA_PAUSE pause
KEYCODE_MEDIA_NEXT skipToNextPlaylistItem
KEYCODE_MEDIA_PREVIOUS skipToPreviousPlaylistItem
KEYCODE_MEDIA_STOP pause and then seekTo with 0
KEYCODE_MEDIA_FAST_FORWARD onFastForward
KEYCODE_MEDIA_REWIND onRewind

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 active MediaSessionCompat is internally created with the MediaSession for the backward compatibility. It's used to handle incoming connection and command from . 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 the media2 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 types

public final class MediaSession.Builder

This class is deprecated.

androidx.media2 is deprecated.

public final class MediaSession.CommandButton implements VersionedParcelable

This class is deprecated.

androidx.media2 is deprecated.

This class is deprecated.

androidx.media2 is deprecated.

public final class MediaSession.ControllerInfo

This class is deprecated.

androidx.media2 is deprecated.

public abstract class MediaSession.SessionCallback

This class is deprecated.

androidx.media2 is deprecated.

Public methods

void
broadcastCustomCommand(
    @NonNull SessionCommand command,
    @Nullable Bundle args
)

Broadcasts a custom command to all connected controllers.

void
@NonNull List<MediaSession.ControllerInfo>

Returns the list of connected controller.

@NonNull String

Gets the session ID

@NonNull SessionPlayer

Gets the underlying SessionPlayer.

@NonNull MediaSessionCompat.Token

Gets the MediaSessionCompat.Token for the MediaSessionCompat created internally by this session.

@NonNull SessionToken

Returns the SessionToken for creating MediaController.

@NonNull ListenableFuture<SessionResult>
sendCustomCommand(
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull SessionCommand command,
    @Nullable Bundle args
)

Sends a custom command to a specific controller.

void

Sets the new allowed command group for the controller.

@NonNull ListenableFuture<SessionResult>

Sets ordered list of CommandButton for controllers to build UI with it.

void

Updates the underlying SessionPlayer for this session to dispatch incoming event to.

Public methods

broadcastCustomCommand

Added in 1.0.0
Deprecated in 1.3.0
public void broadcastCustomCommand(
    @NonNull SessionCommand command,
    @Nullable Bundle args
)

Broadcasts a custom command to all connected controllers.

This is synchronous call and doesn't wait for result from the controller. Use sendCustomCommand for getting the result.

A command is not accepted if it is not a custom command.

Parameters
@NonNull SessionCommand command

a command

@Nullable Bundle args

optional argument

close

Added in 1.0.0
Deprecated in 1.3.0
public void close()

getConnectedControllers

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull List<MediaSession.ControllerInfogetConnectedControllers()

Returns the list of connected controller.

getId

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull String getId()

Gets the session ID

Returns
@NonNull String

getPlayer

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull SessionPlayer getPlayer()

Gets the underlying SessionPlayer.

When the session is closed, it returns the lastly set player.

Returns
@NonNull SessionPlayer

player.

getSessionCompatToken

Added in 1.1.0
Deprecated in 1.3.0
public @NonNull MediaSessionCompat.Token getSessionCompatToken()

Gets the MediaSessionCompat.Token for the MediaSessionCompat created internally by this session.

getToken

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull SessionToken getToken()

Returns the SessionToken for creating MediaController.

sendCustomCommand

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull ListenableFuture<SessionResultsendCustomCommand(
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull SessionCommand command,
    @Nullable Bundle args
)

Sends a custom command to a specific controller.

A command is not accepted if it is not a custom command.

Parameters
@NonNull SessionCommand command

a command

@Nullable Bundle args

optional argument

setAllowedCommands

Added in 1.0.0
Deprecated in 1.3.0
public void setAllowedCommands(
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull SessionCommandGroup commands
)

Sets the new allowed command group for the controller.

This is synchronous call. Changes in the allowed commands take effect immediately regardless of the controller notified about the change through #onAllowedCommandsChanged(MediaController, SessionCommandGroup)

Parameters
@NonNull MediaSession.ControllerInfo controller

controller to change allowed commands

@NonNull SessionCommandGroup commands

new allowed commands

setCustomLayout

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull ListenableFuture<SessionResultsetCustomLayout(
    @NonNull MediaSession.ControllerInfo controller,
    @NonNull List<MediaSession.CommandButton> layout
)

Sets ordered list of CommandButton for controllers to build UI with it.

It's up to controller's decision how to represent the layout in its own UI. Here are some examples.

Note: layout[i] means a CommandButton at index i in the given list

Controller UX layout Layout example
Row with 3 icons layout[1]layout[0]layout[2]
Row with 5 icons layout[3]layout[1]layout[0]layout[2]layout[4]
Row with 5 icons and an overflow icon, and another expandable row with 5 extra icons layout[3]layout[1]layout[0]layout[2]layout[4]
layout[3]layout[1]layout[0]layout[2]layout[4]

This API can be called in the onConnect.

Parameters
@NonNull MediaSession.ControllerInfo controller

controller to specify layout.

@NonNull List<MediaSession.CommandButton> layout

ordered list of layout.

updatePlayer

Added in 1.0.0
Deprecated in 1.3.0
public void updatePlayer(@NonNull SessionPlayer player)

Updates the underlying SessionPlayer for this session to dispatch incoming event to.

Parameters
@NonNull SessionPlayer player

a player that handles actual media playback in your app