Created
March 13, 2019 14:12
-
-
Save iamchristough/a875321e08bab814a4408dd022262d3b to your computer and use it in GitHub Desktop.
Buffer load Media files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
var request = new XMLHttpRequest(); | |
request.open('GET', url, true); | |
request.responseType = 'arraybuffer'; | |
var loader = this; | |
request.onload = function() { | |
loader.context.decodeAudioData( | |
request.response, | |
function(buffer) { | |
if (!buffer) { | |
return; | |
} | |
loader.bufferList[index] = buffer; | |
buffer.bufferSize = 2048; | |
if (++loader.loadCount == loader.urlList.length) { | |
loader.onload(loader.bufferList); | |
} | |
}, | |
function(error) { | |
console.error('decodeAudioData error', error); | |
} | |
); | |
} | |
request.onerror = function() { | |
console.error('BufferLoader: XHR error'); | |
} | |
request.send(); | |
}; | |
BufferLoader.prototype.load = function() { | |
for (var i = 0; i < this.urlList.length; i++) { | |
this.loadBuffer(this.urlList[i], i); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment