Sound not playing when in useEffect()
See original GitHub issueHello, I am using this with socket.io and my code looks like this:
import moveAudioFile from "../assets/sounds/Move.mp3";
import captureAudioFile from "../assets/sounds/Capture.mp3";
import gameStartAudioFile from "../assets/sounds/GameStart.mp3";
export default function Example() {
const [playMoveSound] = useSound(moveAudioFile);
const [playCaptureSound] = useSound(captureAudioFile);
const [playGameStartSound] = useSound(gameStartAudioFile);
useEffect(() => {
socket.on("opponentMoved", (moveMade) => {
let move = null;
safeGameMutate((game) => {
move = game.move({
from: moveMade.move.sourceSquare,
to: moveMade.move.targetSquare,
promotion: "q", // always promote to a queen for example simplicity
});
if (move && move.captured !== undefined) {
console.log("capture sound");
playCaptureSound();
} else {
console.log("move sound");
playMoveSound();
}
});
setFen(game.fen());
});
}, []);
}
It is properly printing out “capture sound” and “move sound” to the console but no sound plays on either. Do you have any suggestions? Thanks!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:13
- Comments:5
Top Results From Across the Web
React, is there a way to play sound on useEffect?
i thinks would be better if u make seperate component just for sound like this: sandbox. import { useEffect } from "react"; import...
Read more >When I run play() inside useEffect in useAudio, no sound is ...
Safari (and most browsers) wont autoplay audio without user interaction. It will start playing muted if at all.
Read more >Audio
Creates and loads a new Sound object to play back the Recording . Note that this will only succeed once the Recording is...
Read more >Playing audio with useEffect hook on page load-Reactjs
[Solved]-Playing audio with useEffect hook on page load-Reactjs · score:2. Accepted answer. const [audio, SetAudio] = useState(""); const Playit = () => {...
Read more >React useEffect Hook usages you must know - GreenRoots Blog
The first is the default case. If you do not pass the dependency array to the useEffect hook, the callback function executes on...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Same here. Minimal code to reproduce:
Very weird issue: In a dev environment (next dev with HMR), sound doesn’t play when the page loads initially, but on each subsequent refresh triggered by HMR, the sound plays.
Works fine…
–Update–
I snapped and changed the code to
new Audio('/path/to/file.mp3').play()
. Works flawlessly 😄@CodingMeSwiftly OMG the
new Audio
thing worked. Thank you so much!OP’s problem is identical to mine. I would love to see a fix that would allow me to use
use-sound
. But this works!