From 2ba613fdd8d8cf9d32f0df881bb2216d4f91aeed Mon Sep 17 00:00:00 2001 From: marwin Date: Sat, 4 Apr 2026 02:27:18 +0200 Subject: [PATCH] Reconnect audio automatically on device change Listens for navigator.mediaDevices.devicechange and re-routes the audio element to the new default output device without a page reload. Debounced 500ms to handle BT device bursts. Podcasts resume at the correct timestamp via loadedmetadata. Co-Authored-By: Claude Sonnet 4.6 --- static/js/app.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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 // ---------------------------------------------------------------------------