问题描述
我编写此代码是为了在单击按钮时播放音频,但我无法使其工作。 谷歌文档在处理回复方面帮不上忙。 有谁能帮帮忙吗?
async function playVoiceover() { const url ='https://texttospeech.googleapis.com/v1/text:synthesize?key=XXXXXXX' const rawResponse = await fetch(url, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ 'input':{ 'ssml':`<speak>The <say-as interpret-as="characters">S**L</say-as> standard <break time="1s"/>is defined by the <sub alias="World Wide Web Consortium">W3C</sub>.</speak>` }, 'voice':{ 'languageCode':'en-us', 'name':'en-US-Standard-B', 'ssmlGender':'MALE' }, 'audioConfig':{ 'audioEncoding':'MP3' } }) }); const content = await rawResponse.json(); const mp3 = 'data:audio/mp3;base64,'+window.atob(content.audioContent) const audio = new Audio(mp3); audio.play(); }
推荐答案
我将此答案作为有关此案例的信息留给社区:
window.atob()
方法的作用是:对base-64编码的字符串进行解码。
texttospeech.googleapis.com(响应结构)
{";audioContent&Quot;:字符串} //Base64编码的字符串。
在使用编解码器结构格式化mp3字符串时,不需要对其进行解码。您可以看到类似的回复here。
有关exttospeech接口响应的其他详细信息:
- text-to-speech -> synthesize -> response-body