Direct HTML5 Audio - Quick & Simple
const playSound = (file, volume = 0.5) => {
const audio = new Audio(`/sounds/${file}`);
audio.volume = volume;
audio.play();
};/public/sounds/// 1. Simple button with sound
<button onClick={() => {
const audio = new Audio('/sounds/click.mp3');
audio.volume = 0.5;
audio.play();
}}>
Click me!
</button>
// 2. With other logic
<button onClick={() => {
// Play sound first
const audio = new Audio('/sounds/click.mp3');
audio.play();
// Then do your action
handleSubmit();
}}>
Submit
</button>
// 3. Reusable function
const playSound = (file) => {
const audio = new Audio(`/sounds/${file}`);
audio.volume = 0.5;
audio.play();
};
<button onClick={() => {
playSound('success.mp3');
showNotification('Saved!');
}}>
Save
</button>