长文本在说话中间暂停的语音合成问题 [英] Speech Synthesis problem with long texts pause mid-speaking

查看:35
本文介绍了长文本在说话中间暂停的语音合成问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天.

我的语音合成在讲长文本时出现不一致的问题.

I'm having trouble with inconsistencies of my speech synthesis speaking long texts.

我正在尝试用英语和普通话进行文字转语音.当我指定 utterance.lang = 'en-US';我发现我的英文文章会一直读到读完.但是,当我使用 utterance.lang = 'zh-CN';我的英语和普通话文本只能读到 30 个单词.不知道是不是编码有问题.

I'm trying to make text-to-speech in English and Mandarin. When I specify utterance.lang = 'en-US'; I found out that my article in English is read out until finished. However, when I'm using utterance.lang = 'zh-CN'; my text in English and Mandarin can only read out until 30 words only. I don't know if there's a problem with coding or anything.

文章:

E.圆果

Enterolobium cyclocarpum,俗称 guanacaste、caro caro 或象耳树,是豌豆科的一种开花树种.豆科植物,原产于美洲热带地区,来自墨西哥中部南至巴西北部(罗赖马)和委内瑞拉.这是以其巨大的比例、宽阔的、通常为球形的冠部而闻名,以及形状奇特的种荚.这棵树的丰富,尤其是在哥斯达黎加的瓜纳卡斯特省它从强烈的阳光下提供的阴凉处,加上它的巨大,使其成为广泛认可的物种.它是哥斯达黎加的国树.

Enterolobium cyclocarpum, commonly known as guanacaste, caro caro, or elephant-ear tree, is a species of flowering tree in the pea family. Fabaceae, that is native to tropical regions of the Americas, from the central Mexico south to northern Brazil (Roraima) and Venezuela. It is known for its large proportions, its expansive, often spherical crown, and its curiously shaped seedpods. The abundance of this tree, especially in Guanacaste Province, Costa Rica, where it is prized for the shady relief it provides from the intense sun, coupled with its immensity, have made it a widely recognized species. It is the national tree of Costa Rica.

onload = function() {
    if ('speechSynthesis' in window) with(speechSynthesis) {

        var playEle = document.querySelector('#play');
        var pauseEle = document.querySelector('#pause');
        var stopEle = document.querySelector('#stop');
        var flag = false;

        playEle.addEventListener('click', onClickPlay);
        pauseEle.addEventListener('click', onClickPause);
        stopEle.addEventListener('click', onClickStop);

        function onClickPlay() {
            if(!flag){
                flag = true;
                utterance = new SpeechSynthesisUtterance(document.querySelector('article').textContent);
                utterance.lang = 'zh-CN';
                utterance.onend = function(){
                    flag = false; playEle.className = pauseEle.className = ''; stopEle.className = 'stopped';
                };
                playEle.className = 'played';
                stopEle.className = '';
                speak(utterance);
            }
             if (paused) { /* unpause/resume narration */
                playEle.className = 'played';
                pauseEle.className = '';
                resume();
            } 
        }

        function onClickPause() {
            if(speaking && !paused){ /* pause narration */
                pauseEle.className = 'paused';
                playEle.className = '';
                pause();
            }
        }

        function onClickStop() {
            if(speaking){ /* stop narration */
                /* for safari */
                stopEle.className = 'stopped';
                playEle.className = pauseEle.className = '';
                flag = false;
                cancel();

            }
        }
    }

    else { /* speech synthesis not supported */
        msg = document.createElement('h5');
        msg.textContent = "Detected no support for Speech Synthesis";
        msg.style.textAlign = 'center';
        msg.style.backgroundColor = 'red';
        msg.style.color = 'white';
        msg.style.marginTop = msg.style.marginBottom = 0;
        document.body.insertBefore(msg, document.querySelector('div'));
    }
}

推荐答案

这是一个 已知错误.解决方法是每14秒发一份简历.

It's a known bug. The workaround is to issue a resume every 14 seconds.

对于您的代码,这意味着在speak(utterance)"之后添加以下内容:

For your code this means to add the following after 'speak(utterance)':

let r = setInterval(() => {
  console.log(speechSynthesis.speaking);
  if (!speechSynthesis.speaking) {
    clearInterval(r);
  } else {
    speechSynthesis.resume();
  }
}, 14000);

这篇关于长文本在说话中间暂停的语音合成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