SpeechSynthesisUtterance not working in mobile broswer

时光毁灭记忆、已成空白 提交于 2019-12-23 15:12:24

问题


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);

}

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);
                   });

Please help. Thank you.


回答1:


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.



来源:https://stackoverflow.com/questions/28806399/speechsynthesisutterance-not-working-in-mobile-broswer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!