开始在我的Android和Safari设备上运行SpeechSynthesis API [英] Start running SpeechSynthesis API on my Android and Safari devices

查看:326
本文介绍了开始在我的Android和Safari设备上运行SpeechSynthesis API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SpeechSynthesis API创建一个Web应用程序,在单击开始按钮后运行我的程序,然后开始在我的Android和iOS设备上收听用户。用户可以说任何话来运行程序。之后,我可以每三秒钟播放一次音频文件。下面是我的代码到目前为止。我的逻辑错了吗?点击后我无法启动我的程序并听到任何声音。

I'm trying to make a web app with the SpeechSynthesis API to run my program after one click on start button and then start listening to the user on my Android and iOS devices. The user could speak anything to run the program. After that, I can play my audio files every three seconds. Below is my code so far. Is my logic wrong? I can't start my program after the click and hear any sound.

另一个问题是这个SpeechSynthesis API可以支持Android和iOS设备,但是当我看到一些事件时作为'soundstart event',它不支持Safari Mobile。他们的关系是什么?我真的很困惑。 SpeechRecognition API仅支持Chrome浏览器,但我不需要像soundstart那样使用某些事件吗?

Another question is this SpeechSynthesis API could support Android and iOS devices, but when I saw some event such as 'soundstart event', it doesn't support Safari Mobile. What are their relationships? I got really confused. And the SpeechRecognition API only supports for Chrome browser but don't I need to user some event like soundstart?

提前感谢您的帮助。我真的很感激。

Thank you for help in advance. I really appreciate it.

 <p id="msg" align="center"></p>

    <script>
        var utterance = new SpeechSynthesisUtterance("Hello");
        //window.speechSynthesis.speak(utterance);

        var supportMsg = document.getElementById('msg');

        if ('speechSynthesis' in window)
        {
            supportMsg.innerHTML = 'Your browser <strong>supports</strong> speech synthesis.';
            console.log("Hi");

            utterance.onstart = function(event) 
            {
                console.log('Hhhh')
            };


            var playList = ["1_hello", "2_how_old", "3_what_did_you_make"];
            var dir = "sound/";
            var extention = ".wav";
            audio.src = dir + playList[audioIndex] + extention;
            audio.load();

            var audioIndex = 0;
            setTimeout(function(){audio.play();}, 1000);


            window.speechSynthesis.speak(utterance);

        } 
        else 
        {



            supportMsg.innerHTML = 'Sorry your browser <strong>does not support</strong> speech synthesis.<br>Try this in <a href="https://www.google.co.uk/intl/en/chrome/browser/canary.html">Chrome Canary</a>.';
        }

        //window.speechSynthesis(utterance);

    </script>
    <div class="container">
        <button id="runProgram" onclick='utterance.onstart();' 
        class="runProgram-button">Start</button>
    </div>


推荐答案

这对你有用吗?

function playAudio() {
  var msg = new SpeechSynthesisUtterance('Help me with this code please?');
  msg.pitch = 0;
  msg.rate = .6;
  window.speechSynthesis.speak(msg);



  var msg = new SpeechSynthesisUtterance();
  var voices = window.speechSynthesis.getVoices();
  msg.voice = voices[10]; // Note: some voices don't support altering params
  msg.voiceURI = 'native';
  msg.volume = 1; // 0 to 1
  msg.rate = 1.2; // 0.1 to 10
  msg.pitch = 2; //0 to 2
  msg.text = 'Sure. This code plays "Hello World" for you';
  msg.lang = 'en-US';

  msg.onend = function(e) {
    var msg1 = new SpeechSynthesisUtterance('Now code plays audios for you');
    msg1.voice = speechSynthesis.getVoices().filter(function(voice) { return voice.name == 'Whisper'; })[0];
   msg1.pitch = 2; //0 to 2
   msg1.rate= 1.2; //0 to 2
    // start your audio loop.
    speechSynthesis.speak(msg1);
    console.log('Finished in ' + e.elapsedTime + ' seconds.');
  };

  speechSynthesis.speak(msg);
}

<div class="container">
  <button id="runProgram" onclick='playAudio();' class="runProgram-button">Start</button>
</div>

这篇关于开始在我的Android和Safari设备上运行SpeechSynthesis API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