Skip to content

Commit

Permalink
Fix audio bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmi committed Sep 25, 2023
1 parent 83978b5 commit 9e77b59
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,24 @@ export const preloadAudio = (urls) => {
urls.forEach((url) => {
if (!audioFiles[url]) {
audioFiles[url] = new Audio(url);
audioFiles[url].load();
}
});
};

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) => {
Expand Down

0 comments on commit 9e77b59

Please sign in to comment.