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 <noreply@anthropic.com>
This commit is contained in:
parent
aff4f5aef2
commit
2ba613fdd8
1 changed files with 22 additions and 0 deletions
|
|
@ -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
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue