PlayerState

State holder for audio playback in Compose.

Provides a simplified, reactive API for playing audio that integrates with Compose's state management.

Example Usage

@Composable
fun AudioPlayer(recording: AudioRecording) {
val playerState = rememberPlayerState(recording)

Column {
// Show playback status
Text(
when {
playerState.isLoading -> "Loading..."
playerState.isPlaying -> "Playing"
playerState.isPaused -> "Paused"
playerState.isFinished -> "Finished"
else -> "Ready"
}
)

// Play/Pause button
Button(
onClick = { playerState.toggle() },
enabled = playerState.isReady && !playerState.isLoading
) {
Text(if (playerState.isPlaying) "Pause" else "Play")
}

// Stop button
Button(onClick = { playerState.stop() }) {
Text("Stop")
}
}
}

Properties

Link copied to clipboard

Duration of the loaded recording from the PCM frame clock, or Duration.ZERO if none / unknown. Uses frames (not container metadata) so progress, position, and seekTo stay aligned.

Link copied to clipboard

The most recent error, if any.

Link copied to clipboard

Whether there is an error.

Link copied to clipboard

Whether a recording is loaded.

Link copied to clipboard

Whether playback has finished.

Link copied to clipboard

Whether the player is loading audio.

Link copied to clipboard

Whether the player is paused.

Link copied to clipboard

Whether the player is currently playing, or seeking with intent to resume.

Link copied to clipboard

Whether the player is ready (has audio loaded and can play).

Link copied to clipboard

Estimated playback position derived from progress and duration.

Link copied to clipboard

Normalized playback position in 0f..1f across the full recording.

Link copied to clipboard

The currently loaded recording (full source, not a seek window).

Functions

Link copied to clipboard

Clears the current error.

Link copied to clipboard
fun load(recording: AudioRecording)

Loads an AudioRecording for playback.

Link copied to clipboard
suspend fun loadAsync(recording: AudioRecording)

Loads an AudioRecording for playback (suspend version).

Link copied to clipboard
fun pause()

Pauses playback.

Link copied to clipboard
fun play()

Starts or resumes playback.

Link copied to clipboard
suspend fun playAsync()

Starts or resumes playback (suspend version).

Link copied to clipboard
fun reset()

Resets the player state and unloads any recording.

Link copied to clipboard
suspend fun resetAsync()

Resets the player state and unloads any recording (suspend version).

Link copied to clipboard
fun seekTo(progress: Float)

Seeks within the loaded recording.

Link copied to clipboard
suspend fun seekToAsync(progress: Float)

Seeks within the loaded recording (suspend version).

Link copied to clipboard
fun stop()

Stops playback and resets to the beginning of the full recording.

Link copied to clipboard
suspend fun stopAsync()

Stops playback and resets to the beginning (suspend version).

Link copied to clipboard
fun toggle()

Toggles between playing and paused states.

Link copied to clipboard
suspend fun toggleAsync(): Boolean?

Toggles between playing and paused states (suspend version).