Force re-initialization of audio device when Play() is called

This commit is contained in:
NeilClack
2026-04-23 17:29:21 -05:00
parent c8756d0528
commit 10ba5df132
+12 -6
View File
@@ -390,13 +390,13 @@ func SwitchDevice(newName string) error {
} }
// Log device switch attempt // Log device switch attempt
oldDevice := deviceName // Automatically defaults to "system default"
oldDevice := CurrentDevice() // Which device is to be closed
if oldDevice == "" { if oldDevice == "" {
oldDevice = "system default" oldDevice = "system default"
} }
newDeviceLog := newName if newName == "" { // Which device is being switched to?
if newDeviceLog == "" { newName = "system default"
newDeviceLog = "system default"
} }
// Capture current state // Capture current state
@@ -421,11 +421,15 @@ func SwitchDevice(newName string) error {
mix.CloseAudio() mix.CloseAudio()
deviceOpen = false deviceOpen = false
} }
// Attempt to open the newName device
if err := openDeviceLocked(newName); err != nil { 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 { 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 return err
} }
@@ -451,6 +455,8 @@ func SwitchDevice(newName string) error {
} }
} }
logger.Info("Audio device switched to %s", newName)
return nil return nil
} }