How to play audio?

前端 未结 20 3229
抹茶落季
抹茶落季 2020-11-21 13:37

I am making a game with HTML5 and JavaScript.

How could I play game audio via JavaScript?

20条回答
  •  暖寄归人
    2020-11-21 13:51

    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/examples/t-rex-roar.mp3');
      audio.play();
    }

    This uses the HTMLAudioElement interface, which plays audio the same way as the


    If you need more functionality, I used the howler.js library and found it simple and useful.

    
    

提交回复
热议问题