1

Web Speech API Specification上给出的示例

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

在 chrome 上给出以下错误:

未捕获的 TypeError:DOM 对象构造函数不能作为函数调用。

有人可以在这里帮忙吗?

谢谢!

4

2 回答 2

2

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

speechSynthesis.speak(new SpeechSynthesisUtterance('Hello World'));
于 2013-10-06T21:40:05.687 回答
1

下面是一些代码和一个 jsbin,以帮助演示如何一起使用这些 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);
};
于 2014-04-26T02:57:10.900 回答