+ 1

How to play sound(WEB)

Could someone please tell me how I could play sound in HTML/CSS/JS

30th Jan 2018, 8:34 PM
eee
7 ответов
30th Jan 2018, 8:39 PM
SplittyDev
SplittyDev - avatar
+ 5
with js you could use var audio=new Audio("song.mp3"); or html using the <audio> tag //Edited
30th Jan 2018, 8:45 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 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
30th Jan 2018, 8:47 PM
SplittyDev
SplittyDev - avatar
+ 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.
30th Jan 2018, 8:50 PM
SplittyDev
SplittyDev - avatar
+ 1
@Splitty Dev does it have to be a .mp3 file
30th Jan 2018, 8:49 PM
eee
+ 1
@Splittu Dev how do you provide multiple sources
30th Jan 2018, 11:30 PM
eee
+ 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 :)
31st Jan 2018, 5:23 AM
SplittyDev
SplittyDev - avatar