javascript - Web Speech API - SpeechSynthesisUtterance onmark 事件不会触发
问题描述
使用 SSML 时,我无法在 Chrome 或 Edge 中触发“onmark”事件。我已经在 Chrome 和 Edge 中尝试过,并根据https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API的标准编写了代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/TTS/DMAC.TTS.SSML.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="btnSpeak" type="button" onclick="Speak(); return false;" value="Speak" />
</div>
<div id="output"></div>
</form>
</body>
<script>
var synth = window.speechSynthesis;
var voices = null;
function Speak() {
var utterance = new SpeechSynthesisUtterance();
utterance.onboundary = function (event) {
document.getElementById('output').innerHTML += 'onboundary Event: ' + event.toString() + "<br/>";
};
utterance.onmark = function (event) {
document.getElementById('output').innerHTML += 'onmark Event: ' + event.toString() + "<br/>";
}
utterance.text = '<mark name="w1"/>Hello <mark name="w2"/>my <mark name="w3"/>name <mark name="w4"/>is <mark name="w5"/>John.';
utterance.lang = 'en-US';
utterance.voice = voices[0];
synth.speak(utterance);
};
window.speechSynthesis.onvoiceschanged = function () {
voices = synth.getVoices();
};
</script>
</html>
解决方案
对于基于远程 tts 服务的语音,onboundary 事件似乎没有在 Chrome 上正确触发。检查您打算使用的语音的 localService 属性,选择 localService = true voices only。
在 Edge 上,所有声音的 onboundary 事件都被正确触发。
奇怪的是,Chromium 团队已将此已知问题标记为“wontfix”, https: //bugs.chromium.org/p/chromium/issues/detail?id=521666
推荐阅读
- authentication - ASP.NET Core:Cookie 未经过身份验证。失败消息:取消保护票证失败
- reactjs - React Native - 如何防止重新渲染“静态”组件
- javascript - 我可以使用 Discord API 在消息请求中保存信息吗?
- haskell - 有人可以解释这个 Haskell 问题吗?
- android - 搜索/过滤分页列表
- php - 用于 Nginx 和 php-fpm 的 Kubernetes Docker 上的连接错误
- firefox - 语音合成 API 在 Manjaro 上的 Firefox 中不起作用
- reactjs - Apollo Client 3 - 全局事件监听器?
- stata - 用抽样权重计算观测数
- javascript - 如何通过 ctrl + enter 将 br(换行符)添加到 contenteditable div?