问题
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