diff --git a/static/js/app.js b/static/js/app.js index 1c7c628..438a675 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -33,6 +33,28 @@ let feedSortOrder = 'alpha'; const audio = new Audio(); +// Reconnect audio when the output device changes (e.g. Bluetooth, USB DAC) +if (navigator.mediaDevices) { + let _deviceChangeDebounce = null; + navigator.mediaDevices.addEventListener('devicechange', () => { + clearTimeout(_deviceChangeDebounce); + _deviceChangeDebounce = setTimeout(() => { + if (!isPlaying || audio.src === '') return; + const savedTime = audio.currentTime; + const savedSrc = audio.src; + audio.src = savedSrc; + audio.load(); + if (savedTime > 0) { + audio.addEventListener('loadedmetadata', function onMeta() { + audio.currentTime = savedTime; + audio.removeEventListener('loadedmetadata', onMeta); + }); + } + audio.play().catch(() => {}); + }, 500); + }); +} + // --------------------------------------------------------------------------- // DOM helpers // ---------------------------------------------------------------------------