gpt4 book ai didi

javascript - meSpeak.js - 使用回调方法时不播放第二个单词

转载 作者:行者123 更新时间:2023-11-28 01:46:39 24 4
gpt4 key购买 nike

我使用 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++;”就在超时之后!

这会影响

  • 设置播放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() 中的错误,以至于我完全错过了迭代中的这种跳过 - 请参阅其他人也可以尝试一下。)

关于javascript - meSpeak.js - 使用回调方法时不播放第二个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com