Player: a sleep timer, and AirPlay in the app (#429)

The sleep timer pauses after 5 minutes to an hour, fading out over the
last ten seconds. In the iOS app the phone's player keeps the time:
with the screen locked the web view is frozen, and a timer in the page
would not fire until the app was opened again. A browser plays on in a
background tab, so there the page keeps it.

The app says at ready what it does itself ({t:"host", can}), so a page
deployed ahead of the app falls back to its own timer and shows no
AirPlay button an older app would ignore. This deploy order problem
came up with #425.

AirPlay opens AVRoutePickerView by pressing its button from code; there
is no API to present it. On iOS 27 the picker opens on the button's
primary action, not touch-up, so that is what is sent, with touch-up
for versions that have nothing on it. The simulator has no AirPlay
devices and never showed the picker, so this is unverified until it
runs on a phone.

On a phone the player's one line left the title two letters wide once
the two controls were added, so the title takes the top line and the
controls flank play below it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ray Slakinski
2026-11-08 16:54:08 -05:00
parent ecc248307e
commit 2e031763fa
13 changed files with 193 additions and 6 deletions

View File

@@ -23,6 +23,8 @@ final class Playback: NSObject {
var onState: ((Bool) -> Void)?
var onEnded: (() -> Void)?
var onError: ((String) -> Void)?
/// The sleep timer ran out and playback paused.
var onSlept: (() -> Void)?
private let session: URLSession = {
let cfg = URLSessionConfiguration.default
@@ -39,6 +41,9 @@ final class Playback: NSObject {
private var markedRead = false
private var rate: Float = 1
private var volume: Float = 1
private var sleepTimer: Timer?
/// When the sleep timer pauses playback, if it is set.
private(set) var sleepAt: Date?
var isPlaying: Bool { player?.timeControlStatus == .playing }
var currentTime: Double { player?.currentTime().seconds ?? 0 }
@@ -153,7 +158,36 @@ final class Playback: NSObject {
player?.volume = v
}
/// The sleep timer (#429): pauses after `secs`, the last ten fading out; 0 turns it off. Kept
/// here, not in the page: with the screen locked the web view is frozen, and a timer of its own
/// would not fire until the app was opened again. This one fires because the app is playing.
func sleep(after secs: Double) {
sleepTimer?.invalidate()
sleepTimer = nil
sleepAt = nil
player?.volume = volume
guard secs > 0 else { return }
sleepAt = Date(timeIntervalSinceNow: secs)
sleepTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] _ in
MainActor.assumeIsolated { self?.sleepTick() }
}
}
private func sleepTick() {
guard let sleepAt else { return }
let left = sleepAt.timeIntervalSinceNow
// A fade, not a cut: a sudden stop wakes the person it was set for.
if left > 0 {
if left < 10 { player?.volume = volume * Float(left / 10) }
return
}
sleep(after: 0)
pause()
onSlept?()
}
func stop(notify: Bool = true) {
sleep(after: 0)
savePosition()
if let timeObserver { player?.removeTimeObserver(timeObserver) }
if let endObserver { NotificationCenter.default.removeObserver(endObserver) }