Эх сурвалжийг харах

虚拟实验室接入语音识别api并优化样式和性能
古诗词诗词显示宽度样式调整

liyanbo 1 сар өмнө
parent
commit
377be69959

+ 4 - 2
src/views/AIPage/aiGenerate/DialogContent.vue

@@ -1634,8 +1634,10 @@ onUnmounted(() => {
 }
 
 .poem-text {
-  width: rpx(300);
-  height: rpx(90);
+  width: 100%;
+  max-width: rpx(500);
+  max-height: rpx(200);
+  height: auto;
   font-family: 'STKaiti', 'KaiTi', '楷体', 'Ma Shan Zheng', cursive;
   font-size: rpx(20);
   color: black;

+ 38 - 26
src/views/blockly/Blockly.vue

@@ -235,7 +235,6 @@ const showLampPreview = ref(true);
 const isRecording = ref(false);
 const recordingCountdown = ref(10);
 let countdownInterval = null;
-let recognition = null;
 
 // 语音输入组件
 const voiceInputRef = ref(null);
@@ -257,9 +256,14 @@ const toggleLight = () => {
 };
 
 // 语音识别结果处理
-const handleVoiceRecognized = (text) => {
-  console.log("语音识别结果:", text);
-  voiceResult.value = text;
+const handleVoiceRecognized = (data) => {
+  if (isVoiceRecording.value) {
+    // 在同一次录音过程中,实时更新语音结果
+    voiceResult.value = data.processedText;
+  } else {
+    // 在录音结束时,将最终的语音内容保存
+    voiceResult.value = data.processedText;
+  }
 };
 
 // 录音状态变化处理
@@ -471,8 +475,7 @@ const aiService = {
   console.log("语音识别开始");
     // 前端语音采集
     const recognitionResult = await this.captureVoice(language, promptText);
-    console.log("-============-1-",recognitionResult);
-    
+
     return recognitionResult || "";
   }, '语音识别失败'),
 
@@ -495,6 +498,9 @@ const aiService = {
         return;
       }
 
+      // 添加标志,确保只resolve一次
+      let resolved = false;
+
       // 直接调用组件的toggleSpeechInput方法开始录音
       voiceInputRef.value.toggleSpeechInput();
 
@@ -507,9 +513,12 @@ const aiService = {
         }
         // 等待一小段时间确保结果已处理
         setTimeout(() => {
-          showStatus("语音识别完成");
-          endRecordingStatus();
-          resolve(voiceResult.value || "");
+          if (!resolved) {
+            resolved = true;
+            showStatus("语音识别完成");
+            endRecordingStatus();
+            resolve(voiceResult.value || "");
+          }
         }, 500);
       }, 10000);
 
@@ -521,24 +530,30 @@ const aiService = {
         
         // 只有当录音已经开始过,并且现在已经停止时才返回结果
         if (isVoiceRecording.value === false && checkCount > 2) {
-          clearInterval(checkRecordingStatus);
-          clearTimeout(timeoutId);
-          showStatus("语音识别完成");
-          endRecordingStatus();
-          resolve(voiceResult.value || "");
+          if (!resolved) {
+            resolved = true;
+            clearInterval(checkRecordingStatus);
+            clearTimeout(timeoutId);
+            showStatus("语音识别完成");
+            endRecordingStatus();
+            resolve(voiceResult.value || "");
+          }
         }
         
         // 防止无限循环
         if (checkCount >= maxChecks) {
-          clearInterval(checkRecordingStatus);
-          clearTimeout(timeoutId);
-          // 停止录音
-          if (isVoiceRecording.value) {
-            voiceInputRef.value.toggleSpeechInput();
+          if (!resolved) {
+            resolved = true;
+            clearInterval(checkRecordingStatus);
+            clearTimeout(timeoutId);
+            // 停止录音
+            if (isVoiceRecording.value) {
+              voiceInputRef.value.toggleSpeechInput();
+            }
+            showStatus("语音识别完成");
+            endRecordingStatus();
+            resolve(voiceResult.value || "");
           }
-          showStatus("语音识别完成");
-          endRecordingStatus();
-          resolve(voiceResult.value || "");
         }
       }, 500);
     });
@@ -1181,10 +1196,7 @@ onUnmounted(() => {
   // 停止所有轮询
   pollingManager.stopAll();
 
-  // 关闭语音识别(如果正在进行)
-  if (recognition) {
-    recognition.stop();
-  }
+  
 
   // 清理工作区
   if (workspace) {

+ 29 - 21
src/views/blockly/Blockly2.vue

@@ -253,7 +253,6 @@ const showLampPreview = ref(true);
 const isRecording = ref(false);
 const recordingCountdown = ref(10);
 let countdownInterval = null;
-let recognition = null;
 // 语音输入状态跟踪
 const isVoiceRecording = ref(false); // 当前是否正在录音
 const voiceRecognizedText = ref(""); // 实时语音识别结果
@@ -530,6 +529,9 @@ const aiService = {
         return;
       }
 
+      // 添加标志,确保只resolve一次
+      let resolved = false;
+
       // 直接调用组件的toggleSpeechInput方法开始录音
       voiceInputRef.value.toggleSpeechInput();
 
@@ -542,9 +544,12 @@ const aiService = {
         }
         // 等待一小段时间确保结果已处理
         setTimeout(() => {
-          showStatus("语音识别完成");
-          endRecordingStatus();
-          resolve(voiceResult.value || "");
+          if (!resolved) {
+            resolved = true;
+            showStatus("语音识别完成");
+            endRecordingStatus();
+            resolve(voiceResult.value || "");
+          }
         }, 500);
       }, 10000);
 
@@ -556,24 +561,30 @@ const aiService = {
         
         // 只有当录音已经开始过,并且现在已经停止时才返回结果
         if (isVoiceRecording.value === false && checkCount > 2) {
-          clearInterval(checkRecordingStatus);
-          clearTimeout(timeoutId);
-          showStatus("语音识别完成");
-          endRecordingStatus();
-          resolve(voiceResult.value || "");
+          if (!resolved) {
+            resolved = true;
+            clearInterval(checkRecordingStatus);
+            clearTimeout(timeoutId);
+            showStatus("语音识别完成");
+            endRecordingStatus();
+            resolve(voiceResult.value || "");
+          }
         }
         
         // 防止无限循环
         if (checkCount >= maxChecks) {
-          clearInterval(checkRecordingStatus);
-          clearTimeout(timeoutId);
-          // 停止录音
-          if (isVoiceRecording.value) {
-            voiceInputRef.value.toggleSpeechInput();
+          if (!resolved) {
+            resolved = true;
+            clearInterval(checkRecordingStatus);
+            clearTimeout(timeoutId);
+            // 停止录音
+            if (isVoiceRecording.value) {
+              voiceInputRef.value.toggleSpeechInput();
+            }
+            showStatus("语音识别完成");
+            endRecordingStatus();
+            resolve(voiceResult.value || "");
           }
-          showStatus("语音识别完成");
-          endRecordingStatus();
-          resolve(voiceResult.value || "");
         }
       }, 500);
     });
@@ -1430,10 +1441,7 @@ onUnmounted(() => {
   // 停止所有轮询
   pollingManager.stopAll();
 
-  // 关闭语音识别(如果正在进行)
-  if (recognition) {
-    recognition.stop();
-  }
+  
 
   // 清理工作区
   if (workspace) {