Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getVoices()未在Firefox中列出语音_Javascript_Html_Firefox_Text To Speech_Speech Synthesis - Fatal编程技术网

Javascript getVoices()未在Firefox中列出语音

Javascript getVoices()未在Firefox中列出语音,javascript,html,firefox,text-to-speech,speech-synthesis,Javascript,Html,Firefox,Text To Speech,Speech Synthesis,我正在开发一个需要在web浏览器中使用文本到语音的应用程序。我正在使用HTML5语音合成。在Google Chrome上,代码运行良好,使用| getVoices()|列出所有可用的语音,但在Firefox中根本没有列出任何语音。我正在Firefox 56.0(Ubuntu)上测试我的代码 在互联网上搜索时,我确实遇到了一个StackOverflow,它建议在| onVoiceChanged |事件之后调用getVoices()函数 window.speechSynthesis.onvo

我正在开发一个需要在web浏览器中使用文本到语音的应用程序。我正在使用HTML5语音合成。在Google Chrome上,代码运行良好,使用| getVoices()|列出所有可用的语音,但在Firefox中根本没有列出任何语音。我正在Firefox 56.0(Ubuntu)上测试我的代码

在互联网上搜索时,我确实遇到了一个StackOverflow,它建议在| onVoiceChanged |事件之后调用getVoices()函数

    window.speechSynthesis.onvoiceschanged = function() {
    window.speechSynthesis.getVoices();
    ... 
};
我以上述方式调用该调用,它在Chrome中可以正常工作,但在Firefox上不能

另一个StackOverflow回答建议我在Firefox的about:config中启用| media.webspeech.synth.enabled |,但在我的Firefox中,首选项| media.webspeech.synth.enabled |已设置为true

我查看了MDN文档,该页面上的示例在Firefox中没有运行,但在Chrome中运行良好。我发现Firefox55以后的版本都支持SpeechSynthesis,但它对我不起作用


此外,Mozilla开发者网络还演示了语音合成在我的Firefox上无法工作,但在Google Chrome上运行良好。我在网上广泛搜索了一个解决方案,但没有找到。有人能给我指出正确的方向吗。

遇到了同样的问题,下面是我的答案

  • 在Firefox Ubuntu 16.04上对我不起作用
  • 在virtualbox Windows上工作。声音来自窗户。“微软大卫”就是其中之一
  • Chrome可以在Ubuntu上运行,但只有在在线的情况下才能运行。它不会在控制台中显示任何流量,但语音仅在联机时才起作用
原始错误似乎表明您需要安装speechd(语音调度器),请参阅。

我正在Windows 7 64位pro下运行FireFox 58.0.2(64位)。你提到的确实为我列出了一个声音:微软安娜英语(美国)(en-US)。此语音由我的Windows操作系统提供,而不是FireFox(Chrome列出了Chrome附带的19种附加语音)

你的代码在Chrome中工作而不是在Firefox中工作的原因是Firefox不调用speechSynthesis.onvoiceschanged,而Chrome调用

为什么??以下是Mozilla对voiceschanged事件的描述:

Web语音API的voiceschanged事件在列表 将由 getVoices()方法已更改(当VoiceSchange 事件火灾。)

这只是猜测,但可能是因为Chrome在向页面“添加”声音后触发了事件。Firefox没有,所以它没有

前面提到的演示通过在触发它的条件代码之前调用
populateVoiceList()
(下面底部第四行,从):

函数populateVoiceList(){
voices=synth.getVoices();
var selectedIndex=voiceSelect.selectedIndex<0?0:voiceSelect.selectedIndex;
voiceSelect.innerHTML='';
对于(i=0;i

这是我在web应用程序中采用的方法否则我的语音列表永远不会被Firefox填充。这段代码也恰好解决了Safari的类似问题;请参阅。

该演示在Fx ESR 53.3.0和当前的56中对我有效,在Win 10上是否调用
.getVoices()
onvoiceschanged
事件处理程序之外?
function populateVoiceList() {
  voices = synth.getVoices();
  var selectedIndex = voiceSelect.selectedIndex < 0 ? 0 : voiceSelect.selectedIndex;
  voiceSelect.innerHTML = '';
  for(i = 0; i < voices.length ; i++) {
    var option = document.createElement('option');
    option.textContent = voices[i].name + ' (' + voices[i].lang + ')';

    if(voices[i].default) {
      option.textContent += ' -- DEFAULT';
    }

    option.setAttribute('data-lang', voices[i].lang);
    option.setAttribute('data-name', voices[i].name);
    voiceSelect.appendChild(option);
  }
  voiceSelect.selectedIndex = selectedIndex;
}

populateVoiceList();
if (speechSynthesis.onvoiceschanged !== undefined) {
  speechSynthesis.onvoiceschanged = populateVoiceList;
}