lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

belongs to Maven artifact androidx.media:media:1.0.0-alpha3

MediaBrowserServiceCompat

public abstract class MediaBrowserServiceCompat
extends Service

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


Base class for media browse services.

Media browse 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 MediaSessionCompat.

To extend this class, you must declare the service in your manifest file with an intent filter with the SERVICE_INTERFACE action. For example:

 <service android:name=".MyMediaBrowserServiceCompat"
          android:label="@string/service_name" >
     <intent-filter>
         <action android:name="android.media.browse.MediaBrowserService" />
     </intent-filter>
 </service>
 

Developer Guides

For information about building your media application, read the Media Apps developer guide.

Summary

Nested classes

class MediaBrowserServiceCompat.BrowserRoot

Contains information that the browser service needs to send to the client when first connected. 

class MediaBrowserServiceCompat.Result<T>

Completion handler for asynchronous callback methods in MediaBrowserServiceCompat

Constants

String SERVICE_INTERFACE

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

Inherited constants

Public constructors

MediaBrowserServiceCompat()

Public methods

void dump(FileDescriptor fd, PrintWriter writer, String[] args)
final Bundle getBrowserRootHints()

Gets the root hints sent from the currently connected MediaBrowserCompat.

final MediaSessionManager.RemoteUserInfo getCurrentBrowserInfo()

Gets the browser information who sent the current request.

MediaSessionCompat.Token getSessionToken()

Gets the session token, or null if it has not yet been created or if it has been destroyed.

void notifyChildrenChanged(String parentId)

Notifies all connected media browsers that the children of the specified parent id have changed in some way.

void notifyChildrenChanged(String parentId, Bundle options)

Notifies all connected media browsers that the children of the specified parent id have changed in some way.

IBinder onBind(Intent intent)
void onCreate()
void onCustomAction(String action, Bundle extras, Result<Bundle> result)

Called to request a custom action to this service.

abstract MediaBrowserServiceCompat.BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints)

Called to get the root information for browsing by a particular client.

void onLoadChildren(String parentId, Result<List<MediaBrowserCompat.MediaItem>> result, Bundle options)

Called to get information about the children of a media item.

abstract void onLoadChildren(String parentId, Result<List<MediaBrowserCompat.MediaItem>> result)

Called to get information about the children of a media item.

void onLoadItem(String itemId, Result<MediaBrowserCompat.MediaItem> result)

Called to get information about a specific media item.

void onSearch(String query, Bundle extras, Result<List<MediaBrowserCompat.MediaItem>> result)

Called to get the search result.

void setSessionToken(MediaSessionCompat.Token token)

Call to set the media session.

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.browse.MediaBrowserService"

Public constructors

MediaBrowserServiceCompat

public MediaBrowserServiceCompat ()

Public methods

dump

public void dump (FileDescriptor fd, 
                PrintWriter writer, 
                String[] args)

Parameters
fd FileDescriptor

writer PrintWriter

args String

getBrowserRootHints

public final Bundle getBrowserRootHints ()

Gets the root hints sent from the currently connected MediaBrowserCompat. The root hints are service-specific arguments included in an optional bundle sent to the media browser service when connecting and retrieving the root id for browsing, or null if none. The contents of this bundle may affect the information returned when browsing.

Note that this will return null when connected to MediaBrowser and running on API 23 or lower.

Returns
Bundle

Throws
IllegalStateException If this method is called outside of onLoadChildren(String, MediaBrowserServiceCompat.Result>), onLoadItem(String, MediaBrowserServiceCompat.Result) or onSearch(String, Bundle, MediaBrowserServiceCompat.Result>).

getCurrentBrowserInfo

public final MediaSessionManager.RemoteUserInfo getCurrentBrowserInfo ()

Gets the browser information who sent the current request.

Returns
MediaSessionManager.RemoteUserInfo

Throws
IllegalStateException If this method is called outside of onGetRoot(String, int, Bundle) or onLoadChildren(String, MediaBrowserServiceCompat.Result>) or onLoadItem(String, MediaBrowserServiceCompat.Result).

getSessionToken

public MediaSessionCompat.Token getSessionToken ()

Gets the session token, or null if it has not yet been created or if it has been destroyed.

Returns
MediaSessionCompat.Token

notifyChildrenChanged

public void notifyChildrenChanged (String parentId)

Notifies all connected media browsers that the children of the specified parent id have changed in some way. This will cause browsers to fetch subscribed content again.

Parameters
parentId String: The id of the parent media item whose children changed.

notifyChildrenChanged

public void notifyChildrenChanged (String parentId, 
                Bundle options)

Notifies all connected media browsers that the children of the specified parent id have changed in some way. This will cause browsers to fetch subscribed content again.

Parameters
parentId String: The id of the parent media item whose children changed.

options Bundle: A bundle of service-specific arguments to send to the media browse. The contents of this bundle may contain the information about the change.

onBind

public IBinder onBind (Intent intent)

Parameters
intent Intent

Returns
IBinder

onCreate

