【问题标题】:Using Speech Synthesis to speak array of strings with pauses between使用语音合成来说出字符串数组之间的停顿
【发布时间】:2016-09-10 22:09:54
【问题描述】:

我正在尝试找出一种可靠的方法将字符串数组传递给语音合成 API,并在说出每个数组项之间暂停。例如。说第 1 项,暂停 x 秒,说第 2 项,等等。

我尝试使用 API 的 onend 方法/事件,但它只工作了几次,然后就完全停止工作,从那时起背靠背读取其余的数组项。

建议?

var dropdown = $('#item-select'),
  interval = $('#item-interval').val() * 1000,
  itemBtn = $('#item-btn'),
  stopBtn = $('#item-stop'),
  items = {
    'first': ['hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine'],
    'second': ['hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world']
  };

if ('speechSynthesis' in window) {
  function speak(text) {
    var msg = new SpeechSynthesisUtterance();
    msg.text = text;

    speechSynthesis.speak(msg);
    
    msg.addEventListener('end', function(e) {
      speechSynthesis.pause();
      window.setTimeout(function() {
        speechSynthesis.resume();
      }, interval);
    });
  }
  
  itemBtn.on('click', function(evt) {
    currItem = items[dropdown.val()];
    for (var phrase in currItem) {
      speak(currItem[phrase]);
    }
  });
  stopBtn.on('click', function(evt) {
    speechSynthesis.cancel();
  });
} else {
  console.log('Voice synthesis isn\'t supported in your browser.');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id='item-select'>
  <option selected value='first'>First Item</option>
  <option value='second'>Second Item</option>
</select>
<label for='item-interval'>Interval (seconds)</label>
<input id='item-interval' max='10' name='item-interval' type='range' value='2'>
<button id='item-btn'>Speak!</button>
<button id='item-stop'>CEASE FIRE</button>

【问题讨论】:

    标签: javascript text-to-speech speech-synthesis


    【解决方案1】:

    通过项目数组进行超时延迟的递归旅行怎么样:

    function speak(list) {
        if (list.length) {
          var msg = new SpeechSynthesisUtterance();
          msg.text = list[0];
    
          speechSynthesis.speak(msg);
    
          msg.addEventListener('end', function(e) {
            window.setTimeout(() => {
                speak(list.slice(1));
            }, interval);
          });
        }
      }
    
      itemBtn.on('click', function(evt) {
        const list = items[dropdown.val()];  
        speak(list);
      });
    

    它对我有用,但后来我遇到了一些奇怪的事情。 有时 SpeechSynthesis 只是停止触发“结束”事件:

    这个小提琴添加了一些日志来显示话语何时开始/结束;观察“结束”事件是否只是无缘无故停止

    https://jsfiddle.net/cak4bju9/2/

    这篇关于错误行为的堆栈帖子很有用:

    SpeechSynthesis API onend callback not working

    【讨论】:

    • 有趣的想法 - 我一定会试一试,看看它是否有帮助!同时,为了修复未触发的结束事件,您链接的帖子中的这个答案对我最有帮助:stackoverflow.com/a/35935851/1505423。感谢您抽出宝贵时间对此进行调查!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多