speechSynthesis API示例给出了错误

speechSynthesis API example gives error

本文关键字:错误 API speechSynthesis      更新时间:2023-09-26

关于Web语音API规范的示例

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

在chrome上给出以下错误:

Uncaught TypeError:DOM对象构造函数不能作为作用

这里有人能帮忙吗?

谢谢!

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

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

这里有一些代码和一个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);
};