PlayerFence
@UnstableApi
public final class PlayerFence
A Java-compatible fluent API for waiting for certain Player conditions.
Kotlin users should strongly prefer directly calling the suspending Player extension functions like awaitPlaybackState instead.
Instances are created via futureWhen.
Summary
Public methods |
|
|---|---|
final @NonNull ListenableFuture<Void> |
entersPlaybackState(@Player.State int targetState)Returns a future that completes when the player enters the provided |
static final @NonNull PlayerFence |
futureWhen(@NonNull Player player)Entry point for Java callers to get a |
final @NonNull PlayerFence |
Returns a new instance that will not fail the future on non-fatal errors. |
final @NonNull ListenableFuture<Void> |
isPlaying(boolean targetIsPlaying)Returns a future that completes when the player's |
final @NonNull ListenableFuture<Void> |
passesContentPosition(long targetPositionMs)Returns a future that completes when the content position reaches (or passes) |
final @NonNull ListenableFuture<Void> |
Returns a future that completes when |
final @NonNull PlayerFence |
withTimeoutMs(long timeoutMs)Returns a new instance that will fail the future if the condition is not met within the specified timeout. |
Public methods
entersPlaybackState
public final @NonNull ListenableFuture<Void> entersPlaybackState(@Player.State int targetState)
Returns a future that completes when the player enters the provided targetState.
If the player is already in the provided targetState, the future completes immediately.
Must be called on the player's application looper thread.
| Throws | |
|---|---|
IllegalStateException |
if not called on the player's application looper thread. |
futureWhen
public static final @NonNull PlayerFence futureWhen(@NonNull Player player)
Entry point for Java callers to get a ListenableFuture for a Player condition.
-
Futures returned from the returned
PlayerFencewill fail after a default timeout (10 seconds, or 30 seconds if running on an emulator). This can be customized usingPlayerFence.withTimeoutMs. -
Futures returned from the returned
PlayerFencewill fail immediately when thePlayerencounters a non-fatal error (such asAnalyticsListener.onAudioCodecError). This can be customized usingPlayerFence.ignoringNonFatalErrors.
ignoringNonFatalErrors
public final @NonNull PlayerFence ignoringNonFatalErrors()
Returns a new instance that will not fail the future on non-fatal errors.
By default, futures will fail if a non-fatal error (e.g. loading error) occurs while waiting.
isPlaying
public final @NonNull ListenableFuture<Void> isPlaying(boolean targetIsPlaying)
Returns a future that completes when the player's Player.isPlaying value matches targetIsPlaying.
Must be called on the player's application looper thread.
passesContentPosition
public final @NonNull ListenableFuture<Void> passesContentPosition(long targetPositionMs)
Returns a future that completes when the content position reaches (or passes) targetPositionMs in the current media item.
See Player.awaitContentPositionAtLeast for assumptions made by this condition.
Must be called on the player's application looper thread.
rendersFirstFrame
public final @NonNull ListenableFuture<Void> rendersFirstFrame()
Returns a future that completes when Player.Listener.onRenderedFirstFrame is invoked.
Must be called on the player's application looper thread.
withTimeoutMs
public final @NonNull PlayerFence withTimeoutMs(long timeoutMs)
Returns a new instance that will fail the future if the condition is not met within the specified timeout.