Is there some voice or speech ready event in JavaScript? [duplicate]

寵の児 提交于 2020-01-24 10:13:41

问题


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.

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)).

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

回答1:


The voiceschanged event might be helpful:

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.)



来源:https://stackoverflow.com/questions/59778304/is-there-some-voice-or-speech-ready-event-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!