sounds: suppress error when user has disabled autoplay

This commit is contained in:
Alex Gleason 2023-10-15 17:19:02 -05:00
parent 1604e5061b
commit 1bfe5908a9
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -19,7 +19,7 @@ const createAudio = (sources: Sound[]): HTMLAudioElement => {
}; };
/** Play HTML5 sound. */ /** Play HTML5 sound. */
const play = (audio: HTMLAudioElement): void => { const play = (audio: HTMLAudioElement): Promise<void> => {
if (!audio.paused) { if (!audio.paused) {
audio.pause(); audio.pause();
if (typeof audio.fastSeek === 'function') { if (typeof audio.fastSeek === 'function') {
@ -29,7 +29,15 @@ const play = (audio: HTMLAudioElement): void => {
} }
} }
audio.play(); return audio.play().catch((error: Error) => {
if (error.name === 'NotAllowedError') {
// User has disabled autoplay.
// https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide
return;
} else {
throw error;
}
});
}; };
const soundCache: Record<Sounds, HTMLAudioElement> = { const soundCache: Record<Sounds, HTMLAudioElement> = {