speechSynthesis API example gives error

浪尽此生 提交于 2019-12-24 13:19:11

问题


The example given on Web Speech API Specification

    speechSynthesis.speak(SpeechSynthesisUtterance('Hello World'));

gives the following error on chrome:

Uncaught TypeError: DOM object constructor cannot be called as a function.

Can anyone help here?

Thanks!


回答1:


I think there is a type in the specifications and you are expected to use the new keyword with the SpeechSynthesisUtterance object. Try this:

speechSynthesis.speak(new SpeechSynthesisUtterance('Hello World'));



回答2:


Here's some code and a jsbin as well to help demonstrate how to use the APIs together:

var utterance = new window.SpeechSynthesisUtterance();
utterance.lang = 'ja-JP'; //translates on the fly - soooo awesome (japanese is the funniest)
utterance.volume = 1.0;
utterance.rate = 1.0;
utterance.pitch = 1.0;
utterance.voice = 'Hysterical'; // this seems to do nothing
utterance.text = "Facebook news feeds are full of garbage";

//Speak the phrase
window.speechSynthesis.speak(utterance);

window.speechSynthesis.onvoiceschanged = function () {
  var speechSynthesisVoices = speechSynthesis.getVoices();
  var accents = _(speechSynthesisVoices).pluck('lang');
  var voices = _(speechSynthesisVoices).pluck('voiceURI');
  var names = _(speechSynthesisVoices).pluck('name');
  console.log('names', names);
  console.log('accents', _.uniq(accents));
  console.log('voices', voices);
};


来源:https://stackoverflow.com/questions/18292625/speechsynthesis-api-example-gives-error

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