2017-10-21 148 views
3

Chromium支持語音合成API嗎?我需要安裝聲音嗎?如果是這樣,我該怎麼做?我使用的是Fedora。是否需要爲視頻安裝額外的軟件包才能工作?Chromium Fedora中的speechSynthesis.getVoices()是空數組

我已經嘗試此代碼:

var msg = new SpeechSynthesisUtterance('I see dead people!'); 
msg.voice = speechSynthesis.getVoices().filter(function(voice) { 
    return voice.name == 'Whisper'; 
})[0]; 
speechSynthesis.speak(msg); 

從文章Web apps that talk - Introduction to the Speech Synthesis API

但功能speechSynthesis.getVoices()返回空數組。

我也試過:

window.speechSynthesis.onvoiceschanged = function() { 
    console.log(window.speechSynthesis.getVoices()) 
}; 

功能得到執行,但數組也是空的。

https://fedoraproject.org/wiki/Chromium有信息使用--enable-speech-dispatcher標誌,但是當我使用它時,我有警告說不支持標誌。

回答

1

Chromium支持語音合成API嗎?

是的,Web Speech API具有在Chromium瀏覽器的基本支持,但也有既鉻和Firefox實施規範的幾個問題,看的見Blink>SpeechInternals>SpeechSynthesisWeb Speech

我需要安裝聲音嗎?如果是這樣,我該怎麼做?我正在使用 Fedora。是聲音像視頻,我需要安裝額外的包 它的工作?

是的,需要安裝聲音。鉻未附帶聲音默認設置爲SpeechSynthesisUtterancevoice屬性,請參閱How to use Web Speech API at chromium?; How to capture generated audio from window.speechSynthesis.speak() call?

您可以安裝speech-dispatcher作爲系統語音合成服務器的服務器和espeak作爲語音合成器。

$ yum install speech-dispatcher espeak 

您也可以在用戶的​​主文件夾設置配置文件speech-dispatcher設置兩個speech-dispatcher特定選項和輸出模塊,您的使用,例如espeak

$ spd-conf -u 

啓動鉻與--enable-speech-dispatcher標誌會自動生成到speech-dispatcher的連接,您可以在05之間設置LogLevel以檢查Chromium代碼和speech-dispatcher之間的SSIP通信。

.getVoices()返回異步結果,需要被調用兩次

看到這個electron問題在GitHub上Speech Synthesis: No Voices #586

window.speechSynthesis.onvoiceschanged = e => { 
    const voices = window.speechSynthesis.getVoices(); 
    // do speech synthesis stuff 
    console.log(voices); 
} 
window.speechSynthesis.getVoices(); 

或作爲與價值是聲音

(async() => { 

    const getVoices = (voiceName = "") => { 
    return new Promise(resolve => { 
     window.speechSynthesis.onvoiceschanged = e => { 
     // optionally filter returned voice by `voiceName` 
     // resolve(
     // window.speechSynthesis.getVoices() 
     // .filter(({name}) => /^en.+whisper/.test(name)) 
     //); 
     resolve(window.speechSynthesis.getVoices()); 
     } 
     window.speechSynthesis.getVoices(); 
    }) 
    } 

    const voices = await getVoices(); 
    console.log(voices); 

})(); 
+0

大的陣列返回Promise異步函數組成的,非常感謝你的解釋。將測試並標記您的解決方案,並在工作時加註,這可能會。 – jcubic

+0

@jcubic另請參見https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis/cancel – guest271314

+0

安裝語音調度程序和espeak足以使鉻語言工作,例如'spd-conf - ü'我有錯誤,找不到命令,當啓動鉻時,'--enable-speech-dispatcher'選項顯示關於不支持標誌的錯誤。 – jcubic