我试图从http请求中得到一个数组缓冲区,这个请求明确地说是一个数组缓冲区。然而,我没有设法做到这一点,关于这个问题的文件是相当稀缺的。Http,你如何处理角度2中的非文本响应?
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));
但我无法设法从响应中获取数组缓冲区。控制台中的响应主体无法识别,因此我认为它必须是正确的格式。此外,响应的内容类型确实是“音频/ mpeg”。
编辑:这里是未来的读者
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)});
}
,并定义构造函数或smtg音频方面的一些工作代码:
constructor(private http:Http) {
let AudioContext_ = AudioContext || webkitAudioContext;
this.audioContext = new AudioContext_();
}
你尝试没有括号'新的Blob(response.blob(),{type:'audio/mpeg'})'? – PierreDuc
但是如果你做了response.blob(),你为什么要把它放在一个新的Blob调用中?也许你可以使用'response.arrayBuffer()'来启动blob – PierreDuc
让我把它制作成答案:) – PierreDuc