首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >speechSynthesis.getVoices()是Chromium中的空数组

speechSynthesis.getVoices()是Chromium中的空数组
EN

Stack Overflow用户
提问于 2017-10-21 12:22:47
回答 1查看 3.4K关注 0票数 6

语音合成API支持铬吗?我需要安装声音吗?如果是这样的话,我怎么能做到呢?我在用费多拉。像视频这样的声音,我需要安装额外的软件包才能工作吗?

我试过这段代码:

代码语言:javascript
运行
复制
var msg = new SpeechSynthesisUtterance('I see dead people!');
msg.voice = speechSynthesis.getVoices().filter(function(voice) {
    return voice.name == 'Whisper';
})[0];
speechSynthesis.speak(msg);

来自聊天的Web应用程序-语音合成API简介的文章

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

我也试过:

代码语言:javascript
运行
复制
window.speechSynthesis.onvoiceschanged = function() {
    console.log(window.speechSynthesis.getVoices())
};

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-10 05:04:31

语音合成API支持铬吗?

是的,Web语音API在Chromium上有基本的支持,尽管该规范的Chromium和Firefox实现都存在一些问题,参见Blink>SpeechInternals>SpeechSynthesis网络语音

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

是的,需要安装声音。默认情况下,SpeechSynthesisUtterance voice属性中没有设置声音,参见如何在铬上使用Web语音API?如何从window.speechSynthesis.speak()调用中捕获生成的音频?

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

代码语言:javascript
运行
复制
$ yum install speech-dispatcher espeak

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

代码语言:javascript
运行
复制
$ spd-conf -u

使用--enable-speech-dispatcher标志启动铬会自动生成到speech-dispatcher的连接,在该连接中,可以在05之间设置LogLevel,以检查Chromium与speech-dispatcher之间的SSIP通信。

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

请参阅electron在GitHub 语音合成:无声音#586上的问题。

代码语言:javascript
运行
复制
window.speechSynthesis.onvoiceschanged = e => {
  const voices = window.speechSynthesis.getVoices();
  // do speech synthesis stuff
  console.log(voices);
}
window.speechSynthesis.getVoices();

或组成为异步函数,返回值为声音数组的Promise

代码语言:javascript
运行
复制
(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);

})();
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46863170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档