fix: prevent idle sleep during playback

This commit is contained in:
2026-10-17 22:05:27 +02:00
parent f49eb6d084
commit 2b96a165d2
7 changed files with 82 additions and 7 deletions
+22
View File
@@ -23,6 +23,8 @@ final class SystemPlaybackControls {
private let infoCenter = MPNowPlayingInfoCenter.default()
private var targets: [(MPRemoteCommand, Any)] = []
private var snapshot: Snapshot?
private var activity: NSObjectProtocol?
private var activityOptions: ProcessInfo.ActivityOptions = []
init(model: PlayerModel) {
self.model = model
@@ -75,6 +77,23 @@ final class SystemPlaybackControls {
}
func update() {
var options: ProcessInfo.ActivityOptions = []
if let model, model.fileName != nil,
model.state.status == PLAYER_PLAYING || model.castConnecting {
options = .userInitiated
if model.state.status == PLAYER_PLAYING, model.state.has_video,
model.castDevice == nil || model.castConnecting {
options.insert(.idleDisplaySleepDisabled)
}
}
if options != activityOptions {
// Acquire before releasing so a local/Cast handoff cannot briefly allow sleep.
let next = options.isEmpty ? nil :
ProcessInfo.processInfo.beginActivity(options: options, reason: "Playing media")
if let activity { ProcessInfo.processInfo.endActivity(activity) }
activity = next
activityOptions = options
}
let ready = model?.fileName != nil && model?.canTogglePlayback == true
let seekable = ready && model?.canSeek == true
for (command, _) in targets {
@@ -117,6 +136,9 @@ final class SystemPlaybackControls {
}
func shutdown() {
if let activity { ProcessInfo.processInfo.endActivity(activity) }
activity = nil
activityOptions = []
for (command, target) in targets {
command.removeTarget(target)
command.isEnabled = false