MediaSession2.Builder


public static final class MediaSession2.Builder
extends Object

java.lang.Object
   ↳ android.media.MediaSession2.Builder


This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Builder for MediaSession2.

Any incoming event from the MediaController2 will be handled on the callback executor. If it's not set, Context#getMainExecutor() will be used by default.

Summary

Public constructors

Builder(Context context)

Creates a builder for MediaSession2.

Public methods

MediaSession2 build()

Build MediaSession2.

MediaSession2.Builder setExtras(Bundle extras)

Set extras for the session token.

MediaSession2.Builder setId(String id)

Set ID of the session.

MediaSession2.Builder setSessionActivity(PendingIntent pi)

Set an intent for launching UI for this Session.

MediaSession2.Builder setSessionCallback(Executor executor, MediaSession2.SessionCallback callback)

Set callback for the session and its executor.

Inherited methods

Public constructors

Builder

Added in API level 29
public Builder (Context context)

Creates a builder for MediaSession2.

Parameters
context Context: Context This value cannot be null.

Throws
IllegalArgumentException if context is null.

Public methods

build

Added in API level 29
public MediaSession2 build ()

Build MediaSession2.

Returns
MediaSession2 a new session This value cannot be null.

Throws
IllegalStateException if the session with the same id is already exists for the package.

setExtras

Added in API level 29
public MediaSession2.Builder setExtras (Bundle extras)

Set extras for the session token. If null or not set, Session2Token#getExtras() will return an empty Bundle. An IllegalArgumentException will be thrown if the bundle contains any non-framework Parcelable objects.

Parameters
extras Bundle: This value cannot be null.

Returns
MediaSession2.Builder The Builder to allow chaining

setId

Added in API level 29
public MediaSession2.Builder setId (String id)

Set ID of the session. If it's not set, an empty string will be used to create a session.

Use this if and only if your app supports multiple playback at the same time and also wants to provide external apps to have finer controls of them.

Parameters
id String: id of the session. Must be unique per package. This value cannot be null.

Returns
MediaSession2.Builder The Builder to allow chaining This value cannot be null.

Throws
IllegalArgumentException if id is null.

setSessionActivity

Added in API level 29
public MediaSession2.Builder setSessionActivity (PendingIntent pi)

Set an intent for launching UI for this Session. This can be used as a quick link to an ongoing media screen. The intent should be for an activity that may be started using Context#startActivity(Intent).

Parameters
pi PendingIntent: The intent to launch to show UI for this session. This value may be null.

Returns
MediaSession2.Builder The Builder to allow chaining This value cannot be null.

setSessionCallback

Added in API level 29
public MediaSession2.Builder setSessionCallback (Executor executor, 
                MediaSession2.SessionCallback callback)

Set callback for the session and its executor.

Parameters
executor Executor: callback executor This value cannot be null.

callback MediaSession2.SessionCallback: session callback. This value cannot be null.

Returns
MediaSession2.Builder The Builder to allow chaining This value cannot be null.