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()
}Content copied to clipboard