# MediaBrowser

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/android/media/browse/MediaBrowser#nestedclasses)\|[Constants](https://developer.android.com/reference/android/media/browse/MediaBrowser#constants)\|[Ctors](https://developer.android.com/reference/android/media/browse/MediaBrowser#pubctors)\|[Methods](https://developer.android.com/reference/android/media/browse/MediaBrowser#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/media/browse/MediaBrowser#inhmethods)  

# MediaBrowser

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/media/browse/MediaBrowser "View this page in Kotlin")\|Java

`
public

final

class
MediaBrowser
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|-----------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.browse.MediaBrowser |

<br />

*** ** * ** ***

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)](https://developer.android.com/reference/android/media/browse/MediaBrowser#subscribe(java.lang.String,%20android.os.Bundle,%20android.media.browse.MediaBrowser.SubscriptionCallback)),[unsubscribe(java.lang.String, android.media.browse.MediaBrowser.SubscriptionCallback)](https://developer.android.com/reference/android/media/browse/MediaBrowser#unsubscribe(java.lang.String,%20android.media.browse.MediaBrowser.SubscriptionCallback)), and[SubscriptionCallback.onChildrenLoaded(String, List, Bundle)](https://developer.android.com/reference/android/media/browse/MediaBrowser.SubscriptionCallback#onChildrenLoaded(java.lang.String,%20java.util.List<android.media.browse.MediaBrowser.MediaItem>,%20android.os.Bundle)).

- [EXTRA_PAGE](https://developer.android.com/reference/android/media/browse/MediaBrowser#EXTRA_PAGE)
- [EXTRA_PAGE_SIZE](https://developer.android.com/reference/android/media/browse/MediaBrowser#EXTRA_PAGE_SIZE)

<br />

## Summary

|                                                                                                                                                                           ### Nested classes                                                                                                                                                                           ||
|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [MediaBrowser.ConnectionCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.ConnectionCallback) Callbacks for connection related events.                                                                                                                                                                                     |
| ` class` | [MediaBrowser.ItemCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.ItemCallback) Callback for receiving the result of[MediaBrowser.getItem(String, ItemCallback)](https://developer.android.com/reference/android/media/browse/MediaBrowser#getItem(java.lang.String,%20android.media.browse.MediaBrowser.ItemCallback)). |
| ` class` | [MediaBrowser.MediaItem](https://developer.android.com/reference/android/media/browse/MediaBrowser.MediaItem) A class with information on a single media item for use in browsing/searching media.                                                                                                                                                           |
| ` class` | [MediaBrowser.SubscriptionCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.SubscriptionCallback) Callbacks for subscription related events.                                                                                                                                                                               |

|                                                                                                                       ### Constants                                                                                                                        ||
|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | [EXTRA_PAGE](https://developer.android.com/reference/android/media/browse/MediaBrowser#EXTRA_PAGE) Used as an int extra field to denote the page number to subscribe.                  |
| [String](https://developer.android.com/reference/java/lang/String) | [EXTRA_PAGE_SIZE](https://developer.android.com/reference/android/media/browse/MediaBrowser#EXTRA_PAGE_SIZE) Used as an int extra field to denote the number of media items in a page. |

|                                                                                                                                                                                                                                                                                                                                                                ### Public constructors                                                                                                                                                                                                                                                                                                                                                                ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[MediaBrowser](https://developer.android.com/reference/android/media/browse/MediaBrowser#MediaBrowser(android.content.Context,%20android.content.ComponentName,%20android.media.browse.MediaBrowser.ConnectionCallback,%20android.os.Bundle))`(`[Context](https://developer.android.com/reference/android/content/Context)` context, `[ComponentName](https://developer.android.com/reference/android/content/ComponentName)` serviceComponent, `[MediaBrowser.ConnectionCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.ConnectionCallback)` callback, `[Bundle](https://developer.android.com/reference/android/os/Bundle)` rootHints) ` Creates a media browser for the specified media browser service. |

|                                                                                                                                                                                                                                                                                                                                                                                              ### Public methods                                                                                                                                                                                                                                                                                                                                                                                               ||
|-----------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                                                   | ` `[connect](https://developer.android.com/reference/android/media/browse/MediaBrowser#connect())`() ` Connects to the media browser service.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| ` void`                                                                                                   | ` `[disconnect](https://developer.android.com/reference/android/media/browse/MediaBrowser#disconnect())`() ` Disconnects from the media browser service.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ` `[Bundle](https://developer.android.com/reference/android/os/Bundle)                                    | ` `[getExtras](https://developer.android.com/reference/android/media/browse/MediaBrowser#getExtras())`() ` Gets any extras for the media service.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| ` void`                                                                                                   | ` `[getItem](https://developer.android.com/reference/android/media/browse/MediaBrowser#getItem(java.lang.String,%20android.media.browse.MediaBrowser.ItemCallback))`(`[String](https://developer.android.com/reference/java/lang/String)` mediaId, `[MediaBrowser.ItemCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.ItemCallback)` cb) ` Retrieves a specific[MediaItem](https://developer.android.com/reference/android/media/browse/MediaBrowser.MediaItem)from the connected service.                                                                                                                                                                     |
| ` `[String](https://developer.android.com/reference/java/lang/String)                                     | ` `[getRoot](https://developer.android.com/reference/android/media/browse/MediaBrowser#getRoot())`() ` Gets the root id.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ` `[ComponentName](https://developer.android.com/reference/android/content/ComponentName)                 | ` `[getServiceComponent](https://developer.android.com/reference/android/media/browse/MediaBrowser#getServiceComponent())`() ` Gets the service component that the media browser is connected to.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| ` `[MediaSession.Token](https://developer.android.com/reference/android/media/session/MediaSession.Token) | ` `[getSessionToken](https://developer.android.com/reference/android/media/browse/MediaBrowser#getSessionToken())`() ` Gets the media session token associated with the media browser.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| ` boolean`                                                                                                | ` `[isConnected](https://developer.android.com/reference/android/media/browse/MediaBrowser#isConnected())`() ` Returns whether the browser is connected to the service.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ` void`                                                                                                   | ` `[subscribe](https://developer.android.com/reference/android/media/browse/MediaBrowser#subscribe(java.lang.String,%20android.os.Bundle,%20android.media.browse.MediaBrowser.SubscriptionCallback))`(`[String](https://developer.android.com/reference/java/lang/String)` parentId, `[Bundle](https://developer.android.com/reference/android/os/Bundle)` options, `[MediaBrowser.SubscriptionCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.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](https://developer.android.com/reference/android/media/browse/MediaBrowser#subscribe(java.lang.String,%20android.media.browse.MediaBrowser.SubscriptionCallback))`(`[String](https://developer.android.com/reference/java/lang/String)` parentId, `[MediaBrowser.SubscriptionCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.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](https://developer.android.com/reference/android/media/browse/MediaBrowser#unsubscribe(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` parentId) ` Unsubscribes for changes to the children of the specified media id.                                                                                                                                                                                                                                                                                                                                                                                                                    |
| ` void`                                                                                                   | ` `[unsubscribe](https://developer.android.com/reference/android/media/browse/MediaBrowser#unsubscribe(java.lang.String,%20android.media.browse.MediaBrowser.SubscriptionCallback))`(`[String](https://developer.android.com/reference/java/lang/String)` parentId, `[MediaBrowser.SubscriptionCallback](https://developer.android.com/reference/android/media/browse/MediaBrowser.SubscriptionCallback)` callback) ` Unsubscribes for changes to the children of the specified media id through a callback.                                                                                                                                                                                       |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Constants

### EXTRA_PAGE

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final 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 0.

<br />

**See also:**

- [EXTRA_PAGE_SIZE](https://developer.android.com/reference/android/media/browse/MediaBrowser#EXTRA_PAGE_SIZE)

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

### EXTRA_PAGE_SIZE

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final 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.

<br />

**See also:**

- [EXTRA_PAGE](https://developer.android.com/reference/android/media/browse/MediaBrowser#EXTRA_PAGE)

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

## Public constructors

### MediaBrowser

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MediaBrowser (Context context, 
                ComponentName serviceComponent, 
                MediaBrowser.ConnectionCallback callback, 
                Bundle rootHints)
```

Creates a media browser for the specified media browser service.

<br />

|                                                                                                                                     Parameters                                                                                                                                      ||
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context`          | `Context`: The context. <br />                                                                                                                                                                                                                                  |
| `serviceComponent` | `ComponentName`: The component name of the media browser service. <br />                                                                                                                                                                                        |
| `callback`         | `MediaBrowser.ConnectionCallback`: The connection callback. <br />                                                                                                                                                                                              |
| `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. <br /> |

**See also:**

- [MediaBrowserService.BrowserRoot.EXTRA_RECENT](https://developer.android.com/reference/android/service/media/MediaBrowserService.BrowserRoot#EXTRA_RECENT)
- [MediaBrowserService.BrowserRoot.EXTRA_OFFLINE](https://developer.android.com/reference/android/service/media/MediaBrowserService.BrowserRoot#EXTRA_OFFLINE)
- [MediaBrowserService.BrowserRoot.EXTRA_SUGGESTED](https://developer.android.com/reference/android/service/media/MediaBrowserService.BrowserRoot#EXTRA_SUGGESTED)

## Public methods

### connect

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void connect ()
```

Connects to the media browser service.

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

<br />

<br />

### disconnect

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void disconnect ()
```

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

<br />

### getExtras

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Bundle getExtras ()
```

Gets any extras for the media service.

<br />

|                                               Returns                                                ||
|---------------------------------------------------------------------|---------------------------------|
| [Bundle](https://developer.android.com/reference/android/os/Bundle) | This value may be`null`. <br /> |

|                                                       Throws                                                        ||
|--------------------------------------------------------------------------------------------------|-------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException) | if not connected. |

### getItem

Added in[API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void getItem (String mediaId, 
                MediaBrowser.ItemCallback cb)
```

Retrieves a specific[MediaItem](https://developer.android.com/reference/android/media/browse/MediaBrowser.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.

<br />

|                                                    Parameters                                                     ||
|-----------|--------------------------------------------------------------------------------------------------------|
| `mediaId` | `String`: The id of the item to retrieve. This value cannot be`null`. <br />                           |
| `cb`      | `MediaBrowser.ItemCallback`: The callback to receive the result on. This value cannot be`null`. <br /> |

### getRoot

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String getRoot ()
```

Gets the root id.

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

<br />

<br />

|                                                Returns                                                 ||
|--------------------------------------------------------------------|------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | This value cannot be`null`. <br /> |

|                                                       Throws                                                        ||
|--------------------------------------------------------------------------------------------------|-------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException) | if not connected. |

### getServiceComponent

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public ComponentName getServiceComponent ()
```

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

<br />

|                                                          Returns                                                           ||
|----------------------------------------------------------------------------------------|------------------------------------|
| [ComponentName](https://developer.android.com/reference/android/content/ComponentName) | This value cannot be`null`. <br /> |

### getSessionToken

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MediaSession.Token getSessionToken ()
```

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.

<br />

<br />

|                                                                            Returns                                                                            ||
|--------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
| [MediaSession.Token](https://developer.android.com/reference/android/media/session/MediaSession.Token) | The session token for the browser, never null. <br /> |

|                                                       Throws                                                        ||
|--------------------------------------------------------------------------------------------------|-------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException) | if not connected. |

### isConnected

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean isConnected ()
```

Returns whether the browser is connected to the service.

<br />

|      Returns      ||
|-----------|--------|
| `boolean` | <br /> |

### subscribe

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void subscribe (String parentId, 
                Bundle options, 
                MediaBrowser.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.

<br />

<br />

|                                                                                                       Parameters                                                                                                       ||
|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `parentId` | `String`: The id of the parent media item whose list of children will be subscribed. This value cannot be`null`. <br />                                                                                    |
| `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`. <br /> |
| `callback` | `MediaBrowser.SubscriptionCallback`: The callback to receive the list of children. This value cannot be`null`. <br />                                                                                      |

### subscribe

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void subscribe (String parentId, 
                MediaBrowser.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.

<br />

<br />

|                                                             Parameters                                                              ||
|------------|-------------------------------------------------------------------------------------------------------------------------|
| `parentId` | `String`: The id of the parent media item whose list of children will be subscribed. This value cannot be`null`. <br /> |
| `callback` | `MediaBrowser.SubscriptionCallback`: The callback to receive the list of children. This value cannot be`null`. <br />   |

### unsubscribe

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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.

<br />

<br />

|                                                              Parameters                                                               ||
|------------|---------------------------------------------------------------------------------------------------------------------------|
| `parentId` | `String`: The id of the parent media item whose list of children will be unsubscribed. This value cannot be`null`. <br /> |

### unsubscribe

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void unsubscribe (String parentId, 
                MediaBrowser.SubscriptionCallback callback)
```

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.

<br />

<br />

|                                                                   Parameters                                                                   ||
|------------|------------------------------------------------------------------------------------------------------------------------------------|
| `parentId` | `String`: The id of the parent media item whose list of children will be unsubscribed. This value cannot be`null`. <br />          |
| `callback` | `MediaBrowser.SubscriptionCallback`: A callback sent to the media browser service to subscribe. This value cannot be`null`. <br /> |