Chrome Speech Synthesis with longer texts

前端 未结 12 1208
难免孤独
难免孤独 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条回答
  •  春和景丽
    2020-11-30 20:24

    I want to say that through Chrome Extensions and Applications, I solved this quite irritating issue through using chrome.tts, since chrome.tts allows you to speak through the browser, instead of the window which stops the talk when you close the window.

    Using the below code, you can fix the above issue with large speakings:

    chrome.tts.speak("Abnormally large string, over 250 characters, etc...");
    setInterval(() => { chrome.tts.resume(); }, 100);
    

    I'm sure that will work, but I did this just to be safe:

    var largeData = "";
    var smallChunks = largeData.match(/.{1,250}/g);
    for (var chunk of smallChunks) {
      chrome.tts.speak(chunk, {'enqueue': true});
    }
    

    Hope this helps someone! It helped make my application work more functionally, and epicly.

提交回复
热议问题