我希望下面的代码输出内部()响应的实际值,而不是Promise.我认为.Then()将等待输出,直到promise 得到解决.我应该换些什么? 到目前为止,输出将是未定义的,而不是YourAudioFile.wav的内容. 我知道代码的工作方式并非如此,因为如果我在INTERN()中使用sole.log(Response),响应的实际值将被打印到终端,而不是未定义或promise <;>;
const fs = require('fs');
const sdk = require("microsoft-cognitiveservices-speech-sdk");
const speechConfig = sdk.SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
speechConfig.speechRecognitionLanguage = "he-IL";
const audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync("YourAudioFile.wav"));
const speechRecognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);
async function inner() {
let response = ""
speechRecognizer.startContinuousRecognitionAsync();
speechRecognizer.recognized = (s, e) => {
if (e.result.reason == sdk.ResultReason.RecognizedSpeech) {
//console.log(`RECOGNIZED: Text=${e.result.text}`);
response += (e.result.text + " ")
}
else if (e.result.reason == sdk.ResultReason.NoMatch) {
console.log("NOMATCH: Speech could not be recognized.");
}
};
speechRecognizer.canceled = (s, e) => {
console.log(`CANCELED: Reason=${e.reason}`);
if (e.reason == sdk.CancellationReason.Error) {
console.log(`"CANCELED: ErrorCode=${e.errorCode}`);
console.log(`"CANCELED: ErrorDetails=${e.errorDetails}`);
console.log("CANCELED: Did you set the speech resource key and region values?");
}
speechRecognizer.stopContinuousRecognitionAsync();
};
speechRecognizer.sessionStopped = (s, e) => {
speechRecognizer.stopContinuousRecognitionAsync();
return response
};
}
inner().then((msg) => {
console.log(msg)
}).catch((err) => {
console.log(err)
})