在Javascript中使用Google文本转语音

Bet*_*moo 46 javascript google-text-to-speech

我需要在JavaScript中使用Google文本转语音.
想法是使用Web服务:

http://translate.google.com/translate_tts?tl=en&q=This%20is%20just%20a%20test

并在certian动作上播放,例如点击按钮.

但似乎它不像加载普通的wav/mp3文件:

<audio id="audiotag1" src="audio/example.wav" preload="auto"></audio>

<script type="text/javascript">
    function play() {
        document.getElementById('audiotag1').play();
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Bri*_*unt 119

另一种选择现在可能是HTML5文本到语音,这是在Chrome 33+和许多其他.

这是一个示例:

var msg = new SpeechSynthesisUtterance('Hello World');
window.speechSynthesis.speak(msg);
Run Code Online (Sandbox Code Playgroud)

有了这个,也许您根本不需要使用Web服务.


Bet*_*moo 22

这是我找到的代码片段:

var audio = new Audio();
audio.src ='http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=Hello%20World.';
audio.play();
Run Code Online (Sandbox Code Playgroud)

  • 截至今天,这似乎不再起作用了; 我得到302然后是403. (14认同)
  • Google似乎会禁止在HTTP标头中使用Referrer的请求.有没有办法绕过这个问题? (7认同)
  • 请注意,Google翻译限制为~100个字母. (5认同)
  • 似乎如果你在结尾删除'点'它工作正常,否则它不播放声音. (3认同)

小智 13

您可以将SpeechSynthesisUtterance结合使用,例如say

function say(m) {
  var msg = new SpeechSynthesisUtterance();
  var voices = window.speechSynthesis.getVoices();
  msg.voice = voices[10];
  msg.voiceURI = "native";
  msg.volume = 1;
  msg.rate = 1;
  msg.pitch = 0.8;
  msg.text = m;
  msg.lang = 'en-US';
  speechSynthesis.speak(msg);
}
Run Code Online (Sandbox Code Playgroud)

然后,您只需要say(msg)在使用它时打电话。

更新:查看Google的开发人员博客,该博客与语音驱动的Web应用有关Web Speech API的介绍。


小智 13

响应式语音非常容易。只需包括js和瞧!

<script src='https://code.responsivevoice.org/responsivevoice.js'></script>

<input onclick="responsiveVoice.speak('This is the text you want to speak');" type='button' value=' Play' />
Run Code Online (Sandbox Code Playgroud)

  • 重要提示:虽然这会产生有趣的直接结果,但它是通过“https://code.responsivevoice.org/getvoice.php”使用他们的 API 来生成语音并将其作为渲染音频发送回来。然而,对于非商业项目来说,他们的服务并不是免费的。 (3认同)

小智 8

我不知道 Google 语音,但是使用 javaScript 语音 SpeechSynthesisUtterance,您可以向您引用的元素添加单击事件。例如:

const listenBtn = document.getElementById('myvoice');

listenBtn.addEventListener('click', (e) => {
  e.preventDefault();

  const msg = new SpeechSynthesisUtterance(
    "Hello, hope my code is helpful"
  );
  window.speechSynthesis.speak(msg);

});
Run Code Online (Sandbox Code Playgroud)
<button type="button" id='myvoice'>Listen to me</button>
Run Code Online (Sandbox Code Playgroud)


jkr*_*jkr 8

下面的 JavaScript 代码将要朗读/转换为 mp3 音频的“文本”发送到谷歌云文本转语音 API,并获取 mp3 音频内容作为响应。

 var text-to-speech = function(state) {
    const url = 'https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=GOOGLE_API_KEY'
    const data = {
      'input':{
         'text':'Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.'
      },
      'voice':{
         'languageCode':'en-gb',
         'name':'en-GB-Standard-A',
         'ssmlGender':'FEMALE'
      },
      'audioConfig':{
      'audioEncoding':'MP3'
      }
     };
     const otherparam={
        headers:{
           "content-type":"application/json; charset=UTF-8"
        },
        body:JSON.stringify(data),
        method:"POST"
     };
    fetch(url,otherparam)
    .then(data=>{return data.json()})
    .then(res=>{console.log(res.audioContent); })
    .catch(error=>{console.log(error);state.onError(error)})
  };
Run Code Online (Sandbox Code Playgroud)

  • 这是唯一准确回答问题的答案 (2认同)