使用“prompt”和“say”编写简单的Node.js文本到语音转换应用程序。如何使提示等待语音结束后再显示新的提示行?
我将chat()移到了回调函数之外(参见末尾的注释代码),但这会导致提示符提前出现(正常行为)。
我希望在语音完成后返回提示,因为一旦say.speak回调运行,就会重新启动chat()。
程序在say.speak完成后立即退出,不显示新的提示。
const prompt = require('prompt')
const say = require('say')
chat()
function chat() {
prompt.get(['chat'], (error, result)=> {
if(error) return console.log('prompt <ERROR>: '+error.message)
if (result.chat === 'exit') {
terminate_self()
} else if (result.chat == '') {
say.speak('No input received.', '', '', ()=> {
chat() // program terminates once speech is complete
})
} else {
// process input here
say.speak(result.chat, '', '', ()=> {
chat() // program terminates once speech is complete
})
}
// chat() // program stays alive, BUT prompt returns before speech is completed
})
}
current result:
user@homeserver:~/Nodejs/ttschat$ node bin.js
chat | hey
user@homeserver:~/Nodejs/ttschat$
desired result:
user@homeserver:~/Nodejs/ttschat$ node bin.js
chat | hey
chat | hey
chat | exit
user@homeserver:~/Nodejs/ttschat$
https://stackoverflow.com/questions/56658514
复制相似问题