Added in API level 29

MediaSession2Service


abstract class MediaSession2Service : Service
kotlin.Any
   ↳ 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
open

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

Constants
static String

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

Inherited constants
Public constructors

Public methods
Unit

Adds a session to this service.

MutableList<MediaSession2!>

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

open IBinder?
onBind(intent: Intent)

Return the communication channel to the service.

open Unit

Called by the system when the service is first created.

open Unit

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

abstract MediaSession2?

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

abstract MediaSession2Service.MediaNotification?

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

Unit

Removes a session from this service.

Inherited functions

Constants

SERVICE_INTERFACE

Added in API level 29
static val SERVICE_INTERFACE: String

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

Value: "android.media.MediaSession2Service"

Public constructors

MediaSession2Service

Added in API level 29
MediaSession2Service()

Public methods

addSession

Added in API level 29
fun addSession(session: MediaSession2): Unit

Adds a session to this service.

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

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

getSessions

Added in API level 29
fun getSessions(): MutableList<MediaSession2!>

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

Return
MutableList<MediaSession2!> sessions This value cannot be null.

onBind

Added in API level 29
open fun onBind(intent: Intent): IBinder?

Return the communication channel to the service. May return null if clients can not bind to the service. The returned android.os.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.
Return
IBinder? This value may be null.

onCreate

Added in API level 29
open fun onCreate(): Unit

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
open fun onDestroy(): Unit

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
abstract fun onGetSession(controllerInfo: MediaSession2.ControllerInfo): MediaSession2?

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.
Return
MediaSession2? a MediaSession2 instance for the controller to connect to, or null to reject connection

onUpdateNotification

Added in API level 29
abstract fun onUpdateNotification(session: MediaSession2): MediaSession2Service.MediaNotification?

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 android.Manifest.permission#FOREGROUND_SERVICE permission in order to use this API. For apps targeting android.os.Build.VERSION_CODES#TIRAMISU or later, notifications will only be posted if the app has also been granted the android.Manifest.permission#POST_NOTIFICATIONS permission.

Parameters
session MediaSession2: the session for which an updated media notification is required. This value cannot be null.
Return
MediaSession2Service.MediaNotification? the MediaNotification. Can be null.

removeSession

Added in API level 29
fun removeSession(session: MediaSession2): Unit

Removes a session from this service.

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