Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Arraybuffer:我想在Arraybuffer中使用decodeAudioData播放音频_Angular_Audio_Arraybuffer - Fatal编程技术网

Angular Arraybuffer:我想在Arraybuffer中使用decodeAudioData播放音频

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(

嗨,我的问题是我想解码通过HTTP方法接收到的arraybuffer,以便在浏览器中播放我的问题请看一下代码,我将尝试解释更多

    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为空,也不会:(如果你愿意,你可以把它扔进去,我可以帮你解决这个问题:)