How to release memory using Web Audio API?

本小妞迷上赌 提交于 2019-12-22 09:06:03

问题


var context = new window.AudioContext()
var request = cc.loader.getXMLHttpRequest();
request.open("GET", 'res/raw-assets/resources/audio/bgm.mp3', true);
request.responseType = "arraybuffer";
request.onload = function () {
    context["decodeAudioData"](request.response, function(buffer){
        //success
        cc.log('success')
        window.buffer = buffer
        playBgm()
    }, function(){
        //error
    });
};
request.onerror = function(){
    //error
};
request.send();
function playBgm(){
    var audio = context["createBufferSource"]();
    audio.buffer = buffer;
    var _volume = context['createGain']();
    _volume['gain'].value = 1;
    _volume['connect'](context['destination']);
    audio["connect"](_volume);
    audio.start(0)
}

in my code I load a mp3 file and decode it into AudioBuffer(window.buffer) then I play it successful

but it cost a lot memory about 100MB How to release them? I tried like this

audio.stop()
audio = null
window.buffer = null
//context.close()
//context = null

chrome memory view

in the chrome memory view

sometimes the memory release in about 10second

sometimes release in about 1min

sometimes seems never release

I want to know if my code is the right way to release audiobuffer?

Do I need to close AudioContext?

来源:https://stackoverflow.com/questions/45896791/how-to-release-memory-using-web-audio-api

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