If you don't want to mess with HTML elements:
var audio = new Audio('audio_file.mp3');
audio.play();
function play() {
var audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3');
audio.play();
}
<button onclick="play()">Play Audio</button>
This uses the HTMLAudioElement interface, which plays audio the same way as the <audio> element.
If you need more functionality, I used the howler.js library and found it simple and useful.
<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.1/howler.min.js"></script>
<script>
var sound = new Howl({
src: ['https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3'],
volume: 0.5,
onend: function () {
alert('Finished!');
}
});
sound.play()
</script>
Answer from Uri on Stack OverflowIf you don't want to mess with HTML elements:
var audio = new Audio('audio_file.mp3');
audio.play();
function play() {
var audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3');
audio.play();
}
<button onclick="play()">Play Audio</button>
This uses the HTMLAudioElement interface, which plays audio the same way as the <audio> element.
If you need more functionality, I used the howler.js library and found it simple and useful.
<script src="https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.1/howler.min.js"></script>
<script>
var sound = new Howl({
src: ['https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3'],
volume: 0.5,
onend: function () {
alert('Finished!');
}
});
sound.play()
</script>
It's easy, just get your audio element and call the play() method:
document.getElementById('yourAudioTag').play();
Check out this example: http://www.storiesinflight.com/html5/audio.html
This site uncovers some of the other cool things you can do such as load(), pause(), and a few other properties of the audio element.
Trying to get audio to play using javascript.
Audio Play Error with Javascript
html5 - Play audio in javascript with a good performance - Game Development Stack Exchange
Play sound on click, then go to page
Videos
I'm trying to get an audio effect to play with a mouseover event but not having any luck.
Following https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement the following code should "just work" by the looks of it but I'm getting the following error message, which I'm not really sure exactly what it's telling me.
"Uncaught (in promise) DOMException: The media resource indicated by the src attribute or assigned media provider object was not suitable."
Code is:
const menuHover = new Audio("../audio/menu1.mp3");
function playSound() {
menuHover.play();
}
function hoverListener() {
const menuItem = document.querySelectorAll('.menu-item');
menuItem.forEach(item => {
item.addEventListener("mouseover", playSound)
})
}Any hints on where I should be looking appreciated.
You could attach the audio element to the body (Firefox won't play it else) and detach it, when it stops. So you can be sure it doesn't exist in the DOM anymore.
If you do it this way,
- No, 10 sounds won't impact the performance because they're gone after playback.
- No, they won't stay in memory because they get detached.
Use the Web Audio API.
The HTML5 audio tag was designed and implemented with use cases in mind of: streaming music, playing podcasts, and offering visual controls to users. The use case of playing many samples and triggering them at specific times was addressed later, and web standards discussions have evolved to offering that functionality in the Web Audio API.
The downside of the Web Audio API is complexity, but the additional power and quality is well worth it.
Check out samples.
Consider using a sound library to simplify use, e.g. search for "html5 audio library for games". Some results include SoundJS and SoundManager2.