Hooks version (React 16.8+):
Minimal version.
Place your music file (mp3) in the public folder.
import React from 'react';
function App() {
let audio = new Audio("/christmas.mp3")
const start = () => {
audio.play()
}
return (
< div >
<button onClick={start}>Play</button>
</div >
);
}
export default App;
Answer from Hunter on Stack Overflow Top answer 1 of 8
72
Hooks version (React 16.8+):
Minimal version.
Place your music file (mp3) in the public folder.
import React from 'react';
function App() {
let audio = new Audio("/christmas.mp3")
const start = () => {
audio.play()
}
return (
< div >
<button onClick={start}>Play</button>
</div >
);
}
export default App;
2 of 8
16
You can import the audio using import statement and create a Audio object and call the play() method using the instantiated object.
import React from "react";
import audio from './../assets/audios/success.mp3';
class AudioTest extends React.Component{
playAudio = () => {
new Audio(audio).play();
}
render() {
return (
<div>
<button onClick={this.playAudio}>PLAY AUDIO</button>
</div>
);
}
}
export default AudioTest;
Josh W. Comeau
joshwcomeau.com › react › announcing-use-sound-react-hook
use-sound | a React hook that lets you play sound effects • Josh W. Comeau
A sprite is an audio file with many different sounds. By combining them into a single file, it can be a bit nicer to work with, plus you avoid many parallel HTTP requests. Here we use a sprite to build a drum machine! Test it out by clicking/tapping on the buttons, or using the numbers 1 through 4 on your keyboard.*If the keyboard shortcuts aren’t working for you, try clicking one of the buttons first. This is necessary because the playgrounds on this blog use iframes. ... import React from 'react'; import useSound from 'use-sound'; import styles from './DrumMachineDemo.module.css'; function DrumMachineDemo() { const soundUrl = '/sounds/909-drums.mp3'; const [play] = useSound(soundUrl, { sprite: { kick: [0, 350], hihat: [374, 160], snare: [666, 290], cowbell: [968, 200], } }); // Assign each drum sound to a key.
Playing sound with React
Hi all. I’ve been trying to get my code to play a sound in React. Off course I tried with the element in my React render method first, but that didn’t work. I have this now: [https://codepen.io/cynthiab72/pen/BazyvLg?editors=0111] from what I read here on the forums , others were having ... More on forum.freecodecamp.org
reactjs - Why does my "Audio-Button" don`t play a sound (onClick) - Stack Overflow
I am struggeling on finding out why my button dont play a sound when I click on it. The console.log() test works fine, but the -part dont. I also tried some npm-packets to solve the problem, but it... More on stackoverflow.com
reactjs - Click a button to play sound on React.js - Stack Overflow
I am having trouble creating a button that plays sound or music using React.js. Can someone help me about this? More on stackoverflow.com
Play an Audio File WITHOUT User Interaction
I'm not sure how feasible this would be in woocommerce, but I'd suggest changing the flow so that the user loads the page once, you force some time of UX interaction (e.g., a modal with a button), and then you poll for new orders via the API without reloading the page (to avoid requiring new interactions each time). This should also improve performance since a full refresh every 30 seconds seems pretty heavyweight. You might still need to refresh after detecting a new order though, depending on how the woocommerce admin pages work. More on reddit.com
Videos
Educative
educative.io › answers › how-to-play-sound-in-react
How to play sound in React
Line 5: The useSound hook accepts ... the sound when invoked. Line 9: Assigning playSound to the onClick event handler of the button ensures that clicking the button will play the audio file....
YouTube
youtube.com › watch
How to Play Audio Clips on the Click of a Button in ReactJS - YouTube
#arslan #ReactJS #developmentHopefully yall liked this video. Remember just because you completed this tutorial dosen't mean your done add a different featur...
Published May 24, 2023
React.js Examples
reactjsexample.com › a-react-hook-for-playing-sound-effects
A React Hook for playing sound effects
April 15, 2020 - const Arcade = () => { const [play, { sound }] = useSound('/win-theme.mp3'); return ( <button onClick={() => { // You win! Fade in the victory theme sound.fade(0, 1, 1000); }} > Click to win </button> ); }; ... Manage temporary UI elements with react hook allowing you to show or hide an element 19 January 2024
YouTube
youtube.com › watch
How To Play Audio From An External URL On The Click Of A Button In ReactJS - YouTube
Please don't forget Like, Comment and Subscribe if you're new!Hope y'all enjoyed this tutorial this was a suggestion. We will learn how to play any audio cli...
Published February 20, 2026
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-toggle-play-pause-in-reactjs-with-audio
How to toggle play/pause in ReactJS with audio ? - GeeksforGeeks
July 23, 2025 - import React, { Component } from "react"; // Import your audio file import song from "./static/a.mp3"; class App extends Component { // Create state state = { // Get audio file in a variable audio: new Audio(song), // Set initial state of song isPlaying: false, }; // Main function to handle both play and pause operations playPause = () => { // Get state of song let isPlaying = this.state.isPlaying; if (isPlaying) { // Pause the song if it is playing this.state.audio.pause(); } else { // Play the song if it is paused this.state.audio.play(); } // Change the state of song this.setState({ isPlaying: !isPlaying }); }; render() { return ( <div> {/* Show state of song on website */} <p> {this.state.isPlaying ? "Song is Playing" : "Song is Paused"} </p> {/* Button to call our main function */} <button onClick={this.playPause}> Play | Pause </button> </div> ); } } export default App;
Educative
educative.io › answers › how-to-play-sound-when-hovering-in-react
How to play sound when hovering in React
By attaching the onMouseEnter event handler to an element, you can trigger sound playback during hover actions. For example, hovering over a button can play a sound using the play function from the useSound Hook.
Medium
theshubhagrwl.medium.com › you-might-not-need-a-sound-library-for-react-a265870dabda
You might not need a sound library for React | by Shubh Agrawal | Medium
December 8, 2022 - There are several buttons and when the user clicks on the button a certain audio is played. ... JavaScript takes care of the types but with TypeScript you gotta watch your step. Till now I have only worked with images in React so I googled how to use audio files and two libraries shot up. ... The work of libraries is to make the implementation simple and these perfectly do that. I implemented as it says in the docs. import useSound from 'use-sound'; import boopSfx from '../../sounds/boop.mp3'; const BoopButton = () => { const [play] = useSound(boopSfx); return <button onClick={play}>Boop!</button>; };