Release audio Buffer Web Audio API

女生的网名这么多〃 提交于 2019-12-01 14:29:53

loader.context.decodeAudioData(
  request.response,
  function(buffer) {
    if (!buffer) {
      alert('error decoding file data: ' + url);
      return;
    }
    loader.bufferList[index] = buffer;
    if (++loader.loadCount == loader.urlList.length)
      loader.onload(loader.bufferList);
  },
  function(error) {
    console.error('decodeAudioData error', error);
  }
);

In the above piece of code extracted from your code, you'll see each time you decode the audio, you get a new AudioBuffer Object, which you're adding to this array loader.bufferList[index] = buffer; That buffer is then later assigned to a Map with the URL being the key. obj[name] = buffer;

While the array still holds reference to the AudioBuffer object it won't be garbage collected. These AudioBuffers are actually pretty large since they hold the decoded audio. Hence you're seeing large amounts of memory being used up.

The actual XHR responses request.response should be garbage collected automatically based on your code, but they wouldn't add much to the memory use especially if you're downloading compressed files (mp3, etc)

To ensure that the AudioBuffer is garbage collected you should remove it from that sound Map when you're done using them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!