var audio = new Audio("folder_name/audio_file.mp3");
audio.play();
var audio = new Audio("soundfile.wav");
document.onclick = function() {
audio.play();
}
<button onclick="playSound()">Play</button>
<script>
let playSound = () => new Audio("src.mp3").play();
</scirpt>
<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>
var audio = new Audio('audio_file.mp3');
audio.play();
var bMusic = new Audio('welcome1.mp3')
bMusic.play()
//play audio with from html audio element:
document.getElementById('myAudioTagID').play();
//play audio with out html audio tag
var myAudio = new Audio('my_great_song.mp3');
myAudio.play();
<!doctype html>
<html>
<head>
<title>Audio</title>
</head>
<body>
<script>
function play() {
var audio = document.getElementById("audio");
audio.play();
}
</script>
<input type="button" value="PLAY" onclick="play()">
<audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>
</body>
</html>
JavaScript
function playAudio(url) {
new Audio(url).play();
}
HTML
<img src="image.png" onclick="playAudio('mysound.mp3')">
Supported in most modern browsers and easy to embed into HTML elements.