Added in API level 21

MediaBrowser


class MediaBrowser
kotlin.Any
   ↳ android.media.browse.MediaBrowser

Browses media content offered by a link MediaBrowserService.

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

Standard Extra Data

These are the current standard fields that can be used as extra data via subscribe(java.lang.String,android.os.Bundle,android.media.browse.MediaBrowser.SubscriptionCallback), unsubscribe(java.lang.String,android.media.browse.MediaBrowser.SubscriptionCallback), and SubscriptionCallback.onChildrenLoaded(String, List, Bundle).

Summary

Nested classes

Callbacks for connection related events.

abstract

Callback for receiving the result of getItem.

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

abstract

Callbacks for subscription related events.

Constants
static String

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

static String

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

Public constructors
MediaBrowser(context: Context!, serviceComponent: ComponentName!, callback: MediaBrowser.ConnectionCallback!, rootHints: Bundle!)

Creates a media browser for the specified media browser service.

Public methods
Unit

Connects to the media browser service.

Unit

Disconnects from the media browser service.

Bundle?

Gets any extras for the media service.

Unit

Retrieves a specific MediaItem from the connected service.

String

Gets the root id.

ComponentName

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

MediaSession.Token

Gets the media session token associated with the media browser.

Boolean

Returns whether the browser is connected to the service.

Unit

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

Unit
subscribe(parentId: String, options: Bundle, callback: MediaBrowser.SubscriptionCallback)

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.

Unit
unsubscribe(parentId: String)

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

Unit

Unsubscribes for changes to the children of the specified media id through a callback.

Constants

EXTRA_PAGE

Added in API level 24
static val EXTRA_PAGE: String

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 0.

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

See Also

EXTRA_PAGE_SIZE

Added in API level 24
static val EXTRA_PAGE_SIZE: String

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.

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

See Also

Public constructors

MediaBrowser

Added in API level 21
MediaBrowser(
    context: Context!,
    serviceComponent: ComponentName!,
    callback: MediaBrowser.ConnectionCallback!,
    rootHints: Bundle!)

Creates a media browser for the specified media browser service.

Parameters
context Context!: The context.
serviceComponent ComponentName!: The component name of the media browser service.
callback MediaBrowser.ConnectionCallback!: The connection callback.
rootHints Bundle!: An optional bundle of service-specific arguments to send 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.

Public methods

connect

Added in API level 21
fun connect(): Unit

Connects to the media browser service.

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

disconnect

Added in API level 21
fun disconnect(): Unit

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

getExtras

Added in API level 21
fun getExtras(): Bundle?

Gets any extras for the media service.

Return
Bundle? This value may be null.
Exceptions
java.lang.IllegalStateException if not connected.

getItem

Added in API level 23
fun getItem(
    mediaId: String,
    cb: MediaBrowser.ItemCallback
): Unit

Retrieves a specific 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. This value cannot be null.
cb MediaBrowser.ItemCallback: The callback to receive the result on. This value cannot be null.

getRoot

Added in API level 21
fun getRoot(): String

Gets the root id.

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

Return
String This value cannot be null.
Exceptions
java.lang.IllegalStateException if not connected.

getServiceComponent

Added in API level 21
fun getServiceComponent(): ComponentName

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

Return
ComponentName This value cannot be null.

getSessionToken

Added in API level 21
fun getSessionToken(): MediaSession.Token

Gets the media session token associated with the media browser.

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

Return
MediaSession.Token The session token for the browser, never null.
Exceptions
java.lang.IllegalStateException if not connected.

isConnected

Added in API level 21
fun isConnected(): Boolean

Returns whether the browser is connected to the service.

subscribe

Added in API level 21
fun subscribe(
    parentId: String,
    callback: MediaBrowser.SubscriptionCallback
): Unit

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. This value cannot be null.
callback MediaBrowser.SubscriptionCallback: The callback to receive the list of children. This value cannot be null.

subscribe

Added in API level 24
fun subscribe(
    parentId: String,
    options: Bundle,
    callback: MediaBrowser.SubscriptionCallback
): Unit

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. This value cannot be null.
options Bundle: The bundle of service-specific arguments to send to the media browser service. The contents of this bundle may affect the information returned when browsing. This value cannot be null.
callback MediaBrowser.SubscriptionCallback: The callback to receive the list of children. This value cannot be null.

unsubscribe

Added in API level 21
fun unsubscribe(parentId: String): Unit

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. This value cannot be null.

unsubscribe

Added in API level 24
fun unsubscribe(
    parentId: String,
    callback: MediaBrowser.SubscriptionCallback
): Unit

Unsubscribes for changes to the children of the specified media id through a callback.

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. This value cannot be null.
callback MediaBrowser.SubscriptionCallback: A callback sent to the media browser service to subscribe. This value cannot be null.