Fix task cancellation bugs in MediaController, PlaybackController, and Recorder that could cause audio/media to resume during new recordings

This commit is contained in:
Beingpax 2026-01-01 15:12:52 +05:45
parent 0da24dc732
commit a702199b56
3 changed files with 24 additions and 13 deletions

View File

@ -86,18 +86,23 @@ class MediaController: ObservableObject {
let delay = audioResumptionDelay
let shouldUnmute = didMuteAudio && !wasAudioMutedBeforeRecording
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
let task = Task {
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
if Task.isCancelled {
return
if Task.isCancelled {
return
}
if shouldUnmute {
_ = executeAppleScript(command: "set volume without output muted")
}
didMuteAudio = false
currentMuteTask = nil
}
if shouldUnmute {
_ = executeAppleScript(command: "set volume without output muted")
}
didMuteAudio = false
currentMuteTask = nil
unmuteTask = task
await task.value
}
private func executeAppleScript(command: String) -> Bool {

View File

@ -108,13 +108,18 @@ class PlaybackController: ObservableObject {
return
}
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
let task = Task {
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
if Task.isCancelled {
return
if Task.isCancelled {
return
}
mediaController.play()
}
mediaController.play()
resumeTask = task
await task.value
}
private func isAppStillRunning(bundleId: String) -> Bool {

View File

@ -213,6 +213,7 @@ class Recorder: NSObject, ObservableObject {
deinit {
audioLevelCheckTask?.cancel()
audioMeterUpdateTask?.cancel()
audioRestorationTask?.cancel()
if let observer = deviceObserver {
NotificationCenter.default.removeObserver(observer)
}