# 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)

*** ** * ** ***


```
public final class MediaController.Builder
```

<br />

*** ** * ** ***

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

## Summary

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

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

## Public constructors

### Builder

```
public Builder(Context context, SessionToken token)
```

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

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

1. `https://developer.android.com/reference/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/androidx/media3/session/SessionToken#TYPE_SESSION_SERVICE()` or `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_LIBRARY_SERVICE()`: The controller connects to the session provided by the `https://developer.android.com/reference/androidx/media3/session/MediaSessionService#onGetSession(androidx.media3.session.MediaSession.ControllerInfo)` or `https://developer.android.com/reference/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/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 |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context. |
| `https://developer.android.com/reference/androidx/media3/session/SessionToken token` | The token to connect to. |

## Public methods

### buildAsync

```
public ListenableFuture<MediaController> buildAsync()
```

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

The controller instance can be obtained like the following example:

```
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/androidx/media3/session/MediaController>` | A future of the controller instance. |

### experimentalSetPlatformSessionCallbackAggregationTimeoutMs

```
@ExperimentalApi
@CanIgnoreReturnValue
public MediaController.Builder experimentalSetPlatformSessionCallbackAggregationTimeoutMs(
    long platformSessionCallbackAggregationTimeoutMs
)
```

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

The default is 100ms.

| Parameters |
|---|---|
| `long platformSessionCallbackAggregationTimeoutMs` | The timeout, in milliseconds. |

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

### setAllowDeviceVolumeCommandsForLocalPlayback

```
@UnstableApi
@CanIgnoreReturnValue
@ExperimentalApi
public MediaController.Builder setAllowDeviceVolumeCommandsForLocalPlayback(
    boolean allowDeviceVolumeCommandsForLocalPlayback
)
```

Sets whether `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS()` and `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS()` are allowed to be available for `https://developer.android.com/reference/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/androidx/media3/session/MediaController.Builder` | The builder to allow chaining. |

### setApplicationLooper

```
@CanIgnoreReturnValue
public MediaController.Builder setApplicationLooper(Looper looper)
```

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/androidx/media3/common/Player` methods and that is used to call `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | The looper. |

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

### setBitmapLoader

```
@UnstableApi
@CanIgnoreReturnValue
public MediaController.Builder setBitmapLoader(BitmapLoader bitmapLoader)
```

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

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

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

### setConnectionHints

```
@CanIgnoreReturnValue
public MediaController.Builder setConnectionHints(Bundle connectionHints)
```

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/androidx/media3/session/MediaSession`. They will be ignored when connecting to `android.support.v4.media.session.MediaSessionCompat`.

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

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

### setListener

```
@CanIgnoreReturnValue
public MediaController.Builder setListener(MediaController.Listener listener)
```

Sets a listener for the controller.

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

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

### setMaxCommandsForMediaItems

```
@UnstableApi
@CanIgnoreReturnValue
public MediaController.Builder setMaxCommandsForMediaItems(int maxCommandsForMediaItems)
```

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

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

| Parameters |
|---|---|
| `int maxCommandsForMediaItems` | The max number of commands per media item. |

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