React Useaudio Hook Not Playing Sound [Solved]

Issue Probable Cause Primary Solution
No Sound on Load Browser Autoplay Policy Trigger play() via user interaction (click/tap).
State Not Updating Stale Closures Use useRef to track the Audio object.
404 File Not Found Incorrect Asset Path Move file to public/ folder or use import.

Debugging React useAudio hook not playing sound on a laptop screen.

What is the React useAudio Hook?

The useAudio hook is a custom React hook—often found in libraries like react-use—designed to manage HTML5 audio elements. It provides an easy interface to play, pause, and track the progress of sound files within a component.

Unlike standard HTML audio tags, this hook returns the current state of the audio (playing, duration, time) and controls to manipulate it. However, developers frequently encounter “silence” where the hook technically runs, but no sound is produced.

Step-by-Step Solutions

1. Handle Browser Autoplay Policies

Modern browsers like Chrome and Safari block audio from playing automatically to protect the user experience. Audio must be initiated by a user gesture, such as a click.

If your useAudio hook is called inside a useEffect on mount, it will likely fail. Ensure the play() function is attached to a button click.


// Example of a user-triggered play action
const [audio, state, controls] = useAudio({
  src: '/sound.mp3',
});

return (
  <button onClick={controls.play}>Play Sound</button>
);

2. Verify the Audio Source Path

React handles assets differently depending on your build tool (Vite or Webpack). If your audio file is in the src/assets folder, you must import it. If it is in the public/ folder, you can reference it as a string starting from the root.

Incorrect paths are the leading cause of “not playing” errors. Check your browser console for 404 errors related to your .mp3 or .wav files.

3. Use Refs for Audio Persistent State

If you are building a custom hook, using useState for the Audio object can cause re-renders that reset the audio element. Instead, use useRef to maintain the same HTMLAudioElement throughout the component lifecycle.


const audioRef = useRef(new Audio(url));

const play = () => {
  audioRef.current.play();
};

4. Check Volume and Mute States

Sometimes the hook is working perfectly, but the initial state is set to muted: true or volume: 0. Programmatically check these values before calling the play method.

Always include an error handler in your play promise to catch issues related to interrupted loading or permission denials.