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
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
}