Recorder.js WebAudio API TypeError尝试加载缓冲区

时间:2015-10-04 21:19:35

标签: javascript audio html5-audio web-audio

我用recorder.js记录用户。我试图将录音的缓冲区发送到缓冲区列表,这样当我想一次播放所有声音时,程序循环遍历缓冲区数组并将它们转换为源节点并播放它们。但是,它抛出了一个TypeError。这是代码:

function layerRecording() {
  var newBuffer;
  rec.exportWAV(function(wav) {
  var url = window.URL.createObjectURL(wav);
  console.log(url);
  var getSound = new XMLHttpRequest(); // Load the Sound with XMLHttpRequest
  getSound.open("GET", url, true);
  getSound.responseType = "arraybuffer"; // Read as Binary Data
  getSound.onload = function() {
    context.decodeAudioData(getSound.response, function(buffer){
      newBuffer = buffer; // Decode the Audio Data and Store it in a Variable
    });
    buffers.push(newBuffer); //adds to the list of buffers
  }
  getSound.send(); // Send the Request and Load the File
});
}

//And then later on when I call this with buffers:
var playAll = function(bufferList) {
  	sourceList=[];
		for (var i=0; i<bufferList.length;i++){
			var currentSource = context.createBufferSource();
			console.log(i);
			currentSource.buffer = bufferList[i];
			currentSource.connect(context.destination); 
			sourceList[i]=currentSource;
		}
		for (var i=0; i<sourceList.length;i++){ 
			sourceList[i].start(0,startOffset);
		}
	}

我对录制的音频的迭代产生以下错误: 未捕获的TypeError:无法在'AudioBufferSourceNode'上设置'buffer'属性:提供的值不是'AudioBuffer'类型。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

没关系,这是一个范围问题。只需要在decodeAudioData()中移动push语句。糟糕