From 4a44aaf23e0af9fd4b6b224c97d01ee1cc22c221 Mon Sep 17 00:00:00 2001 From: GuillaumeSD <47183782+GuillaumeSD@users.noreply.github.com> Date: Sun, 11 May 2025 21:24:52 +0200 Subject: [PATCH] fix : play sound on move --- src/hooks/useChessActions.ts | 4 +++- src/lib/sounds.ts | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/hooks/useChessActions.ts b/src/hooks/useChessActions.ts index b7d4e92..1b2ec98 100644 --- a/src/hooks/useChessActions.ts +++ b/src/hooks/useChessActions.ts @@ -96,10 +96,12 @@ export const useChessActions = (chessAtom: PrimitiveAtom) => { (moves: string[]) => { const newGame = copyGame(); + let lastMove: Move | null = null; for (const move of moves) { - newGame.move(move); + lastMove = newGame.move(move); } setGame(newGame); + if (lastMove) playSoundFromMove(lastMove); }, [copyGame, setGame] ); diff --git a/src/lib/sounds.ts b/src/lib/sounds.ts index 7432362..1c34639 100644 --- a/src/lib/sounds.ts +++ b/src/lib/sounds.ts @@ -6,11 +6,12 @@ let audio: HTMLAudioElement | null = null; const playSound = async (url: string) => { if (!audio) audio = new Audio(); - const isPlaying = !audio.paused && !audio.ended; - if (isPlaying) return; - audio.src = url; - await audio.play(); + try { + await audio.play(); + } catch { + console.warn("Audio play failed"); + } }; export const playCaptureSound = () => playSound("/sounds/capture.webm");