From c69ec730054dae6e7fa9e0a9f4309bd83fb1dfa2 Mon Sep 17 00:00:00 2001 From: GuillaumeSD Date: Wed, 27 Mar 2024 01:19:47 +0100 Subject: [PATCH] fix : don't play sounds on top of each other --- src/lib/sounds.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lib/sounds.ts b/src/lib/sounds.ts index 5e0d648..85192c7 100644 --- a/src/lib/sounds.ts +++ b/src/lib/sounds.ts @@ -1,8 +1,15 @@ import { Move } from "chess.js"; import { getWhoIsCheckmated, isCheck } from "./chess"; +let audio: HTMLAudioElement | null = null; + const playSound = async (url: string) => { - const audio = new Audio(url); + if (!audio) audio = new Audio(); + + const isPlaying = !audio.paused && !audio.ended; + if (isPlaying) return; + + audio.src = url; await audio.play(); };