gpt4 book ai didi

javascript - 如何在 1 个函数中多次调用语音识别?

转载 作者:行者123 更新时间:2023-11-30 20:07:10 25 4
gpt4 key购买 nike

我对 JavaScript 比较陌生,目前正在做一个初学者可能不应该开始的项目。我已经在 python 中编码了一段时间,并决定试一试。我想做一个私有(private)助理,或者我喜欢这样调用它,Jarvis(来自 IRON MAN)。它目前可以监听命令并很好地执行它们,但是,我正在尝试制作一个 send email 命令,该命令需要用户在一个函数中进行多次输入。这个发送电子邮件功能在技术上是在 parseCommand() 中,我在其中传递一个标识符,称为 i。我最终会向 parseCommand() 添加更多命令,标识符将有助于了解要执行的命令。当我说“jarvis 发送电子邮件”时,parseCommand("email") 被调用。这是一个 promise ,因为一旦完成,我想再次调用 annyang(命令监听器)。

当“jarvis send an email”被触发时->

"jarvis send an email" : async function(){
annyang.abort(); // stop listening
await parseCommand("email").then(annyang.start()); // Start listening for another command when done
}

解析命令:

parseCommand(i){
return new Promise(async (resolve) => {
recognition.stop();



if(i == "email"){
var SUBJECT;
var TEXT;
var TO;

console.log("Running: send an email")
await this.promptListen("Who shall I send it to?").then( async (email) =>{
TO = email;
console.log("got TO", TO)

await this.promptListen("What shall the subject be?").then( async (subject) => {
SUBJECT = subject;
console.log("got SUBJECT", SUBJECT)

await this.promptListen("What would you like to say?").then((text) => {
TEXT = text;
console.log("got TEXT", TEXT)
resolve()
})
})
});
}
})
}

提示监听命令:

promptListen(prompt){
return new Promise((resolve, reject) => {
socket.emit("speak", prompt);
setTimeout(1000);

promptRecognition.stop(); // NOTE #1

promptRecognition.start();

promptRecognition.onresult = function(event){
console.log(event)
promptRecognition.stop();
resolve(event.results[0][0].transcript);
}
promptRecognition.onnomatch = function() {
promptRecognition.stop();
reject(promptListen(prompt));
}
})

}

当前功能的问题是它根本不起作用。系统提示我“我应该将它发送给谁?”贾维斯在听。我得到了 TO 控制台输出,但是,之后我得到了一个错误:

responses.js:63 Uncaught (in promise) DOMException: Failed to execute 'start' on 'SpeechRecognition': recognition has already started.
at Promise (http://localhost:3000/scripts/responses.js:63:31)
at new Promise (<anonymous>)
at Respond.promptListen (http://localhost:3000/scripts/responses.js:57:16)
at promptListen.then (http://localhost:3000/scripts/responses.js:41:28)

错误表明我已经在运行 SpeechRecognition,但是,为了以防万一,我每次都在 promptListen() 开始时停止它。我完全不知道发生了什么。期望的结果是每次调用 promptListen() 都会提示用户,听取响应,将响应存储在一个变量中:对于发送电子邮件所需的每个变量(收件人、主题和正文)。我不熟悉 JavaScript、promises、async 或 await。我已经尝试了所有可能的变体,也查看了文档但无济于事。如果我不使用 promises,则会同时为收件人、主题和正文提示调用 promptListen() 函数。这就是为什么我决定尝试用 promise 来解决问题。感谢您的帮助 - 谢谢!

最佳答案

我的代码在理论上是可行的。问题是,或者至少我认为是,当我调用 then promptRecoginition.stop() 时需要时间来停止。一旦我解决了最近的 promptListen(),我立即再次调用 .start(),因此 promptRecognition 没有时间到 .stop()。我发现有效的解决方案是:

setTimeout( async () => {
await this.promptListen("What shall the subject be").then( (subject) => {
SUBJECT = subject;
// Add another setTimeout here
}
}, 1000);

使用 setTimeout 函数,我为 .stop() 提供了 promptRecognition 时间,然后继续没有任何错误。

关于javascript - 如何在 1 个函数中多次调用语音识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52799063/

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