上Web Speech API SpecificationspeechSynthesis API的例子给出了错误
speechSynthesis.speak(SpeechSynthesisUtterance('Hello World'));
给出的例子给出了镀铬以下错误:
Uncaught TypeError: DOM object constructor cannot be called as a function.
谁能帮助吗?
谢谢!
上Web Speech API SpecificationspeechSynthesis API的例子给出了错误
speechSynthesis.speak(SpeechSynthesisUtterance('Hello World'));
给出的例子给出了镀铬以下错误:
Uncaught TypeError: DOM object constructor cannot be called as a function.
谁能帮助吗?
谢谢!
我认为在规范中有一种类型,并且您需要将new
关键字与SpeechSynthesisUtterance
对象一起使用。试试这个:
speechSynthesis.speak(new SpeechSynthesisUtterance('Hello World'));
下面是一些代码和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);
};
此外,要记住必须由用户事件(例如按下按钮)来触发。 – brindy