一、接口设置
// 语音播放
export const getVoicePlay = (content: string) => {return requestVoice({url: '/tts/?text_prompt=' + content,method: 'get',responseType: 'blob', // 返回类型blob});
};
二、数据处理播放
getVoicePlay(item.content).then((res: any) => {console.log(res);const blob = new Blob([res], { type: 'audio/wav' });const localUrl = (window.URL || webkitURL).createObjectURL(blob);const audio = document.createElement('audio');audio.style.display = 'none'; // 防止影响页面布局audio.controls = true;document.body.appendChild(audio);audio.src = localUrl;audio.play();// 语音播放完毕后,需要手动释放内存audio.onended = function () {document.body.removeChild(audio);URL.revokeObjectURL(localUrl);};});
感谢
后端返回二进制流音频数据,怎么让其可播放
前端播放二进制语音流