fix : play sound on move
This commit is contained in:
@@ -96,10 +96,12 @@ export const useChessActions = (chessAtom: PrimitiveAtom<Chess>) => {
|
|||||||
(moves: string[]) => {
|
(moves: string[]) => {
|
||||||
const newGame = copyGame();
|
const newGame = copyGame();
|
||||||
|
|
||||||
|
let lastMove: Move | null = null;
|
||||||
for (const move of moves) {
|
for (const move of moves) {
|
||||||
newGame.move(move);
|
lastMove = newGame.move(move);
|
||||||
}
|
}
|
||||||
setGame(newGame);
|
setGame(newGame);
|
||||||
|
if (lastMove) playSoundFromMove(lastMove);
|
||||||
},
|
},
|
||||||
[copyGame, setGame]
|
[copyGame, setGame]
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,11 +6,12 @@ let audio: HTMLAudioElement | null = null;
|
|||||||
const playSound = async (url: string) => {
|
const playSound = async (url: string) => {
|
||||||
if (!audio) audio = new Audio();
|
if (!audio) audio = new Audio();
|
||||||
|
|
||||||
const isPlaying = !audio.paused && !audio.ended;
|
|
||||||
if (isPlaying) return;
|
|
||||||
|
|
||||||
audio.src = url;
|
audio.src = url;
|
||||||
|
try {
|
||||||
await audio.play();
|
await audio.play();
|
||||||
|
} catch {
|
||||||
|
console.warn("Audio play failed");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const playCaptureSound = () => playSound("/sounds/capture.webm");
|
export const playCaptureSound = () => playSound("/sounds/capture.webm");
|
||||||
|
|||||||
Reference in New Issue
Block a user