From 9e77b59d3be8c71d52309fd9f1fc9b4cf3afa4e5 Mon Sep 17 00:00:00 2001 From: Jeremie Pardou <571533+jrmi@users.noreply.github.com> Date: Mon, 25 Sep 2023 08:56:41 +0200 Subject: [PATCH] Fix audio bug --- src/utils/index.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 51056afe..eb5c61dc 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -101,6 +101,7 @@ export const preloadAudio = (urls) => { urls.forEach((url) => { if (!audioFiles[url]) { audioFiles[url] = new Audio(url); + audioFiles[url].load(); } }); }; @@ -108,16 +109,16 @@ export const preloadAudio = (urls) => { export const playAudio = (url, volume = 1) => { if (!audioFiles[url]) { audioFiles[url] = new Audio(url); + audioFiles[url].load(); } - audioFiles[url].pause(); - audioFiles[url].currentTime = 0; - audioFiles[url].volume = volume; - try { - audioFiles[url].play(); - } catch (e) { - console.log("Fail to play audio", e); + if (!audioFiles[url].paused) { + audioFiles[url].pause(); + audioFiles[url].load(); } + audioFiles[url].volume = volume; + + audioFiles[url].play().catch((e) => console.log("Fail to play audio", e)); }; export const triggerFileDownload = (url, filename) => {