我使用自适应语音(文本到语音),并在数组中创建了一些简单的数据,并用promise遍历了它,但是当我将解析方法放入自适应语音的方法onend
时,不是语音的结尾,它会调用下一个数据。请查看我的代码。
var data = [
{'textTH': "สวัสดี ค่ะ", 'lang': "Thai Female", 'textEN': "Hello", 'langEN': "US English Female"},
{'textTH': "สวัสดี ค่ะ", 'lang': "Thai Female", 'textEN': "Hello 2", 'langEN': "US English Female"}
];
var j = data.length;
function step2(i){
return new Promise(function(resolve, reject){
if (typeof data[i] == 'undefined'){
reject("out of rang");
}
else{
var runEnglistSpeech = function () {
responsiveVoice.speak(data[i].textEN, data[i].langEN, { rate: .7, onend: resolve()});
console.log("done >>>> " + i);
};
responsiveVoice.speak(data[i].textTH, data[i].lang, { rate: .9, onend: runEnglistSpeech });
}
})
}
(async () => {
for (let i = 0; i < j; i++) {
await step2(i);
}
})();
响应式CDN => https://code.responsivevoice.org/responsivevoice.js
答案 0 :(得分:0)
我正在尝试超时,它可以工作,但是我想对此有任何选择
setTimeout(function(){ resolve(); }, 6000);
答案 1 :(得分:0)
您的代码正在运行。
更改
responsiveVoice.speak(data[i].textEN, data[i].langEN, { rate: .7, onend: resolve()});
至
responsiveVoice.speak(data[i].textEN, data[i].langEN, { rate: .7, onend: resolve});