javascript - How to convert audio into an arrayBuffer using synchronous ajax request? -


var context = new webkitaudiocontext(); var url = [ "/foo.mp3"]; function init(callback) {   var req = new xmlhttprequest();   var audiobuffers = [];   for(var = 0; < url.length; i++) {     req.open("get", url[i], true);     req.responsetype = "arraybuffer";      req.onload = function() {       context.decodeaudiodata(req.response, function(buffer) {          //blah blah push buffer array       });     req.send();     }      if(callback) callback(audiobuffers);  } 

this works fine, don't me wrong. thing is, want load synchronously instead of asynchronously.

req.open("get", url[i], true)  

the last argument declare synchronous or not. reason, when make false, unable change responsetype arraybuffer.

this breaks method since not arraybuffer , cannot read response. , surprisingly natural behavior.

context.decodeaudiodata(req.response, function(buffer){});  

i did reasearch on this, found methods convert strings array buffer. for case, converting audio, mp3 format, arraybuffer while keeping request synchronous. there workarounds this?

from standard side. there's rumor (possibily true though) synchronous request going abandaned in future spec. new features not surposed support it. check this message.

the xmlhttprequest2 spec changed prohibit sending synchronous request when xhr.responsetype set.

here's the spec:

if async false, there associated xmlhttprequest document , either timeout attribute value not zero, withcredentials attribute value true, or responsetype attribute value not empty string, throw "invalidaccesserror" exception , terminate these steps.

from application side. using arraybuffer means resource request somehow large binary. while synchronous request block ui, that's bad user experience. if can so, it's not idea either.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -