15

我在 Windows 10 上使用 chrome 版本 55.0.2883.87 m(64 位)。

以下简单的 html 文件重现了该问题,并从我更复杂的应用程序中提取。它应该在页面加载时说出 3 个单词。它适用于 MS Edge 和 Firefox,但不适用于 chrome。几周前,这段代码在 Chrome 上为我工作没问题。

<html>
<head>
    <script lang="javascript">
        window.speechSynthesis.speak(new SpeechSynthesisUtterance("cat"));
        window.speechSynthesis.speak(new SpeechSynthesisUtterance("dog"));
        window.speechSynthesis.speak(new SpeechSynthesisUtterance("bark"));
    </script>
</head>
<body></body>
</html>
4

6 回答 6

23

I may never know for sure, because this problem was intermittent, but it seemed to go away after I started to cancel right before speak.

utter = new window.SpeechSynthesisUtterance("cat");

window.speechSynthesis.cancel();
window.speechSynthesis.speak(utter);

I don't think the cancel necessarily has to come between the utterance object creation and use. Just that it come before every speak. I may have had a different problem as I was only creating one utterance object, not a bunch. I did only see it on Chrome 78. Using Windows 7, 64-bit. Never saw the problem on Firefox or Edge.

EDIT 2 weeks later. No recurrences after several dozen tries. It seems .cancel() solved my problem. My symptoms were: calling speechSynthesis.speak() in Chrome would sometimes not start the speech. There were no immediate indications of a problem in the code, speechSynthesis.speaking would be true and .pending would be false. There would be no events from the utterance object. Normally, when speech would work, I'd get a 'start' event about 0.1 seconds after calling .speak().

于 2019-11-09T02:23:29.843 回答
1

SpeechSynthesis.speak()自 2018 年起,未经用户激活就不再允许在 Google 的 Chrome 网络浏览器中使用。它违反了 Google Chrome 的自动播放政策。因此,谷歌浏览器已经设法取消了它的自动播放功能,但你可以通过添加一个按钮来进行自定义调用来使用它。

您可以访问此处查看 chrome 本身提供的状态,下面的图片也清楚地显示了未经用户许可禁止 SpeechSynthesis.speak() 调用。

链接到图片

链接到谷歌浏览器的文章

于 2020-09-13T06:20:36.557 回答
1

除此之外,对我来说问题是实例的播放速率SpeechSynthesisUtterance高于 2。我发现它必须在 chrome 中设置为 2 或更低(尽管它在其他浏览器(如 safari)中具有更高的速率)。

在 chrome 中,如果 utterance rate 高于 2,它会导致window.speechSynthesis卡住,并且需要window.speechSynthesis.cancel()在它再次播放音频之前(以低于 2 的有效速率)通过.speak().

于 2021-07-16T05:35:37.203 回答
0

您的文字转语音试用是否只工作过一次?这就是为什么。

在 chrome 中你必须取消语音合成,否则它不符合谷歌的自动播放政策。所以你应该开始你的脚本:

window.speechSynthesis.cancel()

取消之前发生的任何语音合成。

于 2021-09-16T12:14:09.200 回答
0

除了在调用 new 时指定文本,您可以尝试分别指定具有速率、音量和文本的对象,然后将其转换为语音。

于 2021-07-02T20:32:15.527 回答
0

resultsDisplay = document.getElementById("rd");
startButton = document.getElementById("startbtn");
stopButton = document.getElementById("stopbtn");

recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition || window.mozSpeechRecognition || window.msSpeechRecognition)();
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.maxAlternatives = 5;

recognition.onresult = function(event) {
  resultsDisplay.innerHTML = "You Said:" + event.results[0][0].transcript;
};

function start() {
  recognition.start();
  startButton.style.display = "none";
  stopButton.style.display = "block";
}

function stop() {
  recognition.stop();
  startButton.style.display = "block";
  stopButton.style.display = "none";
}
.resultsDisplay {width: 100%; height: 90%;}
#stopbtn {display: none;}
<div class="resultsDisplay" id="rd"></div>
<br/>

<center>
  <button onclick="start()" id="startbtn">Start</button>
  <button onclick="stop()" id="stopbtn">Stop</button>
</center>

尝试

utterance = new SpeechSynthesisUtterance("cat, dog, bark");
speechSynthesis.speak(utterance);

我在LiveWeave做了一个编织

于 2017-11-21T00:28:53.717 回答