在Chrome中接收“decodeAudioData error null”

时间:2014-04-21 18:30:47

标签: javascript html google-chrome audio web

Hello Web Audio Developers,

我在Chrome中收到“decodeAudioData error null” 和 Firebug中的“decodeAudioData错误未定义”。

萤火虫也说 “传递给decodeAudioData的缓冲区包含一个未知的内容类型。”

此代码是否有问题或 还有什么需要与之合作 “Web Audio API”?

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
    <title>Together 2 </title>
</head>
<body>
<script type="text/javascript">
  window.onload = init;
  var context;
  var bufferLoader;
  function init() {
    window.AudioContext = window.AudioContext || window.webkitAudioContext;
    context = new AudioContext();
    bufferLoader = new BufferLoader(context,
      [ '../web-audio/path/chrono.mp3' ],
      finishedLoading
    );
    bufferLoader.load();
}

function finishedLoading(bufferList) {
  var source1 = context.createBufferSource();
  source1.buffer = bufferList[0];
  source1.connect(context.destination);
  source1.start(0);
}

function BufferLoader(context, urlList, callback) {
  this.context = context;
  this.urlList = urlList;
  this.onload = callback;
  this.bufferList = new Array();
  this.loadCount = 0;
}
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();
}

BufferLoader.prototype.load = function() {
    for (var i = 0; i < this.urlList.length; ++i)
      this.loadBuffer(this.urlList[i], i);
    }
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这取决于wav文件本身使用的编解码器类型。只支持PCM而造成错误的文件是MS-ADPCM。您可以使用QuickTime或VLC获取编解码器信息,并在必要时将其转换为PCM。

我认为这与你的MP3有类似的问题。