问题
The example given on Web Speech API Specification
speechSynthesis.speak(SpeechSynthesisUtterance('Hello World'));
gives the following error on chrome:
Uncaught TypeError: DOM object constructor cannot be called as a function.
Can anyone help here?
Thanks!
回答1:
I think there is a type in the specifications and you are expected to use the new
keyword with the SpeechSynthesisUtterance
object. Try this:
speechSynthesis.speak(new SpeechSynthesisUtterance('Hello World'));
回答2:
Here's some code and a jsbin as well to help demonstrate how to use the APIs together:
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);
};
来源:https://stackoverflow.com/questions/18292625/speechsynthesis-api-example-gives-error