# PlayerFenceKt

*** ** * ** ***


```
public final class PlayerFenceKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final void` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/test/utils/PlayerFenceKt#(androidx.media3.common.Player).awaitContentPositionAtLeast(kotlin.Long,kotlin.Boolean,kotlin.time.Duration)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player receiver, long targetPositionMs, boolean failOnNonFatalErrors, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout )` Suspends until the content position reaches (or passes) `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitContentPositionAtLeast(kotlin.Long,kotlin.Boolean,kotlin.time.Duration)` in the current media item. |
| `static final void` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/test/utils/PlayerFenceKt#(androidx.media3.common.Player).awaitFirstFrameRendered(kotlin.Boolean,kotlin.time.Duration)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player receiver, boolean failOnNonFatalErrors, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout )` Suspends until `https://developer.android.com/reference/androidx/media3/common/Player.Listener#onRenderedFirstFrame()` is invoked. |
| `static final void` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/test/utils/PlayerFenceKt#(androidx.media3.common.Player).awaitIsPlaying(kotlin.Boolean,kotlin.Boolean,kotlin.time.Duration)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player receiver, boolean targetIsPlaying, boolean failOnNonFatalErrors, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout )` Suspends until the player's `https://developer.android.com/reference/androidx/media3/common/Player#isPlaying()` value matches `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitIsPlaying(kotlin.Boolean,kotlin.Boolean,kotlin.time.Duration)`. |
| `static final void` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/test/utils/PlayerFenceKt#(androidx.media3.common.Player).awaitPlaybackState(kotlin.Int,kotlin.Boolean,kotlin.time.Duration)( @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.State int targetState, boolean failOnNonFatalErrors, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout )` Suspends until the player enters the provided `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitPlaybackState(kotlin.Int,kotlin.Boolean,kotlin.time.Duration)`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi <T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/media3/test/utils/PlayerFenceKt#(androidx.media3.common.Player).awaitSupervised(kotlin.Boolean,kotlin.time.Duration,kotlin.coroutines.SuspendFunction1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/media3/common/Player receiver, boolean failOnNonFatalErrors, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout, @https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html, @https://developer.android.com/reference/androidx/annotation/NonNull T> block )` Suspends until `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitSupervised(kotlin.Boolean,kotlin.time.Duration,kotlin.coroutines.SuspendFunction1)` completes, or a playback error occurs, or `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitSupervised(kotlin.Boolean,kotlin.time.Duration,kotlin.coroutines.SuspendFunction1)` expires. |

## Public methods

### awaitContentPositionAtLeast

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/PlayerFence.kt+function:awaitContentPositionAtLeast)

```
@UnstableApi
public static final void awaitContentPositionAtLeast(
    @NonNull Player receiver,
    long targetPositionMs,
    boolean failOnNonFatalErrors,
    Duration timeout
)
```

Suspends until the content position reaches (or passes) `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitContentPositionAtLeast(kotlin.Long,kotlin.Boolean,kotlin.time.Duration)` in the current media item.

This makes some assumptions:

1. The condition completes immediately if `https://developer.android.com/reference/androidx/media3/common/Player#getContentPosition()` is already at least `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitContentPositionAtLeast(kotlin.Long,kotlin.Boolean,kotlin.time.Duration)`.

2. Seeking past the target position satisfies the condition.

3. If playback ends, or the media item changes, before the target position is reached, an exception is thrown.

Must be called on the player's application looper thread.

| Parameters |
|---|---|
| `long targetPositionMs` | The `https://developer.android.com/reference/androidx/media3/common/Player#getContentPosition()` to wait for. |
| `boolean failOnNonFatalErrors` | Whether non-fatal errors (such as those from `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener#onAudioCodecError(androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime,java.lang.Exception)`) cause an exception to be thrown immediately. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout` | The max time to wait for, or `null` to use a default timeout of 10 seconds. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if not called on the player's application looper thread. |

### awaitFirstFrameRendered

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/PlayerFence.kt+function:awaitFirstFrameRendered)

```
@UnstableApi
public static final void awaitFirstFrameRendered(
    @NonNull Player receiver,
    boolean failOnNonFatalErrors,
    Duration timeout
)
```

Suspends until `https://developer.android.com/reference/androidx/media3/common/Player.Listener#onRenderedFirstFrame()` is invoked.

This should be called before the callback has been invoked (e.g. before calling `https://developer.android.com/reference/androidx/media3/common/Player#prepare()`), otherwise it risks awaiting forever.

Must be called on the player's application looper thread.

