gpt4 book ai didi

angular - 数组缓冲区 : I want to play the audio from an arraybuffer using decodeAudioData in angular

转载 作者:行者123 更新时间:2023-12-02 22:37:02 25 4
gpt4 key购买 nike

嗨我的问题是我想解码通过 HTTP 方法接收的数组缓冲区以便在浏览器中播放它我的问题请看一下我将尝试解释更多的代码。

    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
});



}

最佳答案

我猜你调用 playAudio之前 radioService.generateVoiceMethode()有机会得到回复并填写audioArray
稍加重组;

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 方法

关于angular - 数组缓冲区 : I want to play the audio from an arraybuffer using decodeAudioData in angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49536810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com