本文介绍了Chrome语音合成与更长的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome 33中尝试使用Speech Synthesis API时出现问题。它可以使用较短的文本,但如果我尝试更长的文本,它会停在中间。在它停止一次之后,在浏览器重启之前,语音合成功能无法在Chrome中的任何位置运行。



示例代码():

  function speak(text){
var msg = new SpeechSynthesisUtterance();
var voices = speechSynthesis.getVoices();
msg.voice =语音[10];
msg.voiceURI ='native';
msg.volume = 1;
msg.rate = 1;
msg.pitch = 2;
msg.text = text;
msg.lang ='en-US';

speechSynthesis.speak(msg);
}

说('短文');
speak('通过即插即用网络协同管理授权市场,在安装好基础优势后动态拖延B2C用户,在没有革命性投资回报率的情况下大幅实现客户定向融合的可视化效果,快速释放跨媒体信息最大化实时模式的及时交付结果,在没有功能解决方案的情况下大大维护点击和实时解决方案。
说('另一短文');

停止在第二个文本中间发言,我无法再获得任何其他页面这是一个浏览器错误或某种安全限制?

解决方案 / div>

Google Chrome语音合成已经有一段时间了。经过一番调查后,我发现了以下内容:


  • 只有当语音不是本地语音,

  • 剪切通常发生在 200-300个字符之间,

  • 通过执行 speechSynthesis.cancel();

  • 取消冻结。 onend 事件有时会决定不要开火。一个古怪的解决方法是在讲话之前将console.log()移出话语对象。此外,我发现在setTimeout回调中包装说话调用有助于缓解这些问题。



针对这些问题,我写了一篇功能可以克服字符限制,将文本分成更小的话语,然后逐一播放。显然你会得到一些奇怪的声音,有时候因为句子可能被分成两个单独的话语,每个话语之间有一个小的时间延迟,但是 code> 将尝试并将这些点拆分为标点符号,打破声音不太明显。



更新



- 在。非常感谢的贡献。



函数:

pre $ var speechUpperranceChunker = function(utt,settings,callback){
settings = settings || {};
var newUtt;
var txt =(settings&&& settings.offset!== undefined?utt.text.substring(settings.offset):utt.text);
if(utt.voice& utt.voice.voiceURI ==='native'){//不是spec的一部分
newUtt = utt;
newUtt.text = txt;
if(speechUtteranceChunker.cancel){
speechUtteranceChunker.cancel = false;
}
if(callback!)
newUtt.addEventListener('end',function ==未定义){
callback();
}
});
}
else {
var chunkLength =(settings&&& settings.chunkLength)|| 160;
var pattRegex = new RegExp('^ [\\s\\S] {'+ Math.floor(chunkLength / 2)+','+ chunkLength +'} [。!?,] {1} | ^ [\\s\\S] {1,'+ chunkLength +'} $ | ^ [\\s\\S] {1,'+ chunkLength +'} );
var chunkArr = txt.match(pattRegex);

if(chunkArr [0] === undefined || chunkArr [0] .length< = 2){
//一旦所有文本被说出,就调用...
if(callback!== undefined){
callback();
}
return;
}
var chunk = chunkArr [0];
newUtt = new SpeechSynthesisUtterance(chunk);
var x;
for(x in utt){
if(utt.hasOwnProperty(x)&& x!=='text'){
newUtt [x] = utt [x]


newUtt.addEventListener('end',function(){
if(speechUtteranceChunker.cancel){
speechUtteranceChunker.cancel = false;
返回;
}
settings.offset = settings.offset || 0;
settings.offset + = chunk.length - 1;
speechUtteranceChunker(utt,settings,callback) ;
});
}

if(settings.modifier){
settings.modifier(newUtt);
}
console.log(newUtt); //重要!!不要删除:记录对象,修复了一些触发问题。
//将语音调用放置在回调函数中可以修复排序和问题。
setTimeout(function(){
speechSynthesis.speak(newUtt);
},0);
};

如何使用它...

  //像往常一样创建一个话语...... 
var myLongText =这是一些长文本,哦,我的天啊,看看我多久了,wooooohooo! ;

var utterance = new SpeechSynthesisUtterance(myLongText);

//像通常那样修改它
var voiceArr = speechSynthesis.getVoices();
utterance.voice = voiceArr [2];

//将它传递给chunking函数使其播放。
//您可以通过更改下面的chunkLength属性来设置最大字符数。
//还可以添加一个回调函数,一旦整个文本被说出就会触发。
speechUtteranceChunker(话语,{
chunkLength:120
},function(){
//完成时要执行的代码
console.log('done') ;
});

希望人们认为这是有用的。


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. After it has stopped once like that, the Speech Synthesis does not work anywhere within Chrome until the browser is restarted.

Example code (http://jsfiddle.net/Mdm47/1/):

function speak(text) {
    var msg = new SpeechSynthesisUtterance();
    var voices = speechSynthesis.getVoices();
    msg.voice = voices[10];
    msg.voiceURI = 'native';
    msg.volume = 1;
    msg.rate = 1;
    msg.pitch = 2;
    msg.text = text;
    msg.lang = 'en-US';

    speechSynthesis.speak(msg);
}

speak('Short text');
speak('Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without revolutionary ROI. Efficiently unleash cross-media information without cross-media value. Quickly maximize timely deliverables for real-time schemas. Dramatically maintain clicks-and-mortar solutions without functional solutions.');
speak('Another short text');

It stops speaking in the middle of the second text, and I can't get any other page to speak after that.

Is it a browser bug or some kind of security limitation?

解决方案

I've had this issue for a while now with Google Chrome Speech Synthesis. After some investigation, I discovered the following:

  • The breaking of the utterances only happens when the voice is not a native voice,
  • The cutting out usually occurs between 200-300 characters,
  • When it does break you can un-freeze it by doing speechSynthesis.cancel();
  • The 'onend' event sometimes decides not to fire. A quirky work-around to this is to console.log() out the utterance object before speaking it. Also I found wrapping the speak invocation in a setTimeout callback helps smooth these issues out.

In response to these problems, I have written a function that overcomes the character limit, by chunking the text up into smaller utterances, and playing them one after another. Obviously you'll get some odd sounds sometimes as sentences might be chunked into two separate utterances with a small time delay in between each, however the code will try and split these points at punctuation marks as to make the breaks in sound less obvious.

Update

I've made this work-around publicly available at https://gist.github.com/woollsta/2d146f13878a301b36d7#file-chunkify-js. Many thanks to Brett Zamir for his contributions.

The function:

var speechUtteranceChunker = function (utt, settings, callback) {
    settings = settings || {};
    var newUtt;
    var txt = (settings && settings.offset !== undefined ? utt.text.substring(settings.offset) : utt.text);
    if (utt.voice && utt.voice.voiceURI === 'native') { // Not part of the spec
        newUtt = utt;
        newUtt.text = txt;
        newUtt.addEventListener('end', function () {
            if (speechUtteranceChunker.cancel) {
                speechUtteranceChunker.cancel = false;
            }
            if (callback !== undefined) {
                callback();
            }
        });
    }
    else {
        var chunkLength = (settings && settings.chunkLength) || 160;
        var pattRegex = new RegExp('^[\\s\\S]{' + Math.floor(chunkLength / 2) + ',' + chunkLength + '}[.!?,]{1}|^[\\s\\S]{1,' + chunkLength + '}$|^[\\s\\S]{1,' + chunkLength + '} ');
        var chunkArr = txt.match(pattRegex);

        if (chunkArr[0] === undefined || chunkArr[0].length <= 2) {
            //call once all text has been spoken...
            if (callback !== undefined) {
                callback();
            }
            return;
        }
        var chunk = chunkArr[0];
        newUtt = new SpeechSynthesisUtterance(chunk);
        var x;
        for (x in utt) {
            if (utt.hasOwnProperty(x) && x !== 'text') {
                newUtt[x] = utt[x];
            }
        }
        newUtt.addEventListener('end', function () {
            if (speechUtteranceChunker.cancel) {
                speechUtteranceChunker.cancel = false;
                return;
            }
            settings.offset = settings.offset || 0;
            settings.offset += chunk.length - 1;
            speechUtteranceChunker(utt, settings, callback);
        });
    }

    if (settings.modifier) {
        settings.modifier(newUtt);
    }
    console.log(newUtt); //IMPORTANT!! Do not remove: Logging the object out fixes some onend firing issues.
    //placing the speak invocation inside a callback fixes ordering and onend issues.
    setTimeout(function () {
        speechSynthesis.speak(newUtt);
    }, 0);
};

How to use it...

//create an utterance as you normally would...
var myLongText = "This is some long text, oh my goodness look how long I'm getting, wooooohooo!";

var utterance = new SpeechSynthesisUtterance(myLongText);

//modify it as you normally would
var voiceArr = speechSynthesis.getVoices();
utterance.voice = voiceArr[2];

//pass it into the chunking function to have it played out.
//you can set the max number of characters by changing the chunkLength property below.
//a callback function can also be added that will fire once the entire text has been spoken.
speechUtteranceChunker(utterance, {
    chunkLength: 120
}, function () {
    //some code to execute when done
    console.log('done');
});

Hope people find this as useful.

这篇关于Chrome语音合成与更长的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:40