# MediaController.Builder

Artifact: [androidx.media3:media3-session](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/session/MediaController.java+class:androidx.media3.session.MediaController.Builder)

*** ** * ** ***


```
class MediaController.Builder
```

<br />

*** ** * ** ***

A builder for `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#Builder(android.content.Context,androidx.media3.session.SessionToken)(context: https://developer.android.com/reference/android/content/Context.html!, token: https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken!)` Creates a builder for `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController`. |

| ### Public functions |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#buildAsync()()` Builds a `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController` asynchronously. |
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | `@https://developer.android.com/reference/kotlin/androidx/media3/common/util/ExperimentalApi @https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#experimentalSetPlatformSessionCallbackAggregationTimeoutMs(long)( platformSessionCallbackAggregationTimeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html )` Sets the timeout after which updates from the platform session callbacks are applied to the browser, in milliseconds. |
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | `@https://developer.android.com/reference/kotlin/androidx/media3/common/util/UnstableApi @https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html @https://developer.android.com/reference/kotlin/androidx/media3/common/util/ExperimentalApi https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#setAllowDeviceVolumeCommandsForLocalPlayback(boolean)( allowDeviceVolumeCommandsForLocalPlayback: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` Sets whether `https://developer.android.com/reference/kotlin/androidx/media3/common/Player#COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS()` and `https://developer.android.com/reference/kotlin/androidx/media3/common/Player#COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS()` are allowed to be available for `https://developer.android.com/reference/kotlin/androidx/media3/common/DeviceInfo#PLAYBACK_TYPE_LOCAL()` playbacks (assuming the media session supports it). |
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#setApplicationLooper(android.os.Looper)(looper: https://developer.android.com/reference/android/os/Looper.html!)` Sets a `https://developer.android.com/reference/android/os/Looper.html` that must be used for all calls to the `https://developer.android.com/reference/kotlin/androidx/media3/common/Player` methods and that is used to call `https://developer.android.com/reference/kotlin/androidx/media3/common/Player.Listener` methods on. |
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | `@https://developer.android.com/reference/kotlin/androidx/media3/common/util/UnstableApi @https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#setBitmapLoader(androidx.media3.common.util.BitmapLoader)(bitmapLoader: https://developer.android.com/reference/kotlin/androidx/media3/common/util/BitmapLoader!)` Sets a `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BitmapLoader` for the `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController` to decode bitmaps from compressed binary data. |
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#setConnectionHints(android.os.Bundle)(connectionHints: https://developer.android.com/reference/android/os/Bundle.html!)` Sets connection hints for the controller. |
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#setListener(androidx.media3.session.MediaController.Listener)(listener: https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Listener!)` Sets a listener for the controller. |
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | `@https://developer.android.com/reference/kotlin/androidx/media3/common/util/UnstableApi @https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder#setMaxCommandsForMediaItems(int)(maxCommandsForMediaItems: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Sets the max number of commands the controller supports per media item. |

## Public constructors

### Builder

```
Builder(context: Context!, token: SessionToken!)
```

Creates a builder for `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController`.

The detailed behavior of the `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController` differs depending on the type of the token as follows.

1. `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken#TYPE_SESSION()`: The controller connects to the specified session directly. It's recommended when you're sure which session to control, or you've got a token directly from the session app. This can be used only when the session for the token is running. Once the session is closed, the token becomes unusable.
2. `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken#TYPE_SESSION_SERVICE()` or `https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken#TYPE_LIBRARY_SERVICE()`: The controller connects to the session provided by the `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSessionService#onGetSession(androidx.media3.session.MediaSession.ControllerInfo)` or `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaLibraryService#onGetSession(androidx.media3.session.MediaSession.ControllerInfo)`. It's up to the service to decide which session should be returned for the connection. Use the `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController#getConnectedToken()` to know the connected session. This can be used regardless of whether the session app is running or not. The controller will bind to the service as long as it's connected to wake up and keep the service process running.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | The context. |
| `token: https://developer.android.com/reference/kotlin/androidx/media3/session/SessionToken!` | The token to connect to. |

## Public functions

### buildAsync

```
fun buildAsync(): ListenableFuture<MediaController!>!
```

Builds a `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController` asynchronously.

The controller instance can be obtained like the following example:

```kotlin
MediaController.Builder builder = ...;
ListenableFuture<MediaController> future = builder.buildAsync();
future.addListener(() -> {
  try {
    MediaController controller = future.get();
    // The session accepted the connection.
  } catch (ExecutionException e) {
    if (e.getCause() instanceof SecurityException) {
      // The session rejected the connection.
    }
  }
}, ContextCompat.getMainExecutor());
```

