I'm trying to slim down some repetitive code that worked before by using a for loop to iterate through the process for a list of objects.
var context = new (window.AudioContext || window.webkitAudioContext)();
//Set up the objects for each part
var solo = { audio: context.createBufferSource(),
audioData: context.createBufferSource(),
file: "mp3s/Faith_solo.mp3",
request: new XMLHttpRequest(),
isPlaying: false,
startTime: null,
seekAsOfLastPause: 0,
duration: 0
}
var t1 = { audio: context.createBufferSource(),
audioData: context.createBufferSource(),
file: "mp3s/Faith_t1.mp3",
request: new XMLHttpRequest(),
isPlaying: false,
startTime: null,
seekAsOfLastPause: 0,
duration: 0
}
var t2 = { audio: context.createBufferSource(),
audioData: context.createBufferSource(),
file: "mp3s/Faith_t2.mp3",
request: new XMLHttpRequest(),
isPlaying: false,
startTime: null,
seekAsOfLastPause: 0,
duration: 0
}
var baritone = {
audio: context.createBufferSource(),
audioData: context.createBufferSource(),
file: "mp3s/Faith_baritone.mp3",
request: new XMLHttpRequest(),
isPlaying: false,
startTime: null,
seekAsOfLastPause: 0,
duration: 0
}
var bass = { audio: context.createBufferSource(),
audioData: context.createBufferSource(),
file: "mp3s/Faith_bass.mp3",
request: new XMLHttpRequest(),
isPlaying: false,
startTime: null,
seekAsOfLastPause: 0,
duration: 0
}
var allParts = [solo, t1, t2, baritone, bass];
/* create requests, send requests, load files */
for(i=0;i<allParts.length;i++){
allParts[i].request.open("GET", allParts[i].file, true);
allParts[i].request.responseType = "arraybuffer";
allParts[i].request.onload = function(){
**context.decodeAudioData(allParts[i].request.response, onDecoded);**
}
function onDecoded(buffer){
allParts[i].audioData.buffer = buffer;
}
allParts[i].request.send();
}
I'm getting five copies of the error: "Uncaught TypeError: Cannot read property 'request' of undefined at XMLHttpRequest.allParts.(anonymous function).request.onload" on the line surrounded by asterisks where I set up the context.decodeAudioData parameters.
I'm not quite sure why this is. I've been told this is a closure problem, but I've been unable to find a way to fix it, and I don't quite understand why it would be a closure problem. The XMLHttpRequest is created before that code runs, and it's outside of the scope of the anonymous function, so it should be accessible there.
Thanks for any help!