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>
<audio id="audio" src="http://www.soundjay.com/button/beep-07.wav" autoplay="false" ></audio>
<a onclick="playSound();"> Play</a>
<script>
function playSound() {
var sound = document.getElementById("audio");
sound.play();
}
</script>
var sample = document.getElementById("foobar");
sample.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>
<audio id="foobar" src="yoursample.ogg" preload="auto">
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.
const sound = require("sound-play");
sound.play("file.mp3");
#foobar { display: none }