From 5f22347d7aadeb829748a7f435a4ccb0467f341f Mon Sep 17 00:00:00 2001 From: bbedward Date: Sat, 1 Nov 2025 10:14:30 -0400 Subject: [PATCH] audio: re-create players on default device change --- Services/AudioService.qml | 58 +++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/Services/AudioService.qml b/Services/AudioService.qml index d2a1c60f..1d633c0a 100644 --- a/Services/AudioService.qml +++ b/Services/AudioService.qml @@ -28,6 +28,9 @@ Singleton { property var normalNotificationSound: null property var criticalNotificationSound: null + property var mediaDevices: null + property var mediaDevicesConnections: null + signal micMuteChanged Timer { @@ -241,6 +244,37 @@ Singleton { } } + function setupMediaDevices() { + if (!soundsAvailable || mediaDevices) { + return + } + + try { + mediaDevices = Qt.createQmlObject(` + import QtQuick + import QtMultimedia + MediaDevices {} + `, root, "AudioService.MediaDevices") + + if (mediaDevices) { + mediaDevicesConnections = Qt.createQmlObject(` + import QtQuick + Connections { + function onDefaultAudioOutputChanged() { + console.log("AudioService: Default audio output changed, recreating sound players") + root.destroySoundPlayers() + root.createSoundPlayers() + } + } + `, root, "AudioService.MediaDevicesConnections") + mediaDevicesConnections.target = mediaDevices + } + } catch (e) { + console.log("AudioService: MediaDevices not available, using default audio output") + mediaDevices = null + } + } + function destroySoundPlayers() { if (volumeChangeSound) { volumeChangeSound.destroy() @@ -269,14 +303,20 @@ Singleton { return } + setupMediaDevices() + try { + const deviceProperty = mediaDevices ? `device: root.mediaDevices.defaultAudioOutput\n ` : "" + const volumeChangePath = getSoundPath("audio-volume-change") volumeChangeSound = Qt.createQmlObject(` import QtQuick import QtMultimedia MediaPlayer { source: "${volumeChangePath}" - audioOutput: AudioOutput { volume: 1.0 } + audioOutput: AudioOutput { + ${deviceProperty}volume: 1.0 + } } `, root, "AudioService.VolumeChangeSound") @@ -286,7 +326,9 @@ Singleton { import QtMultimedia MediaPlayer { source: "${powerPlugPath}" - audioOutput: AudioOutput { volume: 1.0 } + audioOutput: AudioOutput { + ${deviceProperty}volume: 1.0 + } } `, root, "AudioService.PowerPlugSound") @@ -296,7 +338,9 @@ Singleton { import QtMultimedia MediaPlayer { source: "${powerUnplugPath}" - audioOutput: AudioOutput { volume: 1.0 } + audioOutput: AudioOutput { + ${deviceProperty}volume: 1.0 + } } `, root, "AudioService.PowerUnplugSound") @@ -306,7 +350,9 @@ Singleton { import QtMultimedia MediaPlayer { source: "${messagePath}" - audioOutput: AudioOutput { volume: 1.0 } + audioOutput: AudioOutput { + ${deviceProperty}volume: 1.0 + } } `, root, "AudioService.NormalNotificationSound") @@ -316,7 +362,9 @@ Singleton { import QtMultimedia MediaPlayer { source: "${messageNewInstantPath}" - audioOutput: AudioOutput { volume: 1.0 } + audioOutput: AudioOutput { + ${deviceProperty}volume: 1.0 + } } `, root, "AudioService.CriticalNotificationSound") } catch (e) {