public void onCreate ()

onCustomAction

public void onCustomAction (String action, 
                Bundle extras, 
                Result<Bundle> result)

Called to request a custom action to this service.

Implementations must call either MediaBrowserServiceCompat.Result.sendResult(T) or MediaBrowserServiceCompat.Result.sendError(Bundle). If the requested custom action will be an expensive operation MediaBrowserServiceCompat.Result.detach() may be called before returning from this function, and then the service can send the result later when the custom action is completed. Implementation can also call MediaBrowserServiceCompat.Result.sendProgressUpdate(Bundle) to send an interim update to the requester.

If the requested custom action is not supported by this service, call MediaBrowserServiceCompat.Result.sendError(Bundle). The default implementation will invoke MediaBrowserServiceCompat.Result.sendError(Bundle).

Parameters
action String: The custom action sent from the media browser.

extras Bundle: The bundle of service-specific arguments sent from the media browser.

result Result: The MediaBrowserServiceCompat.Result to send the result of the requested custom action.

onGetRoot

public abstract MediaBrowserServiceCompat.BrowserRoot onGetRoot (String clientPackageName, 
                int clientUid, 
                Bundle rootHints)

Called to get the root information for browsing by a particular client.

The implementation should verify that the client package has permission to access browse media information before returning the root id; it should return null if the client is not allowed to access this information.

Parameters
clientPackageName String: The package name of the application which is requesting access to browse media.

clientUid int: The uid of the application which is requesting access to browse media.

rootHints Bundle: An optional bundle of service-specific arguments to send to the media browse service when connecting and retrieving the root id for browsing, or null if none. The contents of this bundle may affect the information returned when browsing.

Returns
MediaBrowserServiceCompat.BrowserRoot The MediaBrowserServiceCompat.BrowserRoot for accessing this app's content or null.

onLoadChildren

public void onLoadChildren (String parentId, 
                Result<List<MediaBrowserCompat.MediaItem>> result, 
                Bundle options)

Called to get information about the children of a media item.

Implementations must call result.sendResult with the list of children. If loading the children will be an expensive operation that should be performed on another thread, result.detach may be called before returning from this function, and then result.sendResult called when the loading is complete.

In case the media item does not have any children, call MediaBrowserServiceCompat.Result.sendResult(T) with an empty list. When the given parentId is invalid, implementations must call result.sendResult with null, which will invoke MediaBrowserCompat.SubscriptionCallback.onError(String).

Parameters
parentId String: The id of the parent media item whose children are to be queried.

result Result: The Result to send the list of children to.

options Bundle: A bundle of service-specific arguments sent from the media browse. The information returned through the result should be affected by the contents of this bundle.

onLoadChildren

public abstract void onLoadChildren (String parentId, 
                Result<List<MediaBrowserCompat.MediaItem>> result)

Called to get information about the children of a media item.

Implementations must call result.sendResult with the list of children. If loading the children will be an expensive operation that should be performed on another thread, result.detach may be called before returning from this function, and then result.sendResult called when the loading is complete.

In case the media item does not have any children, call MediaBrowserServiceCompat.Result.sendResult(T) with an empty list. When the given parentId is invalid, implementations must call result.sendResult with null, which will invoke MediaBrowserCompat.SubscriptionCallback.onError(String).

Parameters
parentId String: The id of the parent media item whose children are to be queried.

result Result: The Result to send the list of children to.

onLoadItem

public void onLoadItem (String itemId, 
                Result<MediaBrowserCompat.MediaItem> result)

Called to get information about a specific media item.

Implementations must call result.sendResult. If loading the item will be an expensive operation result.detach may be called before returning from this function, and then result.sendResult called when the item has been loaded.

When the given itemId is invalid, implementations must call result.sendResult with null.

The default implementation will invoke MediaBrowserCompat.ItemCallback.onError(String).

Parameters
itemId String: The id for the specific MediaBrowserCompat.MediaItem.

result Result: The Result to send the item to, or null if the id is invalid.

onSearch

public void onSearch (String query, 
                Bundle extras, 
                Result<List<MediaBrowserCompat.MediaItem>> result)

Called to get the search result.

Implementations must call result.sendResult. If the search will be an expensive operation result.detach may be called before returning from this function, and then result.sendResult called when the search has been completed.

In case there are no search results, call result.sendResult with an empty list. In case there are some errors happened, call result.sendResult with null, which will invoke MediaBrowserCompat.SearchCallback.onError(String, Bundle).

The default implementation will invoke MediaBrowserCompat.SearchCallback.onError(String, Bundle).

Parameters
query String: The search query sent from the media browser. It contains keywords separated by space.

extras Bundle: The bundle of service-specific arguments sent from the media browser.

result Result: The MediaBrowserServiceCompat.Result to send the search result.

setSessionToken

public void setSessionToken (MediaSessionCompat.Token token)

Call to set the media session.

This should be called as soon as possible during the service's startup. It may only be called once.

Parameters
token MediaSessionCompat.Token: The token for the service's MediaSessionCompat.