SleepTimer

Schedules a fade-out and then stops an AudioStream.

Pause and resume freeze the remaining countdown; they do not pause playback. Cancel (and close) drop the schedule and leave playback running. After expiry the timer fades gain to silence using GainParams.FADE_MS, then calls AudioStream.stop.

gain must be an AudioEffectType.GAIN already in the first stream's effect chain. Extra streams passed to the constructor are stopped at the same time, after the fade. The timer does not close streams; the host still owns AudioStream.close.

Control methods are safe to call from any thread. They never run on the audio callback.

val gain = engine.createEffect(AudioEffectType.GAIN)
chain.add(gain)
stream.effectChain = chain
stream.start()

SleepTimer(stream, gain, extraLayer).use { timer ->
timer.schedule(durationMs = 30 * 60_000L, fadeMs = 2_000f)
timer.pause() // countdown only
timer.pause(pauseStreams = true) // also AudioStream.pause()
timer.resume()
timer.cancel()
}

See also

Constructors

Link copied to clipboard
constructor(stream: AudioStream, gain: AudioEffect, vararg extraStreams: AudioStream)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Milliseconds left until fade-out starts. Zero while idle, fading, or completed. While paused this is the frozen remainder.

Link copied to clipboard

Current countdown state. Readable from any thread.

Functions

Link copied to clipboard
fun cancel()

Drop the schedule. Playback is left running, including if a fade already started. Streams paused via pause are restarted.

Link copied to clipboard
open override fun close()

Cancel the schedule. Does not stop or close streams. Restarts streams paused via pause. Idempotent.

Link copied to clipboard
fun pause(pauseStreams: Boolean = false)

Freeze the countdown. Playback continues unless pauseStreams is true.

Link copied to clipboard
fun resume()

Continue a paused countdown from the remaining time.

Link copied to clipboard
fun schedule(durationMs: Long, fadeMs: Float = DEFAULT_FADE_MS)

Start or replace a countdown.