MediaSession2

public class MediaSession2
extends Object implements AutoCloseable

java.lang.Object
   ↳ android.media.MediaSession2


This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

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.

Summary

Nested classes

class MediaSession2.Builder

This API is not generally intended for third party application developers. 

class MediaSession2.ControllerInfo

This API is not generally intended for third party application developers. 

class MediaSession2.SessionCallback

This API is not generally intended for third party application developers. 

Public methods

void broadcastSessionCommand(Session2Command command, Bundle args)

Broadcasts a session command to all the connected controllers

void cancelSessionCommand(MediaSession2.ControllerInfo controller, Object token)

Cancels the session command previously sent.

void close()

Closes this resource, relinquishing any underlying resources.

List<MediaSession2.ControllerInfo> getConnectedControllers()

Gets the list of the connected controllers

String getId()

Returns the session ID

Session2Token getToken()

Returns the Session2Token for creating MediaController2.

boolean isPlaybackActive()

Returns whether the playback is active (i.e.

Object sendSessionCommand(MediaSession2.ControllerInfo controller, Session2Command command, Bundle args)

Sends a session command to a specific controller

void setPlaybackActive(boolean playbackActive)

Sets whether the playback is active (i.e.

Inherited methods

Public methods

broadcastSessionCommand

Added in API level 29
public void broadcastSessionCommand (Session2Command command, 
                Bundle args)

Broadcasts a session command to all the connected controllers

Parameters
command Session2Command: the session command This value cannot be null.

args Bundle: optional arguments This value may be null.

cancelSessionCommand

Added in API level 29
public void cancelSessionCommand (MediaSession2.ControllerInfo controller, 
                Object token)

Cancels the session command previously sent.

Parameters
controller MediaSession2.ControllerInfo: the controller to get the session command This value cannot be null.

token Object: the token which is returned from sendSessionCommand(ControllerInfo, Session2Command, Bundle). This value cannot be null.

close

Added in API level 29
public void close ()

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

getConnectedControllers

Added in API level 29
public List<MediaSession2.ControllerInfo> getConnectedControllers ()

Gets the list of the connected controllers

Returns
List<MediaSession2.ControllerInfo> list of the connected controllers. This value cannot be null.

getId

Added in API level 29
public String getId ()

Returns the session ID

Returns
String This value cannot be null.

getToken

Added in API level 29
public Session2Token getToken ()

Returns the Session2Token for creating MediaController2.

Returns
Session2Token This value cannot be null.

isPlaybackActive

Added in API level 29
public boolean isPlaybackActive ()

Returns whether the playback is active (i.e. playing something)

Returns
boolean true if the playback active, false otherwise.

sendSessionCommand

Added in API level 29
public Object sendSessionCommand (MediaSession2.ControllerInfo controller, 
                Session2Command command, 
                Bundle args)

Sends a session command to a specific controller

Parameters
controller MediaSession2.ControllerInfo: the controller to get the session command This value cannot be null.

command Session2Command: the session command This value cannot be null.

args Bundle: optional arguments This value may be null.

Returns
Object a token which will be sent together in SessionCallback#onCommandResult when its result is received. This value cannot be null.

setPlaybackActive

Added in API level 29
public void setPlaybackActive (boolean playbackActive)

Sets whether the playback is active (i.e. playing something)

Parameters
playbackActive boolean: true if the playback active, false otherwise.