From 9bd1ea2038228e295a0f83c4f9e526ca42c6ce4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Wed, 29 Nov 2023 23:06:06 +0100 Subject: [PATCH] fix() fix music to play when click --- src/utils/music.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/utils/music.ts b/src/utils/music.ts index 809e241..aa3f4fb 100644 --- a/src/utils/music.ts +++ b/src/utils/music.ts @@ -1,13 +1,32 @@ import music from '../assets/music.mp3'; +let enabled = false; +let waitToPlay = false; + const musicAudio = new Audio(music); musicAudio.loop = true; musicAudio.volume = 0.5; export const playMusic = () => { - void musicAudio.play(); + if (enabled) { + void musicAudio.play(); + } else { + waitToPlay = true; + } }; export const pauseMusic = () => { - void musicAudio.pause(); + if (enabled) { + void musicAudio.pause(); + } else { + waitToPlay = false; + } +}; + +const enableMusic = () => { + enabled = true; + if (waitToPlay) void musicAudio.play(); }; + +document.addEventListener('click', enableMusic); +document.addEventListener('tap', enableMusic);