OLD | NEW |
1 function BufferLoader(context, urlList, callback) { | 1 function BufferLoader(context, urlList, callback) { |
2 this.context = context; | 2 this.context = context; |
3 this.urlList = urlList; | 3 this.urlList = urlList; |
4 this.onload = callback; | 4 this.onload = callback; |
5 this.bufferList = new Array(); | 5 this.bufferList = new Array(); |
6 this.loadCount = 0; | 6 this.loadCount = 0; |
7 } | 7 } |
8 | 8 |
9 BufferLoader.prototype.loadBuffer = function(url, index) { | 9 BufferLoader.prototype.loadBuffer = function(url, index) { |
10 // Load buffer asynchronously | 10 // Load buffer asynchronously |
11 var request = new XMLHttpRequest(); | 11 var request = new XMLHttpRequest(); |
12 request.open("GET", url, true); | 12 request.open("GET", url, true); |
13 request.responseType = "arraybuffer"; | 13 request.responseType = "arraybuffer"; |
14 | 14 |
15 var loader = this; | 15 var loader = this; |
16 | 16 |
17 request.onload = function() { | 17 request.onload = function() { |
18 var buffer; | 18 loader.context.decodeAudioData( |
19 try { | 19 request.response, |
20 buffer = loader.context.createBuffer(request.response, false); | 20 function (decodedAudio) { |
21 } catch(e) { | 21 try { |
22 alert('error decoding file data: ' + url); | 22 loader.bufferList[index] = decodedAudio; |
23 } | 23 if (++loader.loadCount == loader.urlList.length) |
24 | 24 loader.onload(loader.bufferList); |
25 try { | 25 } catch(e) { |
26 loader.bufferList[index] = buffer; | 26 alert('BufferLoader: unable to load buffer' + index); |
27 if (++loader.loadCount == loader.urlList.length) | 27 } |
28 loader.onload(loader.bufferList); | 28 }, |
29 } catch(e) { | 29 function () { |
30 alert('BufferLoader: callback problem'); | 30 alert('error decoding file data: ' + url); |
31 } | 31 }); |
32 } | 32 } |
33 | 33 |
34 request.onerror = function() { | 34 request.onerror = function() { |
35 alert('BufferLoader: XHR error'); | 35 alert('BufferLoader: XHR error'); |
36 } | 36 } |
37 | 37 |
38 request.send(); | 38 request.send(); |
39 } | 39 } |
40 | 40 |
41 BufferLoader.prototype.load = function() { | 41 BufferLoader.prototype.load = function() { |
42 for (var i = 0; i < this.urlList.length; ++i) | 42 for (var i = 0; i < this.urlList.length; ++i) |
43 this.loadBuffer(this.urlList[i], i); | 43 this.loadBuffer(this.urlList[i], i); |
44 } | 44 } |
OLD | NEW |