+ 1
How to play sound(WEB)
Could someone please tell me how I could play sound in HTML/CSS/JS
7 Respuestas
+ 2
You can use the audio tag.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio
+ 5
with js you could use
var audio=new Audio("song.mp3");
or html
using the <audio> tag
//Edited
+ 2
@Brains I think the JS way should be
const audio = new Audio('file.mp3');
audio.play(); // play the file
It uses the HTMLAudioElement interface described here, and works in the same way as the audio tag behind the scenes:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement
+ 2
@JobyJem no, you can also use ogg and wav. For maximum compatibility you should provide two or three sources, so for example browsers that don't support mp3 can fall back to ogg.
+ 1
@Splitty Dev does it have to be a .mp3 file
+ 1
@Splittu Dev how do you provide multiple sources
+ 1
@JobyJem just use multiple source tags inside of the audio tag. The link I posted on my first answer has an example with multiple sources :)