我已经使用http://www.masswerk.at/mespeak/中的MeSpeak库编写了一些简单的javascript
我想做的是,如果输入脚本包含方括号和数字,则在单词之间暂停几秒钟(即-“ [3]”将暂停3秒钟)...
我已经在其他地方看到了一种更优雅的方法,但是,令我感到沮丧的是,为什么以下代码不能播放第二个单词? (在FireFox 25.0.1版和meSpeak 1.8.1版中进行了测试)
代码如下:
<html>
<head>
<script src="mespeak.js"></script>
</head>
<body>
<button onclick="speakdata('Testing Word [2]Another [3]Again [5]Blah')">Talk</button>
<div id="audio"></div>
<script>
meSpeak.loadConfig("mespeak_config.json");
meSpeak.loadVoice("voices/en/en-us.json");
var t=0;
var speechArr = [];
var pauseArr = [];
var len =0;
var recurseLoop = 0;
function speakdata(spokendata) {
var looper = new Boolean();
looper = true;
var posCount = new Number(0);
var pauseCount = new Number(0);
while (looper)
{
if (spokendata.substring(posCount).indexOf("[") == -1) {
speechArr.push(meSpeak.speak (spokendata.substring(posCount)),{ 'rawdata': true });
pauseArr.push(0);
len++;
looper = false;
} else {
if (posCount == 0) {
speechArr.push(meSpeak.speak(spokendata.substring(posCount,spokendata.indexOf("[")-1),{ 'rawdata': true }));
pauseArr.push(0);
len++;
}
pauseCount += parseInt(spokendata.substring(posCount).substring(spokendata.substring(posCount).indexOf("[")+1, spokendata.substring(posCount).indexOf("]")))
posCount = parseInt(posCount+spokendata.substring(posCount).indexOf("]")+1)
if (spokendata.substring(posCount).indexOf("[") != -1) {
var posCount2 = spokendata.substring(posCount).indexOf("[");
speechArr.push(meSpeak.speak(spokendata.substring(posCount,posCount2+posCount),{ 'rawdata': true }));
pauseArr.push(pauseCount*1000);
len++;
posCount = posCount2+posCount
looper = true;
} else {
speechArr.push(meSpeak.speak(spokendata.substring(posCount),{ 'rawdata': true }));
pauseArr.push(pauseCount*1000);
len++;
looper = false;
}
}
}
setTimeout(function(){meSpeak.play(speechArr[0],1,playNext)},pauseArr[0]);
recurseLoop++;
}
function playNext() {
if (recurseLoop < len) {
setTimeout(function(){meSpeak.play(speechArr[recurseLoop],1,playNext)},pauseArr[recurseLoop]);
recurseLoop++;
}
}
</script>
</body>
</html>
最佳答案
所以....我想我找到了:
setTimeout(function(){meSpeak.play(speechArr[0],1,playNext)},pauseArr[0]);
recurseLoop++;
function playNext() {
if (recurseLoop < len) {
setTimeout(function(){meSpeak.play(speechArr[recurseLoop],1,playNext)},pauseArr[recurseLoop]);
recurseLoop++;
}
}
为我记录顺序:
timeout to 0 Testing Word
playing "Testing Word"
timeout to 1 Another
playing "Again "
timeout to 2 Again
playing "Blah"
timeout to 3 Blah
playing undefined
Speak: Can't play, not an Array, or ArrayBuffer, or base64-String: [object Undefined]
ended
请介意“ recurseLoop ++”;超时后!
这影响到
setTimeout播放SpeechArr [0],递增recurseLoop(现在为1)
在SpeechArr [0]之后调用playNext(),recurseLoop === 1
调用setTimeout
增量recurseLoop(recurseLoop === 2)
超时回来播放SpeechArr [2]
调用playNext(),recurseLoop === 2
调用setTimeout
增量recurseLoop(recurseLoop === 3)
超时回来播放SpeechArr [3]
将代码更改为仅在“ playNext()”的最开始处增加即可解决此问题:
setTimeout(function(){meSpeak.play(speechArr[0],1,playNext)},pauseArr[0]);
function playNext() {
if (++recurseLoop < len) {
setTimeout(function(){meSpeak.play(speechArr[recurseLoop],1,playNext)},pauseArr[recurseLoop]);
}
}
这记录到以下顺序:
timeout to 0 Testing Word
playing "Testing Word"
timeout to 1 Another
playing "Another "
timeout to 2 Again
playing "Again "
timeout to 3 Blah
playing "Blah"
ended
(我被未定义的值迷住了,这个值看起来像是mespeak.js或解决meSpeak.speak()中的错误,以至于我一开始完全错过了跳过的过程–参见其他尝试。)