gpt4 book ai didi

javascript - 在Javascript中播放Azure输出格式 "audio-16khz-128kbitrate-mono-mp3"的方法

转载 作者:行者123 更新时间:2023-12-03 01:16:20 25 4
gpt4 key购买 nike

我正在调用 Azure TTS REST API,使用 header X-Microsoft-OutputFormat 和值 audio-24khz-160kbitrate-mono-mp3,但我不知道如何转换和播放响应中的音频。有谁知道如何在调用Azure认知服务rest API时播放音频响应?

谢谢。

我尝试使用 blob 进行转换

`

 let wavFile = new Blob(res.data, { 
'type': 'audio/mp3'
});

`但没有成功。

最佳答案

请使用 fetch 并确保至少包含以下 header 和负载:

const audio = document.createElement("audio");

fetch("{YourEndpointUrl}", {
"headers": {
"content-type": "application/ssml+xml",
"ocp-apim-subscription-key": "{YourSpeechKey}",
"x-microsoft-outputformat": "audio-24khz-160kbitrate-mono-mp3"
},
"body": "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:mstts=\"http://www.w3.org/2001/mstts\" xml:lang=\"en-US\"><voice name=\"AdaptVoice\">My SSML</voice></speak>",
"method": "POST"
})
.then(resp => resp.blob())
.then(URL.createObjectURL)
.then(url => {
audio.src = url;
audio.play();
});

或者,使用 async/await 更简洁:

const audio = document.createElement("audio");

const resp = await fetch("{YourEndpointUrl}", {
"headers": {
"content-type": "application/ssml+xml",
"ocp-apim-subscription-key": "{YourSpeechKey}",
"x-microsoft-outputformat": "audio-24khz-160kbitrate-mono-mp3"
},
"body": "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:mstts=\"http://www.w3.org/2001/mstts\" xml:lang=\"en-US\"><voice name=\"AdaptVoice\">My SSML</voice></speak>",
"method": "POST"
});
const blob = await resp.blob();
const url = await URL.createObjectURL(blob);
audio.src = url;
audio.play();

关于javascript - 在Javascript中播放Azure输出格式 "audio-16khz-128kbitrate-mono-mp3"的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74777550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com