PlayerFence
@UnstableApi
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 companion functions |
|
|---|---|
PlayerFence |
futureWhen(player: Player)Entry point for Java callers to get a |
Public functions |
|
|---|---|
ListenableFuture<Void?> |
entersPlaybackState(targetState: @Player.State Int)Returns a future that completes when the player enters the provided |
PlayerFence |
Returns a new instance that will not fail the future on non-fatal errors. |
ListenableFuture<Void?> |
Returns a future that completes when the player's |
ListenableFuture<Void?> |
passesContentPosition(targetPositionMs: Long)Returns a future that completes when the content position reaches (or passes) |
ListenableFuture<Void?> |
Returns a future that completes when |
PlayerFence |
withTimeoutMs(timeoutMs: Long)Returns a new instance that will fail the future if the condition is not met within the specified timeout. |
Public companion functions
futureWhen
fun futureWhen(player: Player): PlayerFence
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.
Public functions
entersPlaybackState
fun entersPlaybackState(targetState: @Player.State Int): ListenableFuture<Void?>
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. |
ignoringNonFatalErrors
fun ignoringNonFatalErrors(): PlayerFence
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
fun isPlaying(targetIsPlaying: Boolean): ListenableFuture<Void?>
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
fun passesContentPosition(targetPositionMs: Long): ListenableFuture<Void?>
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
fun rendersFirstFrame(): ListenableFuture<Void?>
Returns a future that completes when Player.Listener.onRenderedFirstFrame is invoked.
Must be called on the player's application looper thread.
withTimeoutMs
fun withTimeoutMs(timeoutMs: Long): PlayerFence
Returns a new instance that will fail the future if the condition is not met within the specified timeout.