2013-08-17 70 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); 
};