我正在使用文本到语音服务 (TTS) 从 Yelp API 读取结果。我遇到了时间问题:
- 如果我太快调用 TTS 函数,语音会自行中断。
我的想法是编写一个信号量来检查音频是否正在播放,并且只有在完成后才将下一个命令变为红色。很遗憾:
- 如果我等到音频没有播放(audio.paused == true),程序挂起/不会跳出while循环。
有没有人对如何解决第一个问题而不遇到第二个问题有任何想法?
//check if command isn't already queued, and then add command to the queue
function voiceSynth (string, name) {
if(voiceQueue.indexOf(string)== -1){
voiceQueue.push(string)
voiceQueue.push(name) //used to keep track of the current item being read
}
//Iterate over items in the queue
while (voiceQueue.length > 1){
if (voiceBusy == false && audio.paused == true) {
voiceCall.call(undefined, voiceQueue.shift(),voiceQueue.shift())
}
}
}
//ajax call to the TTS service
function voiceCall (string, name) {
voiceBusy = true
console.log('synth called on ' + string)
$.ajax('read/?string=' + string + '&speed=' + speed, {
type: 'GET',
success: function(src) {
audio.setAttribute('src', src)
audio.play()
voiceBusy = false
voiceCursor = name
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr)
console.log(ajaxOptions)
console.log(thrownError)
}
})
}