使用 audioContext.decodeAudioData 时 Safari 浏览器解码失败

Decoding failed for Safari browser while using audioContext.decodeAudioData

提问人:Rutvik D Mehta 提问时间:10/22/2023 更新时间:10/22/2023 访问量:37

问:

我有一个 Vue JS 应用程序,我在其中播放来自 Websocket 连接的音频流。音频流以块的形式连续传入,因此我实现了一个逻辑来播放队列中的传入块。在Chrome浏览器中一切正常,但不幸的是,它在Safari中中断了。我对此做了很多研究,但我无法找到解决这个问题的任何方法。

这是我的代码

vm.socket = new WebSocket('wss://my_website.com');

//required to receive data in array buffer type. If not mentioned I get Blob's
vm.socket.binaryType = 'arraybuffer'


vm.socket.addEventListener("message", (event) => {


    const audioData = new Uint8Array(event.data);

    this.audioQueue.push(audioData);
    if (!this.isPlaying) {
        this.playNextChunk();
    }

});

//Main method which is responsible to process incoming audio chunks
async playNextChunk() {

    if (this.audioQueue.length === 0) {
        this.isPlaying = false;
        return;
    }

    this.isPlaying = true;
    const audioData = this.audioQueue.shift();

    try {
        //decode audio data (This fails randomly in between in Safari browser)
        const audioBuffer = this.audioContext.decodeAudioData(audioData.buffer);
        const source = this.audioContext.createBufferSource();
        source.buffer = audioBuffer;
        source.connect(this.audioContext.destination);
        source.onended = this.playNextChunk;
        source.start();
    } catch (error) {
        console.error('Error decoding audio data', error);
    }

}


下面是控制台日志的屏幕截图enter image description here

websocket safari vuejs3 音频流 web-audio-api

评论


答: 暂无答案