Learn how to play audio
P粉760675452
P粉760675452 2023-08-21 11:45:13
0
2
534

I'm making a game using HTML5 and JavaScript.

How do I play game audio via JavaScript?

P粉760675452
P粉760675452

reply all (2)
P粉635509719

It's easy, just get youraudioelement and call theplay()method:

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.

    P粉512729862

    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(); }
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!