Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

MediaSession2.Builder

public static final class MediaSession2.Builder
extends Object

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


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

MediaSession2.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

MediaSession2.Builder

public MediaSession2.Builder (Context context)

Creates a builder for MediaSession2.

Parameters
context Context: Context This value must never be null.

Throws
IllegalArgumentException if context is null.

Public methods

build

public MediaSession2 build ()

Build MediaSession2.

Returns
MediaSession2 a new session This value will never be null.

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

setExtras

public MediaSession2.Builder setExtras (Bundle extras)

Set extras for the session token.

Parameters
extras Bundle: This value may be null.

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

setId

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 must never be null.

Returns
MediaSession2.Builder The Builder to allow chaining

Throws
IllegalArgumentException if id is null.

setSessionActivity

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 will never be null.

setSessionCallback

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

Set callback for the session and its executor.

Parameters
executor Executor: callback executor This value must never be null.

callback MediaSession2.SessionCallback: session callback. This value must never be null.

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