发布音频Buffer Web Audio API [英] Release audio Buffer Web Audio API

查看:94
本文介绍了发布音频Buffer Web Audio API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WEB Audio API为Web应用程序呈现音频信号。但是,我有一个问题,每秒加载一个音频文件,chrome使用越来越多的RAM,我不知道如何释放我不再需要的缓冲区/声音。

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 idea how to release buffers / sounds that I no longer need.

有什么方法可以通过我的javascript程序解决我的问题或改变chrome的推荐吗?

Is there any way to solve my problem from my javascript program or changing chrome propieties?

我的代码:

loadSounds(this, {
    buffer: this.url
});

function loadSounds(obj, soundMap, callback) {
    // Array-ify
    var names = [];
    var paths = [];

    for (var name in soundMap) {
        var path = soundMap[name];
        names.push(name);
        paths.push(path);
    }

    bufferLoader = new BufferLoader(context, paths, function(bufferList) {
        for (var i = 0; i < bufferList.length; i++) {
            var buffer = bufferList[i];
            var name = names[i];
            obj[name] = buffer;
        }
        if (callback) {
            callback();
        }
    });
    bufferLoader.load();
}
function BufferLoader(context, urlList, callback) {
    this.context = context;
    this.urlList = urlList;
    this.onload = callback;
    this.bufferList = bufferListG;
    this.loadCount = 0;
}
BufferLoader.prototype.load = function() {
    for (var i = 0; i < this.urlList.length; ++i)
        this.loadBuffer(this.urlList[i], i);
};
BufferLoader.prototype.loadBuffer = function(url, index) {
    // Load buffer asynchronously
    var request = new XMLHttpRequest();
    request.open("GET", url, true);
    request.responseType = "arraybuffer";

    var loader = this;

    request.onload = function() {
        // Asynchronously decode the audio file data in request.response
        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);
            }
        );
    }

    request.onerror = function() {
        alert('BufferLoader: XHR error');
    }

    request.send();
};


推荐答案

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);
  }
);

在从代码中提取的上面一段代码中,每次解码音频时都会看到,你会得到一个新的AudioBuffer对象,你要添加到这个数组 loader.bufferList [index] = buffer; 然后将该缓冲区分配给一个URL为URL的Map。 obj [name] = buffer;

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;

虽然数组仍保留对AudioBuffer对象的引用,但它不会垃圾收集。这些AudioBuffers实际上非常大,因为它们保存解码后的音频。因此,你会看到大量的内存被耗尽。

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.

实际的XHR响应 request.response 应该是根据您的代码自动收集垃圾,但它们不会增加内存使用量,特别是如果您正在下载压缩文件(mp3等)

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)

确保AudioBuffer是垃圾收集的,你应该在使用它们时将它从声音地图中删除。

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

这篇关于发布音频Buffer Web Audio API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