PlayerFenceKt


public final class PlayerFenceKt


Summary

Public methods

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

Suspends until the content position reaches (or passes) targetPositionMs in the current media item.

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

Suspends until Player.Listener.onRenderedFirstFrame is invoked.

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

Suspends until the player's Player.isPlaying value matches targetIsPlaying.

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

Suspends until the player enters the provided targetState.

Public methods

awaitContentPositionAtLeast

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

Suspends until the content position reaches (or passes) targetPositionMs in the current media item.

This makes some assumptions:

  1. The condition completes immediately if Player.getContentPosition is already at least targetPositionMs.

  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 Player.getContentPosition to wait for.

boolean failOnNonFatalErrors

Whether non-fatal errors (such as those from AnalyticsListener.onAudioCodecError) cause an exception to be thrown immediately.

Duration timeout

The max time to wait for, or null to use a default timeout of 10 seconds.

Throws
IllegalStateException

if not called on the player's application looper thread.

awaitFirstFrameRendered

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

Suspends until Player.Listener.onRenderedFirstFrame is invoked.

This should be called before the callback has been invoked (e.g. before calling 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 AnalyticsListener.onAudioCodecError) cause an exception to be thrown immediately.

Duration timeout

The max time to wait for, or null to use a default timeout of 10 seconds.

Throws
IllegalStateException

if not called on the player's application looper thread.

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

Suspends until the player's Player.isPlaying value matches targetIsPlaying.

If the player's Player.isPlaying value is already targetIsPlaying this returns immediately.

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

Parameters
boolean targetIsPlaying

The value of Player.isPlaying to wait for.

boolean failOnNonFatalErrors

Whether non-fatal errors (such as those from AnalyticsListener.onAudioCodecError) cause an exception to be thrown immediately.

Duration timeout

The max time to wait for, or null to use a default timeout of 10 seconds.

Throws
IllegalStateException

if not called on the player's application looper thread.

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 targetState.

If the player is already in the provided targetState this returns immediately.

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

Parameters
@Player.State int targetState

The playback Player.State to await.

boolean failOnNonFatalErrors

Whether non-fatal errors (such as those from AnalyticsListener.onAudioCodecError) cause an exception to be thrown immediately.

Duration timeout

The max time to wait for, or null to use a default timeout of 10 seconds.

Throws
IllegalStateException

if not called on the player's application looper thread.