speechSynthesis API示例给出了错误

时间:2013-08-17 19:37:42

标签: javascript html api speech-synthesis

Web Speech API Specification

上给出的示例
    speechSynthesis.speak(SpeechSynthesisUtterance('Hello World'));

在chrome上给出以下错误:

  

未捕获的TypeError:无法将DOM对象构造函数调用为   功能

有人可以帮忙吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

我认为规范中有一种类型,您应该将new关键字与SpeechSynthesisUtterance对象一起使用。试试这个:

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

答案 1 :(得分:1)

以下是一些代码和a jsbin as well,以帮助演示如何一起使用API​​:

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);
};