let audioCtx = new webkitAudioContext();
const urls = ["/foo.mp3"];
function initialize(callback) {
let request = new XMLHttpRequest();
let buffers = [];
for(let i = 0; i < urls.length; i++) {
request.open("GET", urls[i], true);
request.responseType = "arraybuffer";
request.onload = function() {
audioCtx.decodeAudioData(request.response, function(buffer) {
// do something with buffer
});
request.send();
}
if(callback) callback(buffers);
}
This method is effective, but I have a preference for loading synchronously rather than asynchronously.
request.open("GET", urls[i], true)
The last argument determines whether the load is synchronous or asynchronous. However, setting it to false prevents changing the responseType to arraybuffer.
As a result, this causes the method to fail since the response cannot be read as an arraybuffer due to this restriction.
audioCtx.decodeAudioData(request.response, function(buffer){});
I tried searching for solutions, but most resources focus on converting strings into Array Buffers. In my scenario, I need to convert audio files like mp3 formats into arraybuffers while maintaining synchronous requests. Are there any solutions to achieve this?