# MediaSession2.Builder

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

# Builder

*** ** * ** ***

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

```
class Builder
```

|---|----------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                             ||
| ↳ | [android.media.MediaSession2.Builder](https://developer.android.com/reference/kotlin/android/media/MediaSession2.Builder#) |

This API is not generally intended for third party application developers. Use the[AndroidX](https://developer.android.com/jetpack/androidx.html)[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/kotlin/android/media/MediaSession2).

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

## Summary

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

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

## Public constructors

### Builder

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

```
Builder(context: Context)
```

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

|                                                               Parameters                                                               ||
|-----------|-----------------------------------------------------------------------------------------------------------------------------|
| `context` | [Context](https://developer.android.com/reference/kotlin/android/content/Context.html#):Context This value cannot be`null`. |

|                         Exceptions                         ||
|--------------------------------------|----------------------|
| `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.html#ApiLevels)  

```
fun build(): MediaSession2
```

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

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

|                                              Exceptions                                               ||
|-----------------------------------|--------------------------------------------------------------------|
| `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.html#ApiLevels)  

```
fun setExtras(extras: Bundle): MediaSession2.Builder
```

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

|                                                       Parameters                                                       ||
|----------|--------------------------------------------------------------------------------------------------------------|
| `extras` | [Bundle](https://developer.android.com/reference/kotlin/android/os/Bundle.html#):This value cannot be`null`. |

|                                                                   Return                                                                    ||
|--------------------------------------------------------------------------------------------------------------|-------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/kotlin/android/media/MediaSession2.Builder#) | The Builder to allow chaining |

**See Also**

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

### setId

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

```
fun setId(id: String): MediaSession2.Builder
```

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](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):id of the session. Must be unique per package. This value cannot be`null`. |

|                                                                                 Return                                                                                  ||
|--------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/kotlin/android/media/MediaSession2.Builder#) | The Builder to allow chaining This value cannot be`null`. |

|                      Exceptions                       ||
|--------------------------------------|-----------------|
| `java.lang.IllegalArgumentException` | if id is`null`. |

### setSessionActivity

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

```
fun setSessionActivity(pi: PendingIntent?): MediaSession2.Builder
```

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/kotlin/android/content/Context.html#startActivity(android.content.Intent)).

|                                                                                    Parameters                                                                                     ||
|------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `pi` | [PendingIntent](https://developer.android.com/reference/kotlin/android/app/PendingIntent.html#)?:The intent to launch to show UI for this session. This value may be`null`. |

|                                                                                 Return                                                                                  ||
|--------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/kotlin/android/media/MediaSession2.Builder#) | The Builder to allow chaining This value cannot be`null`. |

### setSessionCallback

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

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

Set callback for the session and its executor.

|                                                                                      Parameters                                                                                       ||
|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `executor` | [Executor](https://developer.android.com/reference/kotlin/java/util/concurrent/Executor.html#):callback executor This value cannot be`null`.                              |
| `callback` | [MediaSession2.SessionCallback](https://developer.android.com/reference/kotlin/android/media/MediaSession2.SessionCallback):session callback. This value cannot be`null`. |

|                                                                                 Return                                                                                  ||
|--------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| [MediaSession2.Builder](https://developer.android.com/reference/kotlin/android/media/MediaSession2.Builder#) | The Builder to allow chaining This value cannot be`null`. |