Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

MediaSession2Service

public abstract class MediaSession2Service
extends Service

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


Service containing MediaSession2.

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

Summary

Nested classes

class MediaSession2Service.MediaNotification

Returned by MediaSession2Service.onUpdateNotification(android.media.MediaSession2) for making session service foreground service to keep playback running in the background. 

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 onGetPrimarySession()

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

abstract MediaSession2Service.MediaNotification onUpdateNotification(MediaSession2 session)

Called when notification UI needs update.

final void removeSession(MediaSession2 session)

Removes a session from this service.

Inherited methods

Constants

SERVICE_INTERFACE

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

public MediaSession2Service ()

Public methods

addSession

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 must never be null.

getSessions

public final List<MediaSession2> getSessions ()

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

Returns
List<MediaSession2> sessions This value will never be null.

onBind

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 must never be null.

Returns
IBinder This value may be null.

onCreate

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

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.

onGetPrimarySession

public abstract MediaSession2 onGetPrimarySession ()

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

Primary session is the highest priority session that this service manages. Here are some recommendations of the primary session.

  1. When there's no MediaSession2, create and return a new session. Resume the playback that the app has the lastly played with the new session. The behavior is what framework expects when the framework sends key events to the service.
  2. When there's multiple MediaSession2s, pick the session that has the lastly started the playback. This is the same way as the framework prioritize sessions to receive media key events.

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

Session service will accept or reject the connection with the MediaSession2.SessionCallback in the session returned here.

This method is always called on the main thread.

Returns
MediaSession2 a new session This value will never be null.

onUpdateNotification

public abstract MediaSession2Service.MediaNotification onUpdateNotification (MediaSession2 session)

Called when notification UI needs update. Override this method to show or cancel your own notification UI.

This would be called on MediaSession2's callback executor when playback state is changed.

With the notification returned here, the service becomes foreground service when the playback is started. Apps must request the permission Manifest.permission.FOREGROUND_SERVICE in order to use this API. It becomes background service after the playback is stopped.

Parameters
session MediaSession2: a session that needs notification update. This value must never be null.

Returns
MediaSession2Service.MediaNotification a MediaNotification. Can be null.

removeSession

public final void removeSession (MediaSession2 session)

Removes a session from this service.

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