MediaSession2Service

public abstract class MediaSession2Service
extends Service

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ android.media.MediaSession2Service


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

Service containing MediaSession2.

Summary

Nested classes

class MediaSession2Service.MediaNotification

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

Constants

String SERVICE_INTERFACE

The Intent that must be declared as handled by the service.

Inherited constants

Public constructors

MediaSession2Service()

Public methods

final void addSession(MediaSession2 session)

Adds a session to this service.

final List<MediaSession2> getSessions()

Gets the list of MediaSession2s that you've added to this service.

IBinder onBind(Intent intent)

Return the communication channel to the service. If you override this method you must call through to the superclass implementation.

void onCreate()

Called by the system when the service is first created.

void onDestroy()

Called by the system to notify that it is no longer used and is being removed.

abstract MediaSession2 onGetSession(MediaSession2.ControllerInfo controllerInfo)

Called when a MediaController2 is created with the this service's Session2Token.

abstract MediaSession2Service.MediaNotification onUpdateNotification(MediaSession2 session)

Called to update the media notification when the playback state changes.

final void removeSession(MediaSession2 session)

Removes a session from this service.

Inherited methods

Constants

SERVICE_INTERFACE

Added in API level 29
public static final String SERVICE_INTERFACE

The Intent that must be declared as handled by the service.

Constant Value: "android.media.MediaSession2Service"

Public constructors

MediaSession2Service

Added in API level 29
public MediaSession2Service ()

Public methods

addSession

Added in API level 29
public final void addSession (MediaSession2 session)

Adds a session to this service.

Added session will be removed automatically when it's closed, or removed when removeSession(MediaSession2) is called.

Parameters
session MediaSession2: a session to be added. This value cannot be null.

getSessions

Added in API level 29
public final List<MediaSession2> getSessions ()

Gets the list of MediaSession2s that you've added to this service.

Returns
List<MediaSession2> sessions This value cannot be null.

onBind

Added in API level 29
public IBinder onBind (Intent intent)

Return the communication channel to the service. May return null if clients can not bind to the service. The returned IBinder is usually for a complex interface that has been described using aidl.

Note that unlike other application components, calls on to the IBinder interface returned here may not happen on the main thread of the process. More information about the main thread can be found in Processes and Threads.

If you override this method you must call through to the superclass implementation.

Parameters
intent Intent: This value cannot be null.

Returns
IBinder This value may be null.

onCreate

Added in API level 29
public void onCreate ()

Called by the system when the service is first created. Do not call this method directly.

Override this method if you need your own initialization. Derived classes MUST call through to the super class's implementation of this method.
If you override this method you must call through to the superclass implementation.

onDestroy

Added in API level 29
public void onDestroy ()

Called by the system to notify that it is no longer used and is being removed. Do not call this method directly.

Override this method if you need your own clean up. Derived classes MUST call through to the super class's implementation of this method.
If you override this method you must call through to the superclass implementation.

onGetSession

Added in API level 29
public abstract MediaSession2 onGetSession (MediaSession2.ControllerInfo controllerInfo)

Called when a MediaController2 is created with the this service's Session2Token. Return the session for telling the controller which session to connect. Return null to reject the connection from this controller.

Session returned here will be added to this service automatically. You don't need to call addSession(android.media.MediaSession2) for that.

This method is always called on the main thread.

Parameters
controllerInfo MediaSession2.ControllerInfo: information of the controller which is trying to connect. This value cannot be null.

Returns
MediaSession2 a MediaSession2 instance for the controller to connect to, or null to reject connection

onUpdateNotification

Added in API level 29
public abstract MediaSession2Service.MediaNotification onUpdateNotification (MediaSession2 session)

Called to update the media notification when the playback state changes.

If playback is active and a notification is returned, the service uses it to become a foreground service. If playback is not active then the notification is still posted, but the service does not become a foreground service.

Apps must request the Manifest.permission.FOREGROUND_SERVICE permission in order to use this API. For apps targeting Build.VERSION_CODES.TIRAMISU or later, notifications will only be posted if the app has also been granted the Manifest.permission.POST_NOTIFICATIONS permission.

Parameters
session MediaSession2: the session for which an updated media notification is required. This value cannot be null.

Returns
MediaSession2Service.MediaNotification the MediaNotification. Can be null.

removeSession

Added in API level 29
public final void removeSession (MediaSession2 session)

Removes a session from this service.

Parameters
session MediaSession2: a session to be removed. This value cannot be null.