# 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
public final class PlayerPool<T extends Player>
```

<br />

*** ** * ** ***

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

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

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

## Summary

| ### Public constructors |
|---|
| `<T extends https://developer.android.com/reference/androidx/media3/common/Player> https://developer.android.com/reference/androidx/media3/common/PlayerPool#PlayerPool(kotlin.Int,kotlin.Function0)( int poolCapacity, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull T> playerFactory )` |

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

## Public constructors

### PlayerPool

```
public <T extends Player> PlayerPool(
    int poolCapacity,
    @NonNull Function0<@NonNull T> playerFactory
)
```

| Parameters |
|---|---|
| `<T extends https://developer.android.com/reference/androidx/media3/common/Player>` | The type of the `https://developer.android.com/reference/androidx/media3/common/Player` instances in the pool. |
| `int poolCapacity` | The maximum number of players to hold in the pool. Must be positive. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull T> playerFactory` | A lambda responsible for instantiating and configuring new players. The created players must be configured to be accessed from the main thread. |

## Public methods

### acquire

```
public final @NonNull T acquire()
```

Suspends until a player is available in the pool.

### executeForAcquired

```
@MainThread
public final void executeForAcquired(@NonNull Function1<@NonNull T, Unit> action)
```

Executes the given `https://developer.android.com/reference/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull T, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> action` | The action to perform on each acquired player. |

### executeForAll

```
@MainThread
public final void executeForAll(@NonNull Function1<@NonNull T, Unit> action)
```

Executes the given `https://developer.android.com/reference/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull T, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> action` | The action to perform on each player. |

### release

```
public final void release()
```

Releases all players in the pool and prevents further acquisition.

### yield

```
public final void yield(@NonNull T player)
```

Returns a player to the pool.

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