Check out example codes for "play sound in javascript". It will help you in understanding the concepts better.
Code Example 1
<script>
function play() {
var audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3');
audio.play();
}
</script>
<button onclick-"play();">PLAY MY AUDIO</button>
Code Example 2
var audio = new Audio("folder_name/audio_file.mp3");
audio.play();
Code Example 3
var audio = new Audio('audio_file.mp3');
audio.play();
Code Example 4
<audio id="foobar" src="yoursample.ogg" preload="auto">
Code Example 5
import React, { useState, useEffect } from "react";
const useAudio = url => {
const [audio] = useState(new Audio(url));
const [playing, setPlaying] = useState(false);
const toggle = () => setPlaying(!playing);
useEffect(() => {
playing ? audio.play() : audio.pause();
},
[playing]
);
useEffect(() => {
audio.addEventListener('ended', () => setPlaying(false));
return () => {
audio.removeEventListener('ended', () => setPlaying(false));
};
}, []);
return [playing, toggle];
};
const Player = ({ url }) => {
const [playing, toggle] = useAudio(url);
return (
<div>
<button onClick={toggle}>{playing ? "Pause" : "Play"}</button>
</div>
);
};
export default Player;
Code Example 6
var bMusic = new Audio('welcome1.mp3')
bMusic.play()
Learn ReactJs, React Native from akashmittal.com