修复语音识别小bug

This commit is contained in:
sijinhui 2024-03-28 19:25:51 +08:00
parent 24eb62f5e9
commit 868cf96cc8

View File

@ -47,8 +47,7 @@ export default function VoiceInput({
function onRecognizedResult(result: SpeechRecognitionResult) { function onRecognizedResult(result: SpeechRecognitionResult) {
// setVoiceInputText(""); // setVoiceInputText("");
setVoiceInputText(`${result.text}`); setVoiceInputText(`${result.text ?? ""}`);
let intentJson = result.properties.getProperty( let intentJson = result.properties.getProperty(
ms_audio_sdk.PropertyId.LanguageUnderstandingServiceResponse_JsonResult, ms_audio_sdk.PropertyId.LanguageUnderstandingServiceResponse_JsonResult,
); );
@ -57,8 +56,6 @@ export default function VoiceInput({
} }
// setTempUserInput(""); // setTempUserInput("");
console.log("3333", tempUserInput, "2", voiceInputText);
// if (result?.translations) { // if (result?.translations) {
// let resultJson = JSON.parse(result.json); // let resultJson = JSON.parse(result.json);
// resultJson['privTranslationPhrase']['Translation']['Translations'].forEach( // resultJson['privTranslationPhrase']['Translation']['Translations'].forEach(
@ -89,12 +86,11 @@ export default function VoiceInput({
setUserInput( setUserInput(
tempUserInput + tempUserInput +
voiceInputText.replace(/(.*)(^|[\r\n]+).*\[\.\.\.][\r\n]+/, "$1$2") + voiceInputText.replace(/(.*)(^|[\r\n]+).*\[\.\.\.][\r\n]+/, "$1$2") +
`${result.text} [...]`, `${result.text ?? ""} [...]`,
); );
setVoiceInputText( setVoiceInputText(
voiceInputText.replace(/(.*)(^|[\r\n]+).*\[\.\.\.][\r\n]+/, "$1$2") + voiceInputText.replace(/(.*)(^|[\r\n]+).*\[\.\.\.][\r\n]+/, "$1$2") +
`${result.text} [...]`, `${result.text ?? ""} [...]`,
); );
} }
@ -130,12 +126,10 @@ export default function VoiceInput({
recognizer.current.recognizeOnceAsync( recognizer.current.recognizeOnceAsync(
(result) => { (result) => {
onRecognizedResult(result); onRecognizedResult(result);
setUserInput(
setUserInput(tempUserInput + voiceInputText ?? "" + `${result.text}`); tempUserInput + (voiceInputText ?? "") + `${result.text ?? ""}`,
// setVoiceInputText(result.text); );
console.log("result", result.text);
setVoiceInputLoading(false); setVoiceInputLoading(false);
// recognizer.close();
}, },
(err) => { (err) => {
console.error("Recognition error: ", err); // 错误处理 console.error("Recognition error: ", err); // 错误处理