10

尝试学习音频 API,但我收到 BufferLoader 类的未捕获参考错误。我在 chrome 上,它是最新的。这门课不应该没有问题吗?

<html>
<head>
<script type=text/javascript>

 window.onload = init;
 var context;
 var bufferLoader;



    function init(){

    context = new webkitAudioContext();
    bufferLoader = new BufferLoader(
          context,
          [
             ' https://dl.dropboxusercontent.com/u/1957768/kdFFO3.wav',
             ' https://dl.dropboxusercontent.com/u/1957768/geniuse%20meodies.wav',
          ],
          finishedLoading 
        );
    bufferLoader.load();
 }

     function finishedLoading(bufferList){
    //make two sources and play them
    var source1 = context.createBufferSource();
    var source2 = context.createBufferSource();
    source1.buffer = bufferList[0];
    source2.buffer = bufferList[1];

    source1.connect(context.destination);
    source2.connect(context.destination);
    source1.start(0);
    source2.start(0);
 }


   </script>
   </head>
   <body>
   </body>
   </html>
4

1 回答 1

18

BufferLoader类”是为抽象 Web Audio API 的使用而创建的自定义函数。它不是内置功能,必须包含在您的页面中才能使用;Chrome 没有什么特别之处。这是一个解释它的例子:http: //www.html5rocks.com/en/tutorials/webaudio/intro/#toc-abstract

要使用,请在使用之前包含此代码:

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);
}
于 2013-06-27T03:05:36.610 回答