Join us on the livestream at Android Dev Summit on 7-8 November 2018, starting at 10AM PDT!

MediaLibraryService2

public abstract class MediaLibraryService2
extends MediaSessionService2

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ androidx.media2.MediaSessionService2
           ↳ androidx.media2.MediaLibraryService2


Base class for media library services.

Media library services enable applications to browse media content provided by an application and ask the application to start playing it. They may also be used to control content that is already playing by way of a MediaSession2.

When extending this class, also add the following to your AndroidManifest.xml.

 <service android:name="component_name_of_your_implementation" >
   <intent-filter>
     <action android:name="android.media.MediaLibraryService2" />
   </intent-filter>
 </service>

The MediaLibraryService2 class derives from MediaSessionService2. IDs shouldn't be shared between the MediaSessionService2 and MediaSession2. By default, an empty string will be used for ID of the service. If you want to specify an ID, declare metadata in the manifest as follows.

Summary

Nested classes

class MediaLibraryService2.LibraryRoot

Contains information that the library service needs to send to the client when MediaBrowser2.getLibraryRoot(Bundle) is called. 

class MediaLibraryService2.MediaLibrarySession

Session for the MediaLibraryService2

Constants

String SERVICE_INTERFACE

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

Inherited constants

Public constructors

MediaLibraryService2()

Public methods

IBinder onBind(Intent intent)

Default implementation for MediaSessionService2 to handle incoming binding request.

void onCreate()

Default implementation for MediaSessionService2 to initialize session service.

abstract MediaLibraryService2.MediaLibrarySession onCreateSession(String sessionId)

Called when another app requested to start 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.MediaLibraryService2"

Public constructors

MediaLibraryService2

public MediaLibraryService2 ()

Public methods

onBind

public IBinder onBind (Intent intent)

Default implementation for MediaSessionService2 to handle incoming binding request. If the request is for getting the session, the intent will have action SERVICE_INTERFACE.

Override this method if this service also needs to handle binder requests other than SERVICE_INTERFACE. Derived classes MUST call through to the super class's implementation of this method.

Returns
IBinder Binder

onCreate

public void onCreate ()

Default implementation for MediaSessionService2 to initialize session service.

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

onCreateSession

public abstract MediaLibraryService2.MediaLibrarySession onCreateSession (String sessionId)

Called when another app requested to start this service.

Library service will accept or reject the connection with the MediaLibraryService2.MediaLibrarySession.MediaLibrarySessionCallback in the created session.

Service wouldn't run if null is returned or session's ID doesn't match with the expected ID that you've specified through the AndroidManifest.xml.

This method will be called on the main thread.

Parameters
sessionId String: session id written in the AndroidManifest.xml.

Returns
MediaLibraryService2.MediaLibrarySession a new library session

Throws
RuntimeException if returned session is invalid