SpeechSynthesis.getVoices() 是 Chromium Fedora 中的空数组

jcu*_*bic 5 fedora chromium

Chromium 是否支持语音合成 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);
Run Code Online (Sandbox Code Playgroud)

来自文章Web 应用程序会说话 - 语音合成 API 简介

但是函数 SpeechSynthesis.getVoices() 返回空数组。

我也试过:

window.speechSynthesis.onvoiceschanged = function() {
    console.log(window.speechSynthesis.getVoices())
};
Run Code Online (Sandbox Code Playgroud)

函数被执行,但数组也是空的。

https://fedoraproject.org/wiki/Chromium 上有使用--enable-speech-dispatcher标志的信息,但是当我使用它时,我收到了不支持标志的警告。

gue*_*314 4

Chromium 支持语音合成 API 吗?

是的,Web Speech API在 Chromium 浏览器中具有基本支持,尽管 Chromium 和 Firefox 规范的实现都存在一些问题,请参阅Blink>SpeechInternals>SpeechSynthesisWeb Speech

我需要安装语音吗?如果是这样我该怎么做?我正在使用费多拉。声音像视频一样需要安装额外的软件包才能工作吗?

是的,需要安装语音。默认情况下,Chromium 未附带要在SpeechSynthesisUtterance voice属性中设置的语音,请参阅如何在 chromium 上使用 Web Speech API?; 如何捕获 window.speechSynthesis.speak() 调用生成的音频?

您可以安装speech-dispatcher为系统语音合成服务器的服务器和espeak语音合成器。

$ yum install speech-dispatcher espeak
Run Code Online (Sandbox Code Playgroud)

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

$ spd-conf -u
Run Code Online (Sandbox Code Playgroud)

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

.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();
Run Code Online (Sandbox Code Playgroud)

或组成一个异步函数,返回一个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);

})();
Run Code Online (Sandbox Code Playgroud)