How to play audio?

前端 未结 20 3254
抹茶落季
抹茶落季 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:57

    If you want to be able to play the sound even when the browser-tab is currently not selected, you have to load the audio-resource on page load.

    Like so:

    var audio = new Audio('audio/path.mp3');
    
    function playSound(){
        audio.play();
    }
    

    See this question for more detail

提交回复
热议问题