2013-06-27 36 views
8

Đang cố gắng tìm hiểu API âm thanh, nhưng tôi gặp lỗi tham chiếu Uncaught cho lớp BufferLoader. Tôi đang trên chrome và nó được cập nhật. Lớp học này có nên làm việc không có vấn đề gì không?Lỗi tham chiếu không bắt buộc BufferLoader không được xác định

<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> 
+0

đâu là tài liệu cho này 'BufferLoader'? Bạn đang nói về 'Bộ đệm tải 'từ đây http://chromium.googlecode.com/svn/trunk/samples/audio/doc/loading-sounds.html hoặc tại đây https://code.google.com/p/chromium /source/browse/trunk/samples/audio/doc/resources/buffer-loader.js?r=2681? – Ian

+0

API âm thanh web của nó. nó sẽ hoạt động ngay trên hộp trong chrome. – oxxi

+0

Tôi không nghĩ vậy. Như bạn có thể thấy trong các liên kết được cung cấp (và một số khác mà tôi đã tìm thấy), nó là một hàm tùy chỉnh mà bạn phải khai báo và sử dụng. – Ian

Trả lời

12

"class" là chức năng tùy chỉnh được tạo để trừu tượng hóa việc sử dụng API âm thanh trên web. Đây không phải là tính năng tích hợp và phải được đưa vào trang của bạn để được sử dụng; không có gì đặc biệt về Chrome. Dưới đây là một ví dụ về nơi nó được giải thích: http://www.html5rocks.com/en/tutorials/webaudio/intro/#toc-abstract

Để sử dụng, bao gồm mã này trước khi nó được sử dụng:

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); 
} 
+1

ok hoạt động này, cảm ơn sự giúp đỡ. – oxxi