The future must be kept by callers until the future is complete to get the controller instance. Otherwise, the future might be garbage collected and the listener added by `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html#addListener(java.lang.Runnable, java.util.concurrent.Executor)` would never be called.

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController!>!` | A future of the controller instance. |

### experimentalSetPlatformSessionCallbackAggregationTimeoutMs

```
@ExperimentalApi
@CanIgnoreReturnValue
fun experimentalSetPlatformSessionCallbackAggregationTimeoutMs(
    platformSessionCallbackAggregationTimeoutMs: Long
): MediaController.Builder!
```

Sets the timeout after which updates from the platform session callbacks are applied to the browser, in milliseconds.

The default is 100ms.

| Parameters |
|---|---|
| `platformSessionCallbackAggregationTimeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The timeout, in milliseconds. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | The builder to allow chaining. |

### setAllowDeviceVolumeCommandsForLocalPlayback

```
@UnstableApi
@CanIgnoreReturnValue
@ExperimentalApi
fun setAllowDeviceVolumeCommandsForLocalPlayback(
    allowDeviceVolumeCommandsForLocalPlayback: Boolean
): MediaController.Builder!
```

Sets whether `https://developer.android.com/reference/kotlin/androidx/media3/common/Player#COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS()` and `https://developer.android.com/reference/kotlin/androidx/media3/common/Player#COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS()` are allowed to be available for `https://developer.android.com/reference/kotlin/androidx/media3/common/DeviceInfo#PLAYBACK_TYPE_LOCAL()` playbacks (assuming the media session supports it).

This method will be removed in a future release.

The default is `false`. Local device volume changes should not generally be done by apps and can be triggered via `https://developer.android.com/reference/android/media/AudioManager.html` without using a media controller or media session.

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | The builder to allow chaining. |

### setApplicationLooper

```
@CanIgnoreReturnValue
fun setApplicationLooper(looper: Looper!): MediaController.Builder!
```

Sets a `https://developer.android.com/reference/android/os/Looper.html` that must be used for all calls to the `https://developer.android.com/reference/kotlin/androidx/media3/common/Player` methods and that is used to call `https://developer.android.com/reference/kotlin/androidx/media3/common/Player.Listener` methods on. The `https://developer.android.com/reference/android/os/Looper.html#myLooper()` current looper} at that time this builder is created will be used if not specified. The `https://developer.android.com/reference/android/os/Looper.html#getMainLooper()` will be used if the current looper doesn't exist.

| Parameters |
|---|---|
| `looper: https://developer.android.com/reference/android/os/Looper.html!` | The looper. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | The builder to allow chaining. |

### setBitmapLoader

```
@UnstableApi
@CanIgnoreReturnValue
fun setBitmapLoader(bitmapLoader: BitmapLoader!): MediaController.Builder!
```

Sets a `https://developer.android.com/reference/kotlin/androidx/media3/common/util/BitmapLoader` for the `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController` to decode bitmaps from compressed binary data. If not set, a `https://developer.android.com/reference/kotlin/androidx/media3/session/CacheBitmapLoader` that wraps a `https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSourceBitmapLoader` will be used.

| Parameters |
|---|---|
| `bitmapLoader: https://developer.android.com/reference/kotlin/androidx/media3/common/util/BitmapLoader!` | The bitmap loader. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | The builder to allow chaining. |

### setConnectionHints

```
@CanIgnoreReturnValue
fun setConnectionHints(connectionHints: Bundle!): MediaController.Builder!
```

Sets connection hints for the controller.

The hints are session-specific arguments sent to the session when connecting. The contents of this bundle may affect the connection result.

The hints are only used when connecting to the `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaSession`. They will be ignored when connecting to `android.support.v4.media.session.MediaSessionCompat`.

| Parameters |
|---|---|
| `connectionHints: https://developer.android.com/reference/android/os/Bundle.html!` | A bundle containing the connection hints. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | The builder to allow chaining. |

### setListener

```
@CanIgnoreReturnValue
fun setListener(listener: MediaController.Listener!): MediaController.Builder!
```

Sets a listener for the controller.

| Parameters |
|---|---|
| `listener: https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Listener!` | The listener. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | The builder to allow chaining. |

### setMaxCommandsForMediaItems

```
@UnstableApi
@CanIgnoreReturnValue
fun setMaxCommandsForMediaItems(maxCommandsForMediaItems: Int): MediaController.Builder!
```

Sets the max number of commands the controller supports per media item.

Must be greater or equal to 0. The default is 0.

| Parameters |
|---|---|
| `maxCommandsForMediaItems: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The max number of commands per media item. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/MediaController.Builder!` | The builder to allow chaining. |