Skip to content

Most visited

Recently visited

navigation
added in version 24.1.0
belongs to Maven artifact com.android.support:support-media-compat:27.0.0

MediaBrowserCompat

public final class MediaBrowserCompat
extends Object

java.lang.Object
   ↳ android.support.v4.media.MediaBrowserCompat


Browses media content offered by a MediaBrowserServiceCompat.

This object is not thread-safe. All calls should happen on the thread on which the browser was constructed.

All callback methods will be called from the thread on which the browser was constructed.

Developer Guides

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

Summary

Nested classes

class MediaBrowserCompat.ConnectionCallback

Callbacks for connection related events. 

class MediaBrowserCompat.CustomActionCallback

Callback for receiving the result of sendCustomAction(String, Bundle, MediaBrowserCompat.CustomActionCallback)

class MediaBrowserCompat.ItemCallback

Callback for receiving the result of getItem(String, MediaBrowserCompat.ItemCallback)

class MediaBrowserCompat.MediaItem

A class with information on a single media item for use in browsing/searching media. 

class MediaBrowserCompat.SearchCallback

Callback for receiving the result of search(String, Bundle, MediaBrowserCompat.SearchCallback)

class MediaBrowserCompat.SubscriptionCallback

Callbacks for subscription related events. 

Constants

String CUSTOM_ACTION_DOWNLOAD

Predefined custom action to ask the connected service to download a specific MediaBrowserCompat.MediaItem for offline playback.

String CUSTOM_ACTION_REMOVE_DOWNLOADED_FILE

Predefined custom action to ask the connected service to remove the downloaded file of MediaBrowserCompat.MediaItem by the download action.

String EXTRA_DOWNLOAD_PROGRESS

Used as a float extra field to denote the current progress during download.

String EXTRA_MEDIA_ID

Used as a string extra field to denote the target MediaBrowserCompat.MediaItem.

String EXTRA_PAGE

Used as an int extra field to denote the page number to subscribe.

String EXTRA_PAGE_SIZE

Used as an int extra field to denote the number of media items in a page.

Public constructors

MediaBrowserCompat(Context context, ComponentName serviceComponent, MediaBrowserCompat.ConnectionCallback callback, Bundle rootHints)

Creates a media browser for the specified media browse service.

Public methods

void connect()

Connects to the media browse service.

void disconnect()

Disconnects from the media browse service.

Bundle getExtras()

Gets any extras for the media service.

void getItem(String mediaId, MediaBrowserCompat.ItemCallback cb)

Retrieves a specific MediaBrowserCompat.MediaItem from the connected service.

String getRoot()

Gets the root id.

ComponentName getServiceComponent()

Gets the service component that the media browser is connected to.

MediaSessionCompat.Token getSessionToken()

Gets the media session token associated with the media browser.

boolean isConnected()

Returns whether the browser is connected to the service.

void search(String query, Bundle extras, MediaBrowserCompat.SearchCallback callback)

Searches media items from the connected service.

void sendCustomAction(String action, Bundle extras, MediaBrowserCompat.CustomActionCallback callback)

Sends a custom action to the connected service.

void subscribe(String parentId, Bundle options, MediaBrowserCompat.SubscriptionCallback callback)

Queries with service-specific arguments for information about the media items that are contained within the specified id and subscribes to receive updates when they change.

void subscribe(String parentId, MediaBrowserCompat.SubscriptionCallback callback)

Queries for information about the media items that are contained within the specified id and subscribes to receive updates when they change.

void unsubscribe(String parentId)

Unsubscribes for changes to the children of the specified media id.

void unsubscribe(String parentId, MediaBrowserCompat.SubscriptionCallback callback)

Unsubscribes for changes to the children of the specified media id.

Inherited methods

From class java.lang.Object

Constants

CUSTOM_ACTION_DOWNLOAD

added in version 26.1.0
String CUSTOM_ACTION_DOWNLOAD

Predefined custom action to ask the connected service to download a specific MediaBrowserCompat.MediaItem for offline playback. The id of the media item must be passed in an extra bundle. The download progress might be delivered to the browser via onProgressUpdate(String, Bundle, Bundle).

See also:

Constant Value: "android.support.v4.media.action.DOWNLOAD"

CUSTOM_ACTION_REMOVE_DOWNLOADED_FILE

added in version 26.1.0
String CUSTOM_ACTION_REMOVE_DOWNLOADED_FILE

Predefined custom action to ask the connected service to remove the downloaded file of MediaBrowserCompat.MediaItem by the download action. The id of the media item must be passed in an extra bundle.

See also:

Constant Value: "android.support.v4.media.action.REMOVE_DOWNLOADED_FILE"

EXTRA_DOWNLOAD_PROGRESS

added in version 26.1.0
String EXTRA_DOWNLOAD_PROGRESS

Used as a float extra field to denote the current progress during download. The value of this field must be a float number within [0.0, 1.0].

See also:

Constant Value: "android.media.browse.extra.DOWNLOAD_PROGRESS"

EXTRA_MEDIA_ID

added in version 26.1.0
String EXTRA_MEDIA_ID

Used as a string extra field to denote the target MediaBrowserCompat.MediaItem.

See also:

Constant Value: "android.media.browse.extra.MEDIA_ID"

EXTRA_PAGE

added in version 24.1.0
String EXTRA_PAGE

Used as an int extra field to denote the page number to subscribe. The value of EXTRA_PAGE should be greater than or equal to 1.

See also:

Constant Value: "android.media.browse.extra.PAGE"

EXTRA_PAGE_SIZE

added in version 24.1.0
String EXTRA_PAGE_SIZE

