From a702199b5619a3480b869135524a465e4babf129 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Thu, 1 Jan 2026 15:12:52 +0545 Subject: [PATCH] Fix task cancellation bugs in MediaController, PlaybackController, and Recorder that could cause audio/media to resume during new recordings --- VoiceInk/MediaController.swift | 23 ++++++++++++++--------- VoiceInk/PlaybackController.swift | 13 +++++++++---- VoiceInk/Recorder.swift | 1 + 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/VoiceInk/MediaController.swift b/VoiceInk/MediaController.swift index 34a133e..07db267 100644 --- a/VoiceInk/MediaController.swift +++ b/VoiceInk/MediaController.swift @@ -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 { diff --git a/VoiceInk/PlaybackController.swift b/VoiceInk/PlaybackController.swift index 047c638..54b280f 100644 --- a/VoiceInk/PlaybackController.swift +++ b/VoiceInk/PlaybackController.swift @@ -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 { diff --git a/VoiceInk/Recorder.swift b/VoiceInk/Recorder.swift index b4433bc..b7c0a3c 100644 --- a/VoiceInk/Recorder.swift +++ b/VoiceInk/Recorder.swift @@ -213,6 +213,7 @@ class Recorder: NSObject, ObservableObject { deinit { audioLevelCheckTask?.cancel() audioMeterUpdateTask?.cancel() + audioRestorationTask?.cancel() if let observer = deviceObserver { NotificationCenter.default.removeObserver(observer) }