Release audio Buffer Web Audio API

前端 未结 1 1397
春和景丽
春和景丽 2021-01-17 03:13

I\'m using WEB Audio API for a Webapp to render an Audio Signal. But, I have a problem, being loading an audio file each second, chrome use more and more RAM and I have no i

1条回答
  •  不思量自难忘°
    2021-01-17 03:37

    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.

    0 讨论(0)
提交回复
热议问题