How to play audio?

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

    You can use Web Audio API for playing sounds. There are quite some audio libraries out there like howler.js, soundjs etc. If you don't worry about old browsers then you can also check on http://musquitojs.com/. It provides a simple API to create and play sounds.

    For example, to play a sound all you've to do is.

    import $buzz from 'musquito';
    
    const buzz = $buzz('gunfire.mp3');
    
    buzz.play();
    

    The library also supports Audio Sprites.

提交回复
热议问题