Arraybuffer:我想使用angular

时间:2018-03-28 14:09:58

标签: angular audio arraybuffer

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

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



 }

1 个答案:

答案 0 :(得分:0)

我猜你在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方法