From a4340e8dab964536fe7d3d8631897c9a08fda85e Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Wed, 24 Jul 2024 15:33:36 +0200 Subject: [PATCH] feat(sounds): play call sound on secondary output device if set up Signed-off-by: Maksim Sukharev --- src/services/webNotificationsService.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/services/webNotificationsService.js b/src/services/webNotificationsService.js index 7d7af000..78ceda24 100644 --- a/src/services/webNotificationsService.js +++ b/src/services/webNotificationsService.js @@ -7,6 +7,7 @@ import { emit } from '@nextcloud/event-bus' import { loadState } from '@nextcloud/initial-state' import { generateFilePath } from '@nextcloud/router' import { Howl } from 'howler' +import BrowserStorage from './BrowserStorage.js' /** * Create a browser notification @@ -59,14 +60,26 @@ const createWebNotification = (notification) => { const playNotificationSound = (notification) => { if (notification.app === 'spreed' && notification.objectType === 'call') { if (loadState('notifications', 'sound_talk')) { - const sound = new Howl({ - src: [ - generateFilePath('notifications', 'img', 'talk.ogg'), - ], + const howlPayload = { + src: [generateFilePath('notifications', 'img', 'talk.ogg')], + html5: true, // to access HTMLAudioElement property 'sinkId' volume: 0.5, - }) - + } + const sound = new Howl(howlPayload) + const primaryDeviceId = sound._sounds[0]._node.sinkId ?? '' sound.play() + + const secondarySpeakerEnabled = BrowserStorage.getItem('secondary_speaker') === 'true' + const secondaryDeviceId = JSON.parse(BrowserStorage.getItem('secondary_speaker_device'))?.id ?? null + // Play only if secondary device is enabled, selected and different from primary device + if (secondarySpeakerEnabled && secondaryDeviceId && primaryDeviceId !== secondaryDeviceId) { + const soundDuped = new Howl(howlPayload) + const audioElement = sound._sounds[0]._node // Access the underlying HTMLAudioElement + audioElement.setSinkId?.(secondaryDeviceId) + .then(() => console.debug('Audio output successfully redirected to secondary speaker')) + .catch((error) => console.error('Failed to redirect audio output:', error)) + soundDuped.play() + } } } else if (loadState('notifications', 'sound_notification')) { const sound = new Howl({