2013-08-17 73 views

回答

0

我认为在规范中有一种类型,并且您需要将new关键字与SpeechSynthesisUtterance对象一起使用。试试这个:

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

此外,要记住必须由用户事件(例如按下按钮)来触发。 – brindy

1

下面是一些代码和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); 
};