# MediaSession2.Builder

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/android/media/MediaSession2.Builder#pubctors)\|[Methods](https://developer.android.com/reference/android/media/MediaSession2.Builder#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/media/MediaSession2.Builder#inhmethods)  

# MediaSession2.Builder

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/media/MediaSession2.Builder "View this page in Kotlin")\|Java

`
public
static
final

class
MediaSession2.Builder
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|-------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.MediaSession2.Builder |

<br />

*** ** * ** ***

This API is not generally intended for third party application developers. Use the[AndroidX](https://developer.android.com/jetpack/androidx)[Media3 session Library](https://developer.android.com/media/media3/session/control-playback)for consistent behavior across all devices.

Builder for[MediaSession2](https://developer.android.com/reference/android/media/MediaSession2).

Any incoming event from the[MediaController2](https://developer.android.com/reference/android/media/MediaController2)will be handled on the callback executor. If it's not set,[Context.getMainExecutor()](https://developer.android.com/reference/android/content/Context#getMainExecutor())will be used by default.

## Summary

|                                                                                                                                                      ### Public constructors                                                                                                                                                      ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder#Builder(android.content.Context))`(`[Context](https://developer.android.com/reference/android/content/Context)` context) ` Creates a builder for[MediaSession2](https://developer.android.com/reference/android/media/MediaSession2). |

|                                                                                                                                                                                                                                                                                       ### Public methods                                                                                                                                                                                                                                                                                        ||
|---------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[MediaSession2](https://developer.android.com/reference/android/media/MediaSession2)                 | ` `[build](https://developer.android.com/reference/android/media/MediaSession2.Builder#build())`() ` Build[MediaSession2](https://developer.android.com/reference/android/media/MediaSession2).                                                                                                                                                                                                                                                                                        |
| ` `[MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | ` `[setExtras](https://developer.android.com/reference/android/media/MediaSession2.Builder#setExtras(android.os.Bundle))`(`[Bundle](https://developer.android.com/reference/android/os/Bundle)` extras) ` Set extras for the session token.                                                                                                                                                                                                                                            |
| ` `[MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | ` `[setId](https://developer.android.com/reference/android/media/MediaSession2.Builder#setId(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String)` id) ` Set ID of the session.                                                                                                                                                                                                                                                                     |
| ` `[MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | ` `[setSessionActivity](https://developer.android.com/reference/android/media/MediaSession2.Builder#setSessionActivity(android.app.PendingIntent))`(`[PendingIntent](https://developer.android.com/reference/android/app/PendingIntent)` pi) ` Set an intent for launching UI for this Session.                                                                                                                                                                                        |
| ` `[MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | ` `[setSessionCallback](https://developer.android.com/reference/android/media/MediaSession2.Builder#setSessionCallback(java.util.concurrent.Executor,%20android.media.MediaSession2.SessionCallback))`(`[Executor](https://developer.android.com/reference/java/util/concurrent/Executor)` executor, `[MediaSession2.SessionCallback](https://developer.android.com/reference/android/media/MediaSession2.SessionCallback)` callback) ` Set callback for the session and its executor. |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### Builder

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Builder (Context context)
```

Creates a builder for[MediaSession2](https://developer.android.com/reference/android/media/MediaSession2).

<br />

|                            Parameters                            ||
|-----------|-------------------------------------------------------|
| `context` | `Context`: Context This value cannot be`null`. <br /> |

|                                                            Throws                                                            ||
|--------------------------------------------------------------------------------------------------------|----------------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if context is`null`. |

## Public methods

### build

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MediaSession2 build ()
```

Build[MediaSession2](https://developer.android.com/reference/android/media/MediaSession2).

<br />

|                                                                Returns                                                                 ||
|--------------------------------------------------------------------------------------|--------------------------------------------------|
| [MediaSession2](https://developer.android.com/reference/android/media/MediaSession2) | a new session This value cannot be`null`. <br /> |

|                                                                                Throws                                                                                ||
|--------------------------------------------------------------------------------------------------|--------------------------------------------------------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException) | if the session with the same id is already exists for the package. |

### setExtras

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public MediaSession2.Builder setExtras (Bundle extras)
```

Set extras for the session token. If null or not set,[Session2Token.getExtras()](https://developer.android.com/reference/android/media/Session2Token#getExtras())will return an empty[Bundle](https://developer.android.com/reference/android/os/Bundle). An[IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException)will be thrown if the bundle contains any non-framework Parcelable objects.

<br />

|                       Parameters                       ||
|----------|----------------------------------------------|
| `extras` | `Bundle`: This value cannot be`null`. <br /> |

|                                                                  Returns                                                                   ||
|------------------------------------------------------------------------------------------------------|--------------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | The Builder to allow chaining <br /> |

**See also:**

- [Session2Token.getExtras()](https://developer.android.com/reference/android/media/Session2Token#getExtras())  

### setId

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

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

<br />

|                                            Parameters                                             ||
|------|---------------------------------------------------------------------------------------------|
| `id` | `String`: id of the session. Must be unique per package. This value cannot be`null`. <br /> |

|                                                                                Returns                                                                                 ||
|------------------------------------------------------------------------------------------------------|------------------------------------------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | The Builder to allow chaining This value cannot be`null`. <br /> |

|                                                         Throws                                                          ||
|--------------------------------------------------------------------------------------------------------|-----------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if id is`null`. |

### setSessionActivity

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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)](https://developer.android.com/reference/android/content/Context#startActivity(android.content.Intent)).

<br />

|                                                Parameters                                                ||
|------|----------------------------------------------------------------------------------------------------|
| `pi` | `PendingIntent`: The intent to launch to show UI for this session. This value may be`null`. <br /> |

|                                                                                Returns                                                                                 ||
|------------------------------------------------------------------------------------------------------|------------------------------------------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | The Builder to allow chaining This value cannot be`null`. <br /> |

### setSessionCallback

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

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

Set callback for the session and its executor.

<br />

|                                            Parameters                                             ||
|------------|---------------------------------------------------------------------------------------|
| `executor` | `Executor`: callback executor This value cannot be`null`. <br />                      |
| `callback` | `MediaSession2.SessionCallback`: session callback. This value cannot be`null`. <br /> |

|                                                                                Returns                                                                                 ||
|------------------------------------------------------------------------------------------------------|------------------------------------------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/android/media/MediaSession2.Builder) | The Builder to allow chaining This value cannot be`null`. <br /> |