我正在使用WEB音頻API爲Webapp呈現音頻信號。但是,我遇到了一個問題,每秒鐘都要加載一個音頻文件,Chrome使用的RAM越來越多,我不知道如何釋放我不再需要的緩衝區/聲音。發佈音頻緩衝區Web音頻API
有沒有什麼辦法解決我的問題從我的JavaScript程序或更改鉻合作伙伴?
我的代碼:
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();
};
你能否顯示你的代碼(如果可能,匿名),以便有人可以給你一個特定的答案? – 2014-10-10 07:59:35
+1。我不能說,但我希望你的問題是你堅持緩衝區對象,而不是釋放它們(在這種情況下,正常的垃圾收集應該擺脫它們)。 – cwilso 2014-10-10 16:27:40