public abstract class MediaSessionService
extends Service

   ↳ android.content.Context
     ↳ android.content.ContextWrapper
         ↳ androidx.media2.session.MediaSessionService

Base class for media session services, which is the service containing MediaSession.

It's highly recommended for an app to use this if it wants to keep media playback in the background.

Here are the benefits of using MediaSessionService.

  • Another app can know that your app supports MediaSession even when your app isn't running.
  • Another app can start playback of your app even when your app isn't running.
For example, user's voice command can start playback of your app even when it's not running.

To extend this class, adding followings directly to your AndroidManifest.xml.

 <service android:name="component_name_of_your_implementation" >
     <action android:name="androidx.media2.session.MediaSessionService" />

You may also declare
for compatibility with MediaBrowserCompat. This service can handle it automatically.

It's recommended for an app to have a single MediaSessionService declared in the manifest. Otherwise, your app might be shown twice in the list of the Auto/Wearable, or another app fails to pick the right session service when it wants to start the playback of this app. If you want to provide multiple sessions here, take a look at Supporting Multiple Sessions.

Topics covered here:

  1. Service Lifecycle
  2. Permissions
  3. Supporting Multiple Sessions

Service Lifecycle

Session service is a bound service. When a MediaController is created for the session service, the controller binds to the session service. onGetSession(ControllerInfo) would be called inside of the onBind(Intent).

After the binding, session's MediaSession.SessionCallback.onConnect(MediaSession, MediaSession.ControllerInfo) will be called to accept or reject connection request from a controller. If the connection is rejected, the controller will unbind. If it's accepted, the controller will be available to use and keep binding.

When playback is started for this session service, onUpdateNotification(MediaSession) is called for the playback's session and service would become a foreground service. It's needed to keep playback after the controller is destroyed. The session service becomes background service when all playbacks are stopped. Apps targeting API Build.VERSION_CODES.P or later must request the permission Manifest.permission.FOREGROUND_SERVICE in order to make the service foreground.

The service is destroyed when the all sessions are closed, or no media controller is binding to the session while the service is not running as a foreground service.


Any app can bind to the session service with controller, but the controller can be used only if the session service accepted the connection request through MediaSession.SessionCallback.onConnect(MediaSession, MediaSession.ControllerInfo).

Supporting Multiple Sessions

Generally speaking, multiple sessions aren't necessary for most media apps. One exception is if your app can play multiple media content at the same time, but only for the playback of video-only media or remote playback, since audio focus policy recommends not playing multiple audio content at the same time. Also keep in mind that multiple media sessions would make Android Auto and Bluetooth device with display to show your apps multiple times, because they list up media sessions, not media apps.

However, if you're capable of handling multiple