lol*_*k44 12 text-to-speech google-translate
我曾经使用此网址致电Google Translate TTS下载音频文件:http://translate.google.com/translate_tts?tl = zh- CN& q = Hello + world!
然而谷歌改变了工作方式,因此我无法再下载音频文件.我已经注册了Google Translate API V2的免费试用版,但无法找到如何获取TTS音频文件.
任何的想法?
我偶然发现了这个线程,并想参考 @Alexandre Andrade 给出我的看法,主要是因为他没有提交任何代码。
我在 React 应用程序中执行了此操作,但相同的过程应该适用于普通 Web 项目。
我确实将元标记添加到我的 head public/index.html 中,
<head>
...
<meta name="referrer" content="no-referrer">
...
</head>
Run Code Online (Sandbox Code Playgroud)
然后在我的组件中添加音频标签:
JavaScript:
const playTTS = (text, lang) => {
// Get the audio element
const audioEl = document.getElementById('tts-audio');
const url= `https://translate.google.com/translate_tts?ie=UTF-8&tl=${lang}&client=tw-ob&q=${text}`;
// add the sound to the audio element
audioEl.src = url;
//For auto playing the sound
audioEl.play();
};
Run Code Online (Sandbox Code Playgroud)
html
...
<audio controls id="tts-audio"/>
...
Run Code Online (Sandbox Code Playgroud)
然后只需将该函数连接到某些生命周期方法即可。由于我在反应挂钩中编写了反应代码,因此我在其中一个挂钩中添加了函数调用,以便在加载组件时对其进行初始化。(否则这将在 componentDidMount() 函数中)。
希望这对任何人都有帮助!
| 归档时间: |
|
| 查看次数: |
16450 次 |
| 最近记录: |