levelFlow

fun AudioStream.levelFlow(intervalMs: Long = 50): Flow<Float>

Emits the peak audio level at a regular polling interval.

Creates a Flow that reads the AudioStream.peakLevel property (amplitude in the range 0.0 to 1.0) at the configured intervalMs and emits each reading. The flow runs on Dispatchers.Default and completes automatically when the stream leaves the StreamState.STARTED or StreamState.STARTING state, emitting a final 0f value before completing.

Performance

The default 50 ms interval produces approximately 20 emissions per second, which is a good balance between responsiveness and CPU usage for driving UI level meters. Decreasing intervalMs increases CPU usage; values below 16 ms are not recommended.

Usage

val stream = engine.openStream(inputConfig, callback)
stream.start()

// Collect levels for a VU meter
scope.launch {
stream.levelFlow(intervalMs = 50L).collect { level ->
meterView.setLevel(level)
}
}

Cancellation

The flow is cooperative with structured concurrency. Cancelling the collecting coroutine stops polling immediately.

Return

A Flow of peak level values in the range [0.0, 1.0].

Parameters

intervalMs

Emission interval in milliseconds. Default is 50 ms (approximately 20 updates per second).

See also