# PlayerExtensionsKt

*** ** * ** ***


```
public final class PlayerExtensionsKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Void.html` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/common/PlayerExtensionsKt#(androidx.media3.common.Player).listen(kotlin.Function2)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player receiver, @https://developer.android.com/reference/androidx/annotation/NonNull Function2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player.Events, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onEvents )` Continuously listens to the `https://developer.android.com/reference/androidx/media3/common/Player.Listener#onEvents(androidx.media3.common.Player,androidx.media3.common.Player.Events)` callback, passing the received `https://developer.android.com/reference/androidx/media3/common/Player.Events` to the provided `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listen(kotlin.Function2)` function. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Void.html` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/common/PlayerExtensionsKt#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player receiver, @https://developer.android.com/reference/androidx/media3/common/Player.Event int firstEvent, int... otherEvents, @https://developer.android.com/reference/androidx/annotation/NonNull Function2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player.Events, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onEvents )` Continuously listens to the `https://developer.android.com/reference/androidx/media3/common/Player.Listener#onEvents(androidx.media3.common.Player,androidx.media3.common.Player.Events)` callback, passing the received `https://developer.android.com/reference/androidx/media3/common/Player.Events` to the provided `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)` function. |

## Public methods

### listen

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/PlayerExtensions.kt+function:listen)

```
@UnstableApi
public static final @NonNull Void listen(
    @NonNull Player receiver,
    @NonNull Function2<@NonNull Player, @NonNull Player.Events, Unit> onEvents
)
```

Continuously listens to the `https://developer.android.com/reference/androidx/media3/common/Player.Listener#onEvents(androidx.media3.common.Player,androidx.media3.common.Player.Events)` callback, passing the received `https://developer.android.com/reference/androidx/media3/common/Player.Events` to the provided `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listen(kotlin.Function2)` function.

This function can be called from any thread. The `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listen(kotlin.Function2)` function will be invoked on the thread associated with `https://developer.android.com/reference/androidx/media3/common/Player#getApplicationLooper()`.

If, during the execution of `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listen(kotlin.Function2)`, an exception is thrown, the coroutine corresponding to listening to the Player will be terminated. Any used resources will be cleaned up (e.g. removing of the listeners) and exception will be re-thrown right after the last suspension point.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player.Events, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onEvents` | The function to handle player events. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Void.html` | Nothing This function never returns normally. It will either continue indefinitely or terminate due to an exception or cancellation. |

### listenTo

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/PlayerExtensions.kt+function:listenTo)

```
@UnstableApi
public static final @NonNull Void listenTo(
    @NonNull Player receiver,
    @Player.Event int firstEvent,
    int... otherEvents,
    @NonNull Function2<@NonNull Player, @NonNull Player.Events, Unit> onEvents
)
```

Continuously listens to the `https://developer.android.com/reference/androidx/media3/common/Player.Listener#onEvents(androidx.media3.common.Player,androidx.media3.common.Player.Events)` callback, passing the received `https://developer.android.com/reference/androidx/media3/common/Player.Events` to the provided `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)` function. A non-zero number of events has to be specified (hence `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)` and `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)`). The order is not important.

This function can be called from any thread. The `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)` function will be invoked on the thread associated with `https://developer.android.com/reference/androidx/media3/common/Player#getApplicationLooper()`.

If, during the execution of `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)`, an exception is thrown, the coroutine corresponding to listening to the Player will be terminated. Any used resources will be cleaned up (e.g. removing of the listeners) and exception will be re-thrown right after the last suspension point.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/media3/common/Player.Event int firstEvent` | One of the `https://developer.android.com/reference/androidx/media3/common/Player.Event` to listen to. Does not have to actually be the first one, since the order is not taken into account. This parameter is separated from `https://developer.android.com/reference/androidx/media3/common/package-summary#(androidx.media3.common.Player).listenTo(kotlin.Int,kotlin.IntArray,kotlin.Function2)` to avoid creating an array when only one event is needed or avoid accidentally not passing any events. |
| `int... otherEvents` | The set of other `https://developer.android.com/reference/androidx/media3/common/Player.Event` to listen for. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player.Events, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> onEvents` | The function to handle player events. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Void.html` | Nothing This function never returns normally. It will either continue indefinitely or terminate due to an exception or cancellation. |