当我尝试运行 XHR 加载样本时出现此错误。未捕获的 TypeError:值不是 AudioBuffer 类型。一切似乎都是正确的,但我不确定问题是什么。
Kit.prototype.load = function(){
if(this.startedLoading)
return;
this.startedLoading = true;
// var kick = "samples/M-808Sn2.wav";
var snare = "samples/M-808Sn2.wav";
// var hihat = "samples/M-808Sn2.wav";
// this.loadSample(0, kick, false);
this.loadSample(1, snare, false);
// this.loadSample(2, hihat, false);
}
我从请求开始:
Kit.prototype.loadSample = function(sampleID, url, mixToMono){
// Load Asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
var kit = this;
request.onload = function(){
var buffer;
context.decodeAudioData(request.response, function(decodedBuffer){
buffer = decodedBuffer;
});
switch(sampleID){
// case 0: kit.kickBuffer = buffer; break;
case 1: kit.snareBuffer = buffer; break;
// case 2: kit.hihatBuffer = buffer; break;
}
}
request.send();
}
然后我尝试运行它。
context = new webkitAudioContext();
var kit = new Kit();
kit.load();
var voice = context.createBufferSource();
voice.buffer = kit.snareBuffer;
voice.loop = true;
voice.playbackRate.value = 1;
voice.connect(gain);
voice.start(0);
voice.stop(2);