fix: prevent idle sleep during playback
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user