SpeechSynthesisUtterance在移动浏览器中不起作用 [英] SpeechSynthesisUtterance not working in mobile broswer
问题描述
我正在手机网站上使用SpeechSynthesisUtterance.当我在桌面版本的网站中使用该代码时,该代码就可以了.但是我发现该功能在document.ready中的移动浏览器中不起作用,如下所示:
I am using SpeechSynthesisUtterance in my mobile website. This code is okay when I use it in my desktop version website. But I found that the function is not working in mobile browser in document.ready as below:
$(document).ready(function(){
var text_tts="say something";
speakText(text_tts);
});
function speakText(text_tts){
var u = new SpeechSynthesisUtterance();
u.text = text_tts;
u.lang = 'en-US';
u.rate = 1;
u.onend = function(event) { console.log('Finished in ' + event.elapsedTime + ' seconds.'); }
speechSynthesis.speak(u);
}
但是当我使用"click"事件时,它会起作用:
But when I use "click" event, it works:
$("body").on("click",".button",function(){
var tmp_body_text="say something";
var u = new SpeechSynthesisUtterance();
u.text = tmp_body_text;
u.lang = 'en-US';
u.rate = 1;
//u.onend = function(event) { console.log('Finished in ' + event.elapsedTime + ' seconds.'); }
speechSynthesis.speak(u);
});
请帮助.谢谢.
推荐答案
I have seen several mentions (like this, for example) that speak
only works when called from a user interaction, such as a click. From what I have seen, that seems to be the case for Safari on iOS, and that appears to be what you are describing in your question.
我实际上还没有找到任何可以证实这种行为的文档,并且不胜感激任何人都可以提供的参考.
I have not actually found any kind of documentation that confirms this behavior, and would appreciate any references anyone can provide.
这篇关于SpeechSynthesisUtterance在移动浏览器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!