| Parameters |
|---|---|
| `boolean failOnNonFatalErrors` | Whether non-fatal errors (such as those from `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener#onAudioCodecError(androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime,java.lang.Exception)`) cause an exception to be thrown immediately. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout` | The max time to wait for, or `null` to use a default timeout of 10 seconds. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if not called on the player's application looper thread. |

### awaitIsPlaying

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/PlayerFence.kt+function:awaitIsPlaying)

```
@UnstableApi
public static final void awaitIsPlaying(
    @NonNull Player receiver,
    boolean targetIsPlaying,
    boolean failOnNonFatalErrors,
    Duration timeout
)
```

Suspends until the player's `https://developer.android.com/reference/androidx/media3/common/Player#isPlaying()` value matches `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitIsPlaying(kotlin.Boolean,kotlin.Boolean,kotlin.time.Duration)`.

If the player's `https://developer.android.com/reference/androidx/media3/common/Player#isPlaying()` value is already `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitIsPlaying(kotlin.Boolean,kotlin.Boolean,kotlin.time.Duration)` this returns immediately.

Must be called on the player's application looper thread.

| Parameters |
|---|---|
| `boolean targetIsPlaying` | The value of `https://developer.android.com/reference/androidx/media3/common/Player#isPlaying()` to wait for. |
| `boolean failOnNonFatalErrors` | Whether non-fatal errors (such as those from `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener#onAudioCodecError(androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime,java.lang.Exception)`) cause an exception to be thrown immediately. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout` | The max time to wait for, or `null` to use a default timeout of 10 seconds. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if not called on the player's application looper thread. |

### awaitPlaybackState

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/PlayerFence.kt+function:awaitPlaybackState)

```
@UnstableApi
public static final void awaitPlaybackState(
    @NonNull Player receiver,
    @Player.State int targetState,
    boolean failOnNonFatalErrors,
    Duration timeout
)
```

Suspends until the player enters the provided `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitPlaybackState(kotlin.Int,kotlin.Boolean,kotlin.time.Duration)`.

If the player is already in the provided `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitPlaybackState(kotlin.Int,kotlin.Boolean,kotlin.time.Duration)` this returns immediately.

Must be called on the player's application looper thread.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/media3/common/Player.State int targetState` | The playback `https://developer.android.com/reference/androidx/media3/common/Player.State` to await. |
| `boolean failOnNonFatalErrors` | Whether non-fatal errors (such as those from `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener#onAudioCodecError(androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime,java.lang.Exception)`) cause an exception to be thrown immediately. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout` | The max time to wait for, or `null` to use a default timeout of 10 seconds. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if not called on the player's application looper thread. |

### awaitSupervised

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/PlayerFence.kt+function:awaitSupervised)

```
@UnstableApi
public static final @NonNull T <T extends Object> awaitSupervised(
    @NonNull Player receiver,
    boolean failOnNonFatalErrors,
    Duration timeout,
    @NonNull SuspendFunction1<@NonNull CoroutineScope, @NonNull T> block
)
```

Suspends until `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitSupervised(kotlin.Boolean,kotlin.time.Duration,kotlin.coroutines.SuspendFunction1)` completes, or a playback error occurs, or `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitSupervised(kotlin.Boolean,kotlin.time.Duration,kotlin.coroutines.SuspendFunction1)` expires.

If a fatal `https://developer.android.com/reference/androidx/media3/common/Player#playerError()` or non-fatal `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener` error occurs, or if `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitSupervised(kotlin.Boolean,kotlin.time.Duration,kotlin.coroutines.SuspendFunction1)` expires, the `https://developer.android.com/reference/androidx/media3/test/utils/package-summary#(androidx.media3.common.Player).awaitSupervised(kotlin.Boolean,kotlin.time.Duration,kotlin.coroutines.SuspendFunction1)` coroutine is cancelled immediately and the exception is thrown.

Must be called on the player's application looper thread.

| Parameters |
|---|---|
| `boolean failOnNonFatalErrors` | Whether non-fatal errors (such as those from `https://developer.android.com/reference/androidx/media3/exoplayer/analytics/AnalyticsListener#onAudioCodecError(androidx.media3.exoplayer.analytics.AnalyticsListener.EventTime,java.lang.Exception)`) cause an exception to be thrown immediately. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html timeout` | The max time to wait for, or `null` to use a default timeout of 10 seconds. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html, @https://developer.android.com/reference/androidx/annotation/NonNull T> block` | The suspending computation to execute within the supervision scope. |