Handle play() and load() promises for audio elements
Updated audio playback and preloading logic to check for and handle returned promises from play() and load() methods. This prevents uncaught promise rejections in browsers where these methods may return undefined, improving reliability and error handling for notification sounds.
This commit is contained in:
@@ -12,9 +12,16 @@ export function AudioPreloader() {
|
||||
audio.preload = 'auto'
|
||||
|
||||
// Try to load it immediately
|
||||
audio.load().catch(err => {
|
||||
try {
|
||||
const loadPromise = audio.load();
|
||||
if (loadPromise !== undefined) {
|
||||
loadPromise.catch(err => {
|
||||
console.log('Audio preload failed (non-critical):', err)
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('Audio preload failed (non-critical):', err)
|
||||
})
|
||||
}
|
||||
|
||||
return () => {
|
||||
// Cleanup
|
||||
|
||||
Reference in New Issue
Block a user