public final class

MediaController.Builder

extends java.lang.Object

 java.lang.Object

↳androidx.media3.session.MediaController.Builder

Overview

A builder for MediaController.

Summary

Constructors
publicBuilder(Context context, SessionToken token)

Creates a builder for MediaController.

Methods
public <any>buildAsync()

Builds a MediaController asynchronously.

public MediaController.BuildersetApplicationLooper(Looper looper)

Sets a that must be used for all calls to the Player methods and that is used to call methods on.

public MediaController.BuildersetConnectionHints(Bundle connectionHints)

Sets connection hints for the controller.

public MediaController.BuildersetListener(MediaController.Listener listener)

Sets a listener for the controller.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Builder(Context context, SessionToken token)

Creates a builder for MediaController.

The detailed behavior of the MediaController differs depending on the type of the token as follows.

  1. 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. SessionToken.TYPE_SESSION_SERVICE or SessionToken.TYPE_LIBRARY_SERVICE: The controller connects to the session provided by the MediaSessionService.onGetSession(MediaSession.ControllerInfo) or MediaLibraryService.onGetSession(MediaSession.ControllerInfo). It's up to the service to decide which session should be returned for the connection. Use the 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: The context.
token: The token to connect to.

Methods

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

Parameters:

connectionHints: A bundle containing the connection hints.

Returns:

The builder to allow chaining.

Sets a listener for the controller.

Parameters:

listener: The listener.

Returns:

The builder to allow chaining.

public MediaController.Builder setApplicationLooper(Looper looper)

Sets a that must be used for all calls to the Player methods and that is used to call methods on. The current looper} at that time this builder is created will be used if not specified. The will be used if the current looper doesn't exist.

Parameters:

looper: The looper.

Returns:

The builder to allow chaining.

public <any> buildAsync()

Builds a MediaController asynchronously.

The controller instance can be obtained like the following example:

 MediaController.Builder builder = ...;
 ListenableFuture 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 would never be called.

Returns:

A future of the controller instance.