The Android Developer Challenge is back! Submit your idea before December 2.

MediaController.Builder

public static final class MediaController.Builder
extends Object

java.lang.Object
   ↳ androidx.media2.session.MediaController.Builder


Builder for MediaController.

To set the token of the session for the controller to connect to, one of the setSessionToken(SessionToken) or setSessionCompatToken(MediaSessionCompat.Token) should be called. Otherwise, the build() will throw an IllegalArgumentException.

Any incoming event from the MediaSession will be handled on the callback executor.

Summary

Public constructors

MediaController.Builder(Context context)

Public methods

MediaController build()

Build MediaController.

U extends BuilderBase<T extends MediaController, U, C extends MediaController.ControllerCallback> setConnectionHints(Bundle connectionHints)

Set the connection hints for the controller.

U extends BuilderBase<T extends MediaController, U, C extends MediaController.ControllerCallback> setControllerCallback(Executor executor, C callback)

Set callback for the controller and its executor.

MediaController.Builder setControllerCallback(Executor executor, MediaController.ControllerCallback callback)

Set callback for the controller and its executor.

MediaController.Builder setSessionCompatToken(MediaSessionCompat.Token compatToken)

Set the MediaSessionCompat.Token for the controller to connect to.

MediaController.Builder setSessionToken(SessionToken token)

Set the SessionToken for the controller to connect to.

Inherited methods

Public constructors

MediaController.Builder

public MediaController.Builder (Context context)

Parameters
context Context

Public methods

build

public MediaController build ()

Build MediaController.

It will throw an IllegalArgumentException if both SessionToken and MediaSessionCompat.Token are not set.

Returns
MediaController a new controller

setConnectionHints

public U extends BuilderBase<T extends MediaController, U, C extends MediaController.ControllerCallback> setConnectionHints (Bundle connectionHints)

Set the connection hints for the controller.

connectionHints is a session-specific argument to send to the session when connecting. The contents of this bundle may affect the connection result.

The hints specified here are only used when when connecting to the MediaSession. They will be ignored when connecting to MediaSessionCompat.

Parameters
connectionHints Bundle: a bundle which contains the connection hints

Returns
U extends BuilderBase<T extends MediaController, U, C extends MediaController.ControllerCallback> The Builder to allow chaining

setControllerCallback

public U extends BuilderBase<T extends MediaController, U, C extends MediaController.ControllerCallback> setControllerCallback (Executor executor, 
                C callback)

Set callback for the controller and its executor.

Parameters
executor Executor: callback executor

callback C: controller callback.

Returns
U extends BuilderBase<T extends MediaController, U, C extends MediaController.ControllerCallback> The Builder to allow chaining

setControllerCallback

public MediaController.Builder setControllerCallback (Executor executor, 
                MediaController.ControllerCallback callback)

Set callback for the controller and its executor.

Parameters
executor Executor: callback executor

callback MediaController.ControllerCallback: controller callback.

Returns
MediaController.Builder The Builder to allow chaining

setSessionCompatToken

public MediaController.Builder setSessionCompatToken (MediaSessionCompat.Token compatToken)

Set the MediaSessionCompat.Token for the controller to connect to.

When this method is called, the SessionToken which was set by calling setSessionToken(SessionToken) is removed.

Parameters
compatToken MediaSessionCompat.Token: token to connect to

Returns
MediaController.Builder The Builder to allow chaining

setSessionToken

public MediaController.Builder setSessionToken (SessionToken token)

Set the SessionToken for the controller to connect to.

When this method is called, the MediaSessionCompat.Token which was set by calling setSessionCompatToken(MediaSessionCompat.Token) is removed.

Detailed behavior of the MediaController differs according to the type of the token as follows.

  1. Connected to a SessionToken.TYPE_SESSION token

    The controller connects to the specified session directly. It's recommended when you're sure which session to control, or a you've got 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. Connected to a SessionToken.TYPE_SESSION_SERVICE or SessionToken.TYPE_LIBRARY_SERVICE

    The controller connects to the session provided by the MediaSessionService.onGetSession(ControllerInfo). It's up to the service's decision which session would be returned for the connection. Use the ERROR(/#getConnectedSessionToken()) to know the connected session.

    This can be used regardless of the session app is running or not. The controller would bind to the service while connected to wake up and keep the service process running.

Parameters
token SessionToken: token to connect to

Returns
MediaController.Builder The Builder to allow chaining