From f3248584c432de3071d5d8c58db7576fa7fa4ec9 Mon Sep 17 00:00:00 2001 From: Ajinkya Pande Date: Tue, 17 Dec 2024 14:36:26 +0530 Subject: [PATCH] Issueid #231942 feat: audio delay right after completing level in practice --- src/views/Practice/Practice.jsx | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/views/Practice/Practice.jsx b/src/views/Practice/Practice.jsx index 53520294..206d8107 100644 --- a/src/views/Practice/Practice.jsx +++ b/src/views/Practice/Practice.jsx @@ -70,9 +70,32 @@ const Practice = () => { } }, [startShowCase]); + const [audioSrc, setAudioSrc] = useState(null); + + useEffect(() => { + const preloadAudio = async () => { + try { + const response = await fetch(LevelCompleteAudio); + const audioBlob = await response.blob(); + const audioUrl = URL.createObjectURL(audioBlob); + setAudioSrc(audioUrl); + } catch (error) { + console.error("Error loading audio:", error); + } + }; + preloadAudio(); + }, []); + const callConfettiAndPlay = () => { - let audio = new Audio(LevelCompleteAudio); - audio.play(); + if (audioSrc) { + // Play preloaded audio if available + const audio = new Audio(audioSrc); + audio.play(); + } else { + // Fallback to LevelCompleteAudio if preloaded audio is not available + const fallbackAudio = new Audio(LevelCompleteAudio); + fallbackAudio.play(); + } callConfetti(); };