JavaScript中是否有语音或语音就绪事件? [英] Is there some voice or speech ready event in JavaScript?

查看:94
本文介绍了JavaScript中是否有语音或语音就绪事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Chrome浏览器上尝试了以下操作.它会打印出0声音,然后再次在计算机上打印出0声音,但是如果我重新加载页面以重新开始,然后将最后一行更改为setTimeout(foo, 1000),则第一行将打印出0,第二行将打印出87.

I tried the following on Google Chrome. It'd print out 0 voices, and again print out 0 voices on my computer, but if I reload the page to start fresh, and change the last line to setTimeout(foo, 1000), then the first line would print out 0 and second line, 87.

该数字甚至可以更改为6633并且可以使用,但是我不确定这是否取决于计算机速度和网络速度(用于获取语音数据.(可能是内部网络)流量,因为在调试器的网络"标签中没有看到任何流量).

The number could even be changed to 66 or 33 and it worked, but I am not sure whether it depends on the computer speed and the network speed (for fetching the voice data. (could it be internal network traffic, as I didn't see any traffic in the debugger's network tab)).

是否有一种更可靠的方法来获得类似于DOMReady事件的ready事件或某种调用回调的方法,从而使其更可靠? (而不是使用setTimeout延迟未知的持续时间并希望达到最佳效果).

Is there a more reliable way to get a ready event similar to a DOMReady event or some way to invoke a callback, so that it is more reliable? (instead of using a setTimeout to delay for an unknown duration and hoping for the best).

function foo() {
  msg = new SpeechSynthesisUtterance(),
    voices = window.speechSynthesis.getVoices();

  console.log("How many voices", voices.length);

  msg.text = "Hello World";
  speechSynthesis.speak(msg);
}

foo();
setTimeout(foo, 0);   // just try again in the next event cycle

推荐答案

The voiceschanged event might be helpful:

SpeechSynthesis.getVoices()方法返回的SpeechSynthesisVoice对象列表更改时(当voiceschanged事件触发时),将触发Web Speech API的voiceschanged事件.

The voiceschanged event of the Web Speech API is fired when the list of SpeechSynthesisVoice objects that would be returned by the SpeechSynthesis.getVoices() method has changed (when the voiceschanged event fires.)

这篇关于JavaScript中是否有语音或语音就绪事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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