2
window.onload = init;
var list = new Array(); //array containing list of music sources
var playListBuffer = new Array(); //array to put in all decoded audio
var playList = new Array();
var context = new webkitAudioContext();
function init(){
list = ["../media/cello_suit.mp3","../media/morning.mp3"]; //list of files to play at once
load(list);
}
function load(url){
for (var i=0; i<list.length; i++){ //load in every url
var request = new XMLHttpRequest();
request.open('GET', list[i], true);
request.responseType = 'arraybuffer';
request.onload = function() { //Async method
console.log(request.response);
context.decodeAudioData(request.response, function(buffer) { //Async method
if (!buffer) {
alert('error decoding file data: ');
return;
}
playListBuffer.push(buffer); //Decode audio and put inside playListBuffer
if (list.length==playListBuffer.length){
console.log(playListBuffer); //When All files have been decoded show an Array in console
prepare();
}
}, function(e) { console.log('Error decoding audio file', e)});
};
request.onerror = function() {
alert('BufferLoader: XHR error');
}
request.send();
}
}
function prepare(){
for (var i=0; i<playListBuffer.length; i++){
var source = context.createBufferSource(); // creates a sound source
console.log(playListBuffer[i]);
source.buffer = playListBuffer[i]; // tell the source which sound to play
source.connect(context.destination); // connect the source to the context's destination (the speakers)
playList.push(source);
}
playAll();
}
function playAll(){
for (var i=0; i<playList.length; i++){
playList[i].noteOn(0);
}
}
안녕하세요; 나는 새로운 webaudio API를 사용하는이 기본 스크립트에 약간의 동작을 가지고있다. 이 스크립트는 크롬에서 오류가 발생합니다 : 잡히지 않은 오류 : SYNTAX_ERR : DOM 예외 12 audio.js : 27 request.onload; 그러나 내 목록에서 두 번째 요소를 제거하면 작동합니다. 왜?여러 개의 파일을 위치 이름 배열에서 한 번에로드 및 재생
배경 정보 :이 스크립트는 노래 위치 배열을 읽고 동시에 재생합니다.