Http, how do you deal with non text response in angular 2? -
i'm trying array buffer http request explicitely said array buffer. didn't manage , doc on subject rather scarce.
let headers = new headers({'content-type': "audio/mpeg"}); let options = new requestoptions({responsetype: responsecontenttype.arraybuffer, headers: headers }); this.http.get("http://localhost:4200/piano/a4.mp3") .subscribe((response)=> this.play(response));
but can't manage array buffer out of response. response body in console inintelligible assume must correct format. content type of response indeed "audio/mpeg".
edit : here working code future readers
play(arrbf) { this.audiocontext.decodeaudiodata(arrbf, (buffer) => { let source = this.audiocontext.createbuffersource(); source.buffer = buffer; source.connect(this.audiocontext.destination); source.start(0); }); } loadsounds(){ let options = new requestoptions({responsetype: responsecontenttype.arraybuffer}); this.http.get("http://localhost:4200/assets/piano/a4.mp3", options) .map(r => r.arraybuffer()) .subscribe((d)=> { this.play(d)}); }
and define audio context in constructor or smtg :
constructor(private http:http) { let audiocontext_ = audiocontext || webkitaudiocontext; this.audiocontext = new audiocontext_(); }
to blob
response
object in angular2 can use arraybuffer()
method:
let blob: blob = new blob([response.arraybuffer()], {type: 'audio/mpeg'});
documentation angular not updated yet though
Comments
Post a Comment