setSleepTimer

fun setSleepTimer(timer: SleepTimer?, fade: Duration = DEFAULT_SLEEP_FADE)

Stops playback later, fading the sound down first. Null cancels an armed timer.

The fade is the engine's own and rides the ring's gain, so it never touches the volume the user set: the published value is unchanged throughout, and the level is restored the moment the player pauses. That last part is what every hand-written sleep timer gets wrong, and it is why the next play is not silent.

SleepTimer.After counts wall time while playback is advancing, so a player paused for an hour does not sleep through its own timer. SleepTimer.At fires at a media position. SleepTimer.EndOfItem fires when the current item ends and does not advance a queue past it.

Throws

when fade is negative, or an SleepTimer.After is not in the future.