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 Media3 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.BuilderThis API is not generally intended for third party application developers. | 
| 
        
        
        
        
        class | MediaSession2.ControllerInfoThis API is not generally intended for third party application developers. | 
| 
        
        
        
        
        class | MediaSession2.SessionCallbackThis 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  | 
| 
        
        
        
        
        
        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
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 benull. | 
| args | Bundle: optional arguments
 This value may benull. | 
cancelSessionCommand
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 benull. | 
| token | Object: the token which is returned fromsendSessionCommand(ControllerInfo, Session2Command, Bundle).
 This value cannot benull. | 
close
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
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
public String getId ()
Returns the session ID
| Returns | |
|---|---|
| String | This value cannot be null. | 
getToken
public Session2Token getToken ()
Returns the Session2Token for creating MediaController2.
| Returns | |
|---|---|
| Session2Token | This value cannot be null. | 
isPlaybackActive
public boolean isPlaybackActive ()
Returns whether the playback is active (i.e. playing something)
| Returns | |
|---|---|
| boolean | trueif the playback active,falseotherwise. | 
sendSessionCommand
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 benull. | 
| command | Session2Command: the session command
 This value cannot benull. | 
| args | Bundle: optional arguments
 This value may benull. | 
| Returns | |
|---|---|
| Object | a token which will be sent together in SessionCallback.onCommandResultwhen its result is received.
 This value cannot benull. | 
setPlaybackActive
public void setPlaybackActive (boolean playbackActive)
Sets whether the playback is active (i.e. playing something)
| Parameters | |
|---|---|
| playbackActive | boolean:trueif the playback active,falseotherwise. | 
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.
