84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
I'm making a game using HTML5 and JavaScript.
How do I play game audio via JavaScript?
It's easy, just get youraudioelement and call theplay()method:
audio
play()
document.getElementById('yourAudioTag').play();
Check out this example:http://www.storiesinflight.com/html5/audio.html
This sitereveals some other cool things likeload(),pause()andaudioelements some other attributes.
load()
pause()
If you don't want to deal 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(); }
播放音频
It's easy, just get your
audio
element and call theplay()
method:Check out this example:http://www.storiesinflight.com/html5/audio.html
This sitereveals some other cool things like
load()
,pause()
andaudio
elements some other attributes.If you don't want to deal with HTML elements: