speechSynthesis.getVoices()在Chromium Fedora中是空数组

时间:2017-10-21 12:22:47

标签: fedora chromium

Chromium是否支持Speech Synthesis API?我需要安装声音吗?如果是这样我怎么能这样做?我正在使用Fedora。像视频这样的声音我需要安装额外的包吗?

我试过这段代码:

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 个答案:

答案 0 :(得分:2)

  

Chromium是否支持Speech Synthesis API?

是的,Web Speech API在Chromium浏览器上有基本支持,但Chromium和Firefox实施规范存在一些问题,请参阅Blink>SpeechInternals>SpeechSynthesis,{{3 }}

  

我需要安装声音吗?如果是这样我怎么能这样做?我正在使用   Fedora的。像我需要安装额外包的视频之类的声音   它起作用了?

是的,需要安装声音。默认情况下,Chromium未附带设置为SpeechSynthesisUtterance voice属性的语音,请参阅Web Speech; How to use Web Speech API at chromium?

您可以将How to capture generated audio from window.speechSynthesis.speak() call?安装为系统语音合成服务器的服务器,将speech-dispatcher安装为语音合成器。

$ yum install speech-dispatcher espeak

您还可以在用户主文件夹中为speech-dispatcher设置配置文件,为speech-dispatcher和您使用的输出模块设置特定选项,例如espeak

$ spd-conf -u

使用--enable-speech-dispatcher标志启动Chromium会自动生成与speech-dispatcher的连接,您可以在LogLevel0之间设置5以查看之间的SSIP通信Chromium代码和speech-dispatcher

.getVoices()异步返回结果,需要调用两次

在GitHub espeak上查看此electron问题。

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

或作为异步函数组成,它返回Promise,其值为语音数组

(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);

})();