Chrome Speech Synthesis with longer texts

前端 未结 12 1214
难免孤独
难免孤独 2020-11-30 19:48

I am getting a problem when trying to use Speech Synthesis API in Chrome 33. It works perfectly with a shorter text, but if I try longer text, it just stops in the middle. A

12条回答
  •  Happy的楠姐
    2020-11-30 20:10

    A simple and effective solution is to resume periodically.

    function resumeInfinity() {
        window.speechSynthesis.resume();
        timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
    }
    

    You can associate this with the onend and onstart events, so you will only be invoking the resume if necessary. Something like:

    var utterance = new SpeechSynthesisUtterance();
    
    utterance.onstart = function(event) {
        resumeInfinity();
    };
    
    utterance.onend = function(event) {
        clearTimeout(timeoutResumeInfinity);
    };
    

    I discovered this by chance!

    Hope this help!

提交回复
热议问题