Preload multiple audio files

后端 未结 4 1868
既然无缘
既然无缘 2020-11-30 04:07

I have a single audio control on my webpage. I wish to use it to play multiple very short audio files depending on the state of the page. I do not wish to load the files as

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 04:53

    If you want more control than the element provides you can use Web Audio.

    Fetch your files in advance with XMLHttpRequest and pass them to decodeAudioData(). You then have AudioBuffer objects in memory that you can trigger playback of as required.

    Here's a demo of several short samples being preloaded and played back gaplessly (source).

    (Note: due to a ridiculous Chrome bug the above demo is currently Firefox-only. This is purely a codec issue: browser support for Web Audio is actually quite good. In a real application you should probably offer at least Vorbis and AAC for maximum compatibility.)

提交回复
热议问题