简体   繁体   English

发布音频缓冲区Web音频API

[英]Release audio Buffer Web Audio API

I'm using WEB Audio API for a Webapp to render an Audio Signal. 我正在使用Web应用程序的WEB音频API来呈现音频信号。 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. 但是,我有一个问题,每秒加载一个音频文件,chrome使用越来越多的RAM,而且我不知道如何释放不再需要的缓冲区/声音。

Is there any way to solve my problem from my javascript program or changing chrome propieties? 有什么办法可以解决我的JavaScript程序问题或更改chrome属性吗?

My code: 我的代码:

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

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; 在上面从代码中提取的代码中,每次解码音频时,您都会看到一个新的AudioBuffer对象,该对象将添加到此数组中loader.bufferList[index] = buffer; That buffer is then later assigned to a Map with the URL being the key. 然后将该缓冲区分配给以URL为键的Map。 obj[name] = buffer;

While the array still holds reference to the AudioBuffer object it won't be garbage collected. 虽然数组仍保留对AudioBuffer对象的引用,但不会进行垃圾回收。 These AudioBuffers are actually pretty large since they hold the decoded audio. 这些AudioBuffer实际上非常大,因为它们保存解码的音频。 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) 实际的XHR响应request.response应该根据您的代码自动进行垃圾回收,但是它们并不会增加内存使用量,尤其是当您下载压缩文件(mp3等)时

To ensure that the AudioBuffer is garbage collected you should remove it from that sound Map when you're done using them. 为了确保AudioBuffer被垃圾回收,在使用完它们后,应将其从该声音映射中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM