# PlayerPool

Artifact: [androidx.media3:media3-common-ktx](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/PlayerPool.kt+class:androidx.media3.common.PlayerPool)

*** ** * ** ***


```
@MainThread
@UnstableApi
class PlayerPool<T : Player>
```

<br />

*** ** * ** ***

A pool of `https://developer.android.com/reference/kotlin/androidx/media3/common/Player` instances.

All operations on this class must be called from the main thread.

| Parameters |
|---|---|
| `<T : https://developer.android.com/reference/kotlin/androidx/media3/common/Player>` | The type of the `https://developer.android.com/reference/kotlin/androidx/media3/common/Player` instances in the pool. |

## Summary

| ### Public constructors |
|---|
| `<T : https://developer.android.com/reference/kotlin/androidx/media3/common/Player> https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#PlayerPool(kotlin.Int,kotlin.Function0)(poolCapacity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, playerFactory: () -> T)` |

| ### Public functions |
|---|---|
| `suspend T` | `https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#acquire()()` Suspends until a player is available in the pool. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#executeForAcquired(kotlin.Function1)(action: T.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Executes the given `https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#executeForAcquired(kotlin.Function1)` only on players that are currently acquired from the pool. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#executeForAll(kotlin.Function1)(action: T.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Executes the given `https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#executeForAll(kotlin.Function1)` on all players managed by the pool, including both acquired players and idle players waiting in the pool. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#release()()` Releases all players in the pool and prevents further acquisition. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#yield(androidx.media3.common.Player)(player: T)` Returns a player to the pool. |

## Public constructors

### PlayerPool

```
<T : Player> PlayerPool(poolCapacity: Int, playerFactory: () -> T)
```

| Parameters |
|---|---|
| `<T : https://developer.android.com/reference/kotlin/androidx/media3/common/Player>` | The type of the `https://developer.android.com/reference/kotlin/androidx/media3/common/Player` instances in the pool. |
| `poolCapacity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The maximum number of players to hold in the pool. Must be positive. |
| `playerFactory: () -> T` | A lambda responsible for instantiating and configuring new players. The created players must be configured to be accessed from the main thread. |

## Public functions

### acquire

```
suspend fun acquire(): T
```

Suspends until a player is available in the pool.

### executeForAcquired

```
@MainThread
fun executeForAcquired(action: T.() -> Unit): Unit
```

Executes the given `https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#executeForAcquired(kotlin.Function1)` only on players that are currently acquired from the pool.

This is useful for broadcasting state changes (like muting or updating playback speed) to players that are currently active, without affecting idle players in the pool.

| Parameters |
|---|---|
| `action: T.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | The action to perform on each acquired player. |

### executeForAll

```
@MainThread
fun executeForAll(action: T.() -> Unit): Unit
```

Executes the given `https://developer.android.com/reference/kotlin/androidx/media3/common/PlayerPool#executeForAll(kotlin.Function1)` on all players managed by the pool, including both acquired players and idle players waiting in the pool.

| Parameters |
|---|---|
| `action: T.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | The action to perform on each player. |

### release

```
fun release(): Unit
```

Releases all players in the pool and prevents further acquisition.

### yield

```
fun yield(player: T): Unit
```

Returns a player to the pool.

| Parameters |
|---|---|
| `player: T` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/Player` instance to return to the pool. |