From 10ba5df132615abeca39b770dc5fa7bca04e79de Mon Sep 17 00:00:00 2001 From: NeilClack Date: Wed, 15 Apr 2026 11:58:44 -0500 Subject: [PATCH] Force re-initialization of audio device when Play() is called --- internal/mixer/mixer.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/internal/mixer/mixer.go b/internal/mixer/mixer.go index da7dff9..f143680 100644 --- a/internal/mixer/mixer.go +++ b/internal/mixer/mixer.go @@ -390,13 +390,13 @@ func SwitchDevice(newName string) error { } // Log device switch attempt - oldDevice := deviceName + // Automatically defaults to "system default" + oldDevice := CurrentDevice() // Which device is to be closed if oldDevice == "" { oldDevice = "system default" } - newDeviceLog := newName - if newDeviceLog == "" { - newDeviceLog = "system default" + if newName == "" { // Which device is being switched to? + newName = "system default" } // Capture current state @@ -421,11 +421,15 @@ func SwitchDevice(newName string) error { mix.CloseAudio() deviceOpen = false } + + // Attempt to open the newName device if err := openDeviceLocked(newName); err != nil { - // Best effort: try to reopen the previous device (or default) so we don't leave audio dead + // If error switching to new device, switch back to the old device if restoreErr := openDeviceLocked(oldDevice); restoreErr != nil { - } else { + // We should not hit an error when switching back, but if we do... + logger.Error("Failed to switch to device: %s", oldDevice) } + // Could not switch to the new device, return the error return err } @@ -451,6 +455,8 @@ func SwitchDevice(newName string) error { } } + logger.Info("Audio device switched to %s", newName) + return nil }