Used as an int extra field to denote the number of media items in a page. The value of EXTRA_PAGE_SIZE should be greater than or equal to 1.

See also:

Constant Value: "android.media.browse.extra.PAGE_SIZE"

Public constructors

MediaBrowserCompat

added in version 24.1.0
MediaBrowserCompat (Context context, 
                ComponentName serviceComponent, 
                MediaBrowserCompat.ConnectionCallback callback, 
                Bundle rootHints)

Creates a media browser for the specified media browse service.

Parameters
context Context: The context.

serviceComponent ComponentName: The component name of the media browse service.

callback MediaBrowserCompat.ConnectionCallback: The connection callback.

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.

See also:

Public methods

connect

added in version 24.1.0
void connect ()

Connects to the media browse service.

The connection callback specified in the constructor will be invoked when the connection completes or fails.

disconnect

added in version 24.1.0
void disconnect ()

Disconnects from the media browse service. After this, no more callbacks will be received.

getExtras

added in version 24.1.0
Bundle getExtras ()

Gets any extras for the media service.

Returns
Bundle

Throws
IllegalStateException if not connected.

getItem

added in version 24.1.0
void getItem (String mediaId, 
                MediaBrowserCompat.ItemCallback cb)

Retrieves a specific MediaBrowserCompat.MediaItem from the connected service. Not all services may support this, so falling back to subscribing to the parent's id should be used when unavailable.

Parameters
mediaId String: The id of the item to retrieve.

cb MediaBrowserCompat.ItemCallback: The callback to receive the result on.

getRoot

added in version 24.1.0
String getRoot ()

Gets the root id.

Note that the root id may become invalid or change when when the browser is disconnected.

Returns
String

Throws
IllegalStateException if not connected.

getServiceComponent

added in version 24.1.0
ComponentName getServiceComponent ()

Gets the service component that the media browser is connected to.

Returns
ComponentName

getSessionToken

added in version 24.1.0
MediaSessionCompat.Token getSessionToken ()

Gets the media session token associated with the media browser.

Note that the session token may become invalid or change when when the browser is disconnected.

Returns
MediaSessionCompat.Token The session token for the browser, never null.

Throws
IllegalStateException if not connected.

isConnected

added in version 24.1.0
boolean isConnected ()

Returns whether the browser is connected to the service.

Returns
boolean

search

added in version 25.4.0
void search (String query, 
                Bundle extras, 
                MediaBrowserCompat.SearchCallback callback)

Searches media items from the connected service. Not all services may support this, and onError(String, Bundle) will be called if not implemented.

Parameters
query String: The search query that contains keywords separated by space. Should not be an empty string.

extras Bundle: The bundle of service-specific arguments to send to the media browser service. The contents of this bundle may affect the search result.

callback MediaBrowserCompat.SearchCallback: The callback to receive the search result. Must be non-null.

Throws
IllegalStateException if the browser is not connected to the media browser service.

sendCustomAction

added in version 25.4.0
void sendCustomAction (String action, 
                Bundle extras, 
                MediaBrowserCompat.CustomActionCallback callback)

Sends a custom action to the connected service. If the service doesn't support the given action, onError(String, Bundle, Bundle) will be called.

Parameters
action String: The custom action that will be sent to the connected service. Should not be an empty string.

extras Bundle: The bundle of service-specific arguments to send to the media browser service.

callback MediaBrowserCompat.CustomActionCallback: The callback to receive the result of the custom action.

See also:

subscribe

added in version 24.1.0
void subscribe (String parentId, 
                Bundle options, 
                MediaBrowserCompat.SubscriptionCallback callback)

Queries with service-specific arguments for information about the media items that are contained within the specified id and subscribes to receive updates when they change.

The list of subscriptions is maintained even when not connected and is restored after the reconnection. It is ok to subscribe while not connected but the results will not be returned until the connection completes.

If the id is already subscribed with a different callback then the new callback will replace the previous one and the child data will be reloaded.

Parameters
parentId String: The id of the parent media item whose list of children will be subscribed.

options Bundle: A bundle of service-specific arguments to send to the media browse service. The contents of this bundle may affect the information returned when browsing.

callback MediaBrowserCompat.SubscriptionCallback: The callback to receive the list of children.

subscribe

added in version 24.1.0
void subscribe (String parentId, 
                MediaBrowserCompat.SubscriptionCallback callback)

Queries for information about the media items that are contained within the specified id and subscribes to receive updates when they change.

The list of subscriptions is maintained even when not connected and is restored after the reconnection. It is ok to subscribe while not connected but the results will not be returned until the connection completes.

If the id is already subscribed with a different callback then the new callback will replace the previous one and the child data will be reloaded.

Parameters
parentId String: The id of the parent media item whose list of children will be subscribed.

callback MediaBrowserCompat.SubscriptionCallback: The callback to receive the list of children.

unsubscribe

added in version 24.1.0
void unsubscribe (String parentId)

Unsubscribes for changes to the children of the specified media id.

The query callback will no longer be invoked for results associated with this id once this method returns.

Parameters
parentId String: The id of the parent media item whose list of children will be unsubscribed.

unsubscribe

added in version 24.1.0
void unsubscribe (String parentId, 
                MediaBrowserCompat.SubscriptionCallback callback)

Unsubscribes for changes to the children of the specified media id.

The query callback will no longer be invoked for results associated with this id once this method returns.

Parameters
parentId String: The id of the parent media item whose list of children will be unsubscribed.

callback MediaBrowserCompat.SubscriptionCallback: A callback sent to the media browse service to subscribe.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)