上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