From c9805d295e3f7b23e5948df5af8ffa8486a2d5c5 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Fri, 25 Jul 2025 11:23:09 +0545 Subject: [PATCH] Fix mute/unmute race condition --- VoiceInk/MediaController.swift | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/VoiceInk/MediaController.swift b/VoiceInk/MediaController.swift index 2685e0e..9af27ed 100644 --- a/VoiceInk/MediaController.swift +++ b/VoiceInk/MediaController.swift @@ -9,6 +9,7 @@ class MediaController: ObservableObject { static let shared = MediaController() private var didMuteAudio = false private var wasAudioMutedBeforeRecording = false + private var currentMuteTask: Task? @Published var isSystemMuteEnabled: Bool = UserDefaults.standard.bool(forKey: "isSystemMuteEnabled") { didSet { @@ -49,30 +50,44 @@ class MediaController: ObservableObject { func muteSystemAudio() async -> Bool { guard isSystemMuteEnabled else { return false } - // First check if audio is already muted - wasAudioMutedBeforeRecording = isSystemAudioMuted() + // Cancel any existing mute task and create a new one + currentMuteTask?.cancel() - // If already muted, no need to mute it again - if wasAudioMutedBeforeRecording { - return true + let task = Task { + // First check if audio is already muted + wasAudioMutedBeforeRecording = isSystemAudioMuted() + + // If already muted, no need to mute it again + if wasAudioMutedBeforeRecording { + return true + } + + // Otherwise mute the audio + let success = executeAppleScript(command: "set volume with output muted") + didMuteAudio = success + return success } - // Otherwise mute the audio - let success = executeAppleScript(command: "set volume with output muted") - didMuteAudio = success - return success + currentMuteTask = task + return await task.value } /// Restores system audio after recording func unmuteSystemAudio() async { guard isSystemMuteEnabled else { return } + // Wait for any pending mute operation to complete first + if let muteTask = currentMuteTask { + _ = await muteTask.value + } + // Only unmute if we actually muted it (and it wasn't already muted) if didMuteAudio && !wasAudioMutedBeforeRecording { _ = executeAppleScript(command: "set volume without output muted") } didMuteAudio = false + currentMuteTask = nil } /// Executes an AppleScript command