Angular Arraybuffer:我想在Arraybuffer中使用decodeAudioData播放音频
嗨,我的问题是我想解码通过HTTP方法接收到的arraybuffer,以便在浏览器中播放我的问题请看一下代码,我将尝试解释更多Angular Arraybuffer:我想在Arraybuffer中使用decodeAudioData播放音频,angular,audio,arraybuffer,Angular,Audio,Arraybuffer,嗨,我的问题是我想解码通过HTTP方法接收到的arraybuffer,以便在浏览器中播放我的问题请看一下代码,我将尝试解释更多 ngOnInit() { // this service returns the arraybuffer and I successfully get it . this.radioService.generateVoiceMethode().subscribe(res => { console.log(
ngOnInit() {
// this service returns the arraybuffer and I successfully get it .
this.radioService.generateVoiceMethode().subscribe(res => {
console.log(res); // display the array in the console
this.audioArray = new ArrayBuffer(res.byteLength);
this.audioArray = res;
console.log(this.audioArray); // display in the console with scucess
// my problem is the data in this audioArray no longer exists when I try to
// decode it in another method
});
}
playAudio() {
console.log(this.audioArray); // this display empty arraybuffer !!
this.context.decodeAudioData(this.audioArray).then((buffer)=>{
this.buf = buffer;
this.play();
}).catch((error)=>{
console.log(error); /// error is : DOMException: Unable to decode audio data
});
}
我猜您是在
radioService.generateVoiceMethode()
有机会获得响应并填充audioArray之前调用playAudio
稍加调整,
ngOnInit() {
this.radioService.generateVoiceMethode().subscribe(res => {
console.log(res); // display the array in the console
this.audioArray = new ArrayBuffer(res.byteLength);
this.audioArray = res;
console.log(this.audioArray);
this.playAudio(this.audioArray);
});
}
playAudio(audioArray) {
console.log(audioArray); // this display empty arraybuffer !!
this.context.decodeAudioData(audioArray).then((buffer)=>{
this.buf = buffer;
this.play();
}).catch((error)=>{
console.log(error); /// error is : DOMException: Unable to decode audio data
});
}
这确保您在设置数据后调用playAudio()
方法
或者,您也可以选择使用可观察模式启动playAudio方法在设置audioArray后是否调用playAudio()
?是的,当我单击按钮时播放音频。感谢您的回答,但是,如果我尝试您的解决方案,即使这次res为空,也不会:(如果你愿意,你可以把它扔进去,我可以帮你解决这个问题:)