Chrome Speech Synthesis with longer texts

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

    Here is what i ended up with, it simply splits my sentences on the period "."

    var voices = window.speechSynthesis.getVoices();
    
    var sayit = function ()
    {
        var msg = new SpeechSynthesisUtterance();
    
        msg.voice = voices[10]; // Note: some voices don't support altering params
        msg.voiceURI = 'native';
        msg.volume = 1; // 0 to 1
        msg.rate = 1; // 0.1 to 10
        msg.pitch = 2; //0 to 2
        msg.lang = 'en-GB';
        msg.onstart = function (event) {
    
            console.log("started");
        };
        msg.onend = function(event) {
            console.log('Finished in ' + event.elapsedTime + ' seconds.');
        };
        msg.onerror = function(event)
        {
    
            console.log('Errored ' + event);
        }
        msg.onpause = function (event)
        {
            console.log('paused ' + event);
    
        }
        msg.onboundary = function (event)
        {
            console.log('onboundary ' + event);
        }
    
        return msg;
    }
    
    
    var speekResponse = function (text)
    {
        speechSynthesis.cancel(); // if it errors, this clears out the error.
    
        var sentences = text.split(".");
        for (var i=0;i< sentences.length;i++)
        {
            var toSay = sayit();
            toSay.text = sentences[i];
            speechSynthesis.speak(toSay);
        }
    }
    

提交回复
热议问题