Javascript 未定义未捕获引用错误缓冲加载程序
试图学习音频API,但我得到了BufferLoader类的一个未捕获的引用错误。我用的是chrome,而且是最新的。这个班不是应该毫无问题地工作吗Javascript 未定义未捕获引用错误缓冲加载程序,javascript,html,html5-audio,web-audio-api,Javascript,Html,Html5 Audio,Web Audio Api,试图学习音频API,但我得到了BufferLoader类的一个未捕获的引用错误。我用的是chrome,而且是最新的。这个班不是应该毫无问题地工作吗 <html> <head> <script type=text/javascript> window.onload = init; var context; var bufferLoader; function init(){ context = new webkitAudioCon
<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>
window.onload=init;
var语境;
var缓冲加载程序;
函数init(){
context=新的webkitAudioContext();
bufferLoader=新的bufferLoader(
上下文
[
' https://dl.dropboxusercontent.com/u/1957768/kdFFO3.wav',
' https://dl.dropboxusercontent.com/u/1957768/geniuse%20meodies.wav',
],
完成装载
);
load();
}
函数完成加载(bufferList){
//制作两个源并播放它们
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);
}
缓冲加载程序的“类”是一个自定义函数,用于抽象Web音频API的使用。它不是内置功能,必须包含在页面中才能使用;Chrome拥有这个没有什么特别的。下面是一个解释的示例:
若要使用,请在使用前包含此代码:
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);
}
函数缓冲加载程序(上下文、URL列表、回调){
this.context=上下文;
this.urlist=urlist;
this.onload=回调;
this.bufferList=新数组();
此参数为0.loadCount;
}
BufferLoader.prototype.loadBuffer=函数(url,索引){
//异步加载缓冲区
var request=new XMLHttpRequest();
打开(“获取”,url,true);
request.responseType=“arraybuffer”;
var loader=this;
request.onload=函数(){
//异步解码request.response中的音频文件数据
loader.context.decodeAudioData(
请求.答复,
功能(缓冲区){
如果(!缓冲区){
警报('解码文件数据时出错:'+url);
返回;
}
loader.bufferList[索引]=缓冲区;
if(++loader.loadCount==loader.urlist.length)
loader.onload(loader.bufferList);
},
函数(错误){
console.error('解码音频数据错误',error);
}
);
}
request.onerror=函数(){
警报('BufferLoader:XHR错误');
}
request.send();
}
BufferLoader.prototype.load=函数(){
对于(var i=0;i
此缓冲加载程序的文档在哪里?您是在这里还是在这里谈论BufferLoader
?它是Web音频API。它应该在chrome中开箱即用,我不这么认为。正如您在提供的链接(以及我发现的其他几个链接)中所看到的,这是一个您必须声明和使用的自定义函数。