长文本的语音合成问题在说话中途暂停
Speech Synthesis problem with long texts pause mid-speaking
美好的一天。
我的语音合成在朗读长文本时遇到了不一致的问题。
我正在尝试用英语和普通话进行文字转语音。当我指定 utterance.lang = 'en-US';我发现我的英文文章读完了。但是,当我使用 utterance.lang = 'zh-CN';我的英文和普通话只能读出30个字而已。不知道是编码问题还是什么问题
文章:
E. Cyclocarpum
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'));
}
}
是known bug. The workaround每14秒发一份简历
对于您的代码,这意味着在 'speak(utterance)' 之后添加以下内容:
let r = setInterval(() => {
console.log(speechSynthesis.speaking);
if (!speechSynthesis.speaking) {
clearInterval(r);
} else {
speechSynthesis.resume();
}
}, 14000);
美好的一天。
我的语音合成在朗读长文本时遇到了不一致的问题。
我正在尝试用英语和普通话进行文字转语音。当我指定 utterance.lang = 'en-US';我发现我的英文文章读完了。但是,当我使用 utterance.lang = 'zh-CN';我的英文和普通话只能读出30个字而已。不知道是编码问题还是什么问题
文章:
E. Cyclocarpum
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'));
}
}
是known bug. The workaround每14秒发一份简历
对于您的代码,这意味着在 'speak(utterance)' 之后添加以下内容:
let r = setInterval(() => {
console.log(speechSynthesis.speaking);
if (!speechSynthesis.speaking) {
clearInterval(r);
} else {
speechSynthesis.resume();
}
}, 14000);