question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sound not playing when in useEffect()

See original GitHub issue

Hello, 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:open
  • Created 2 years ago
  • Reactions:13
  • Comments:5

github_iconTop GitHub Comments

1reaction
CodingMeSwiftlycommented, Apr 10, 2022

Same here. Minimal code to reproduce:

const [playSound] = useSound('path/to/file.mp3')
useEffect(() => {
    playSound()
}, [])

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.

useEffect(() => {
    playSound()
})

Works fine…

–Update–

I snapped and changed the code to new Audio('/path/to/file.mp3').play(). Works flawlessly 😄

0reactions
camdencommented, Jun 28, 2022

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found