Chrome Speech Synthesis with longer texts

前端 未结 12 1216
难免孤独
难免孤独 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:20

    Other suggestion do weird thing with dot or say DOT and do not respect speech intonnation on sentence end.

    var CHARACTER_LIMIT = 200;
    var lang = "en";
    
    var text = "MLA format follows the author-page method of in-text citation. This means that the author's last name and the page number(s) from which the quotation or paraphrase is taken must appear in the text, and a complete reference should appear on your Works Cited page. The author's name may appear either in the sentence itself or in parentheses following the quotation or paraphrase, but the page number(s) should always appear in the parentheses, not in the text of your sentence. Joe waited for the train. The train was late. Mary and Samantha took the bus.";
    
        speak(text, lang)
    
        function speak(text, lang) {
    
          //Support for multipart text (there is a limit on characters)
          var multipartText = [];
    
          if (text.length > CHARACTER_LIMIT) {
    
            var tmptxt = text;
    
            while (tmptxt.length > CHARACTER_LIMIT) {
    
              //Split by common phrase delimiters
              var p = tmptxt.search(/[:!?.;]+/);
              var part = '';
    
              //Coludn't split by priority characters, try commas
              if (p == -1 || p >= CHARACTER_LIMIT) {
                p = tmptxt.search(/[,]+/);
              }
    
              //Couldn't split by normal characters, then we use spaces
              if (p == -1 || p >= CHARACTER_LIMIT) {
    
                var words = tmptxt.split(' ');
    
                for (var i = 0; i < words.length; i++) {
    
                  if (part.length + words[i].length + 1 > CHARACTER_LIMIT)
                    break;
    
                  part += (i != 0 ? ' ' : '') + words[i];
    
                }
    
              } else {
    
                part = tmptxt.substr(0, p + 1);
    
              }
    
              tmptxt = tmptxt.substr(part.length, tmptxt.length - part.length);
    
              multipartText.push(part);
              //console.log(part.length + " - " + part);
    
            }
    
            //Add the remaining text
            if (tmptxt.length > 0) {
              multipartText.push(tmptxt);
            }
    
          } else {
    
            //Small text
            multipartText.push(text);
          }
    
    
          //Play multipart text
          for (var i = 0; i < multipartText.length; i++) {
    
            //Use SpeechSynthesis
            //console.log(multipartText[i]);
    
            //Create msg object
            var msg = new SpeechSynthesisUtterance();
            //msg.voice = profile.systemvoice;
            //msg.voiceURI = profile.systemvoice.voiceURI;
            msg.volume = 1; // 0 to 1
            msg.rate = 1; // 0.1 to 10
            // msg.rate = usersetting || 1; // 0.1 to 10
            msg.pitch = 1; //0 to 2*/
            msg.text = multipartText[i];
            msg.speak = multipartText;
            msg.lang = lang;
            msg.onend = self.OnFinishedPlaying;
            msg.onerror = function (e) {
              console.log('Error');
              console.log(e);
            };
            /*GC*/
            msg.onstart = function (e) {
              var curenttxt = e.currentTarget.text;
              console.log(curenttxt);
              //highlight(e.currentTarget.text);
              //$('#showtxt').text(curenttxt);
              //console.log(e);
            };
            //console.log(msg);
            speechSynthesis.speak(msg);
    
          }
    
        }
    

    https://jsfiddle.net/onigetoc/9r27Ltqz/

提交回复
热议问题