MediaController.Builder

Added in 1.0.0
Deprecated in 1.3.0

public final class MediaController.Builder


Builder for MediaController.

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

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

Summary

Public constructors

Public methods

@NonNull MediaController

Builds a MediaController.

@NonNull MediaController.Builder
setConnectionHints(@NonNull Bundle connectionHints)

Sets the connection hints for the controller.

@NonNull MediaController.Builder

Sets the callback for the controller and its executor.

@NonNull MediaController.Builder

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

@NonNull MediaController.Builder

Sets the SessionToken for the controller to connect to.

Public constructors

Builder

Added in 1.0.0
Deprecated in 1.3.0
public Builder(@NonNull Context context)

Public methods

build

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull MediaController build()

Builds a MediaController.

Returns
@NonNull MediaController

a new controller

setConnectionHints

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull MediaController.Builder setConnectionHints(@NonNull Bundle connectionHints)

Sets 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
@NonNull Bundle connectionHints

a bundle which contains the connection hints

Returns
@NonNull MediaController.Builder

the Builder to allow chaining

Throws
java.lang.IllegalArgumentException

if the bundle contains any non-framework Parcelable objects.

setControllerCallback

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull MediaController.Builder setControllerCallback(
    @NonNull Executor executor,
    @NonNull MediaController.ControllerCallback callback
)

Sets the callback for the controller and its executor.

Parameters
@NonNull Executor executor

callback executor

@NonNull MediaController.ControllerCallback callback

controller callback.

Returns
@NonNull MediaController.Builder

the Builder to allow chaining

setSessionCompatToken

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull MediaController.Builder setSessionCompatToken(@NonNull MediaSessionCompat.Token compatToken)

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

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

Parameters
@NonNull MediaSessionCompat.Token compatToken

token to connect to

Returns
@NonNull MediaController.Builder

the Builder to allow chaining

setSessionToken

Added in 1.0.0
Deprecated in 1.3.0
public @NonNull MediaController.Builder setSessionToken(@NonNull SessionToken token)

Sets the SessionToken for the controller to connect to.

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

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

  1. Connected to a 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 TYPE_SESSION_SERVICE or TYPE_LIBRARY_SERVICE

    The controller connects to the session provided by the onGetSession. It's up to the service's decision which session would be returned for the connection. Use the 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
@NonNull SessionToken token

token to connect to

Returns
@NonNull MediaController.Builder

the Builder to allow chaining