音频文件也是通过 webSocket 传送的 pcm 格式的 arrayBuffer , 毫秒级传送很多的流文件, 每个文件都很小,在播放的时候就会有滋滋的声音
async playNextAudioBuffer() {
if (this.audioQueue.length === 0) {
this.isPlaying = false;
this.playIndex = 0;
this.isStopAudio = true
this.audioStop()
return;
}
this.isPlaying = true;
this.playIndex++;
if(this.playIndex == 1){
this.timeUpdate()
this.haveAiTalkData()
}
let audioBuffer = this.audioQueue.shift();
this.audioBufferSource = this.audioContext.createBufferSource();
this.audioBufferSource.buffer = audioBuffer;
// 连接到扬声器
this.audioBufferSource.connect(this.audioContext.destination);
this.audioBufferSource.onended = () => {
this.playNextAudioBuffer();
};
// 开始播放
this.audioBufferSource.start();
}
这里有一个很长的 audioBuffer 队列, 当一个 audioBuffer 播放完去播下一个的时候, 会有 吱吱 声音, 使用的腾讯云的实时语音合成