feat: 对话语音输入的问题修复调整
This commit is contained in:
@@ -73,7 +73,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, computed, watch, nextTick, onMounted, defineExpose } from "vue";
|
import { ref, computed, watch, nextTick, onMounted, defineExpose, onUnmounted } from "vue";
|
||||||
import RecordingWaveBtn from "@/components/Speech/RecordingWaveBtn.vue";
|
import RecordingWaveBtn from "@/components/Speech/RecordingWaveBtn.vue";
|
||||||
import { getCurrentConfig } from "@/constant/base";
|
import { getCurrentConfig } from "@/constant/base";
|
||||||
|
|
||||||
@@ -103,6 +103,32 @@ const isFocused = ref(false);
|
|||||||
const keyboardHeight = ref(0);
|
const keyboardHeight = ref(0);
|
||||||
const isVoiceMode = ref(false);
|
const isVoiceMode = ref(false);
|
||||||
const visibleWaveBtn = ref(false);
|
const visibleWaveBtn = ref(false);
|
||||||
|
const isRecording = ref(false);
|
||||||
|
let watchDogTimer = null;
|
||||||
|
|
||||||
|
const resetUI = () => {
|
||||||
|
isRecording.value = false;
|
||||||
|
visibleWaveBtn.value = false;
|
||||||
|
try {
|
||||||
|
if (recordingWaveBtnRef.value) {
|
||||||
|
recordingWaveBtnRef.value.stopAnimation();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("resetUI stopAnimation error", e);
|
||||||
|
}
|
||||||
|
if (watchDogTimer) {
|
||||||
|
clearTimeout(watchDogTimer);
|
||||||
|
watchDogTimer = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const startWatchDog = (timeout = 10000) => {
|
||||||
|
if (watchDogTimer) clearTimeout(watchDogTimer);
|
||||||
|
watchDogTimer = setTimeout(() => {
|
||||||
|
console.warn("recording watchdog triggered, forcing UI reset");
|
||||||
|
resetUI();
|
||||||
|
}, timeout);
|
||||||
|
};
|
||||||
|
|
||||||
// 保持和父组件同步
|
// 保持和父组件同步
|
||||||
watch(
|
watch(
|
||||||
@@ -127,8 +153,9 @@ const toggleVoiceMode = () => {
|
|||||||
|
|
||||||
// 处理语音按钮长按开始
|
// 处理语音按钮长按开始
|
||||||
const handleVoiceTouchStart = () => {
|
const handleVoiceTouchStart = () => {
|
||||||
|
try {
|
||||||
manager.start({ lang: "zh_CN" });
|
manager.start({ lang: "zh_CN" });
|
||||||
|
isRecording.value = true;
|
||||||
visibleWaveBtn.value = true;
|
visibleWaveBtn.value = true;
|
||||||
|
|
||||||
// 启动音频条动画
|
// 启动音频条动画
|
||||||
@@ -137,32 +164,54 @@ const handleVoiceTouchStart = () => {
|
|||||||
recordingWaveBtnRef.value.startAnimation();
|
recordingWaveBtnRef.value.startAnimation();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
startWatchDog(10000);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("record start error:", err);
|
||||||
|
// 保底清理
|
||||||
|
resetUI();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 处理语音按钮长按结束
|
// 处理语音按钮长按结束
|
||||||
const handleVoiceTouchEnd = () => {
|
const handleVoiceTouchEnd = () => {
|
||||||
manager.stop();
|
// 如果本地状态不是录音中,也确保 UI 恢复
|
||||||
|
if (!isRecording.value) {
|
||||||
// 停止音频条动画
|
|
||||||
if (recordingWaveBtnRef.value) {
|
if (recordingWaveBtnRef.value) {
|
||||||
recordingWaveBtnRef.value.stopAnimation();
|
recordingWaveBtnRef.value.stopAnimation();
|
||||||
}
|
}
|
||||||
|
|
||||||
visibleWaveBtn.value = false;
|
visibleWaveBtn.value = false;
|
||||||
|
if (watchDogTimer) {
|
||||||
|
clearTimeout(watchDogTimer);
|
||||||
|
watchDogTimer = null;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
manager.stop();
|
||||||
|
} catch (err) {
|
||||||
|
console.error("record stop error:", err);
|
||||||
|
} finally {
|
||||||
|
// 无论 stop 是否抛错,都保证 UI 恢复
|
||||||
|
resetUI();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 处理发送原语音
|
// 处理发送原语音
|
||||||
const initRecord = () => {
|
const initRecord = () => {
|
||||||
manager.onRecognize = (res) => {
|
manager.onRecognize = (res) => {
|
||||||
let text = res.result;
|
let text = res.result || "";
|
||||||
inputMessage.value = text;
|
inputMessage.value = text;
|
||||||
};
|
};
|
||||||
// 识别结束事件
|
// 识别结束事件
|
||||||
manager.onStop = (res) => {
|
manager.onStop = (res) => {
|
||||||
console.log(res, 37);
|
console.log(res, 37);
|
||||||
let text = res.result;
|
let text = (res && res.result) || "";
|
||||||
|
|
||||||
if (text == "") {
|
// 保证 UI 恢复(防止未走 handleVoiceTouchEnd)
|
||||||
|
resetUI();
|
||||||
|
|
||||||
|
if (text === "") {
|
||||||
console.log("没有说话");
|
console.log("没有说话");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -171,6 +220,12 @@ const initRecord = () => {
|
|||||||
// 在语音识别完成后发送消息
|
// 在语音识别完成后发送消息
|
||||||
emit("send", text);
|
emit("send", text);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 错误处理,确保 UI 重置
|
||||||
|
manager.onError = (err) => {
|
||||||
|
console.error("record manager error", err);
|
||||||
|
resetUI();
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// 监听键盘高度变化
|
// 监听键盘高度变化
|
||||||
@@ -188,6 +243,18 @@ onMounted(() => {
|
|||||||
initRecord();
|
initRecord();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
try {
|
||||||
|
manager.stop && manager.stop();
|
||||||
|
} catch (e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
manager.onRecognize = null;
|
||||||
|
manager.onStop = null;
|
||||||
|
manager.onError = null;
|
||||||
|
resetUI();
|
||||||
|
});
|
||||||
|
|
||||||
const sendMessage = () => {
|
const sendMessage = () => {
|
||||||
if (props.isSessionActive) {
|
if (props.isSessionActive) {
|
||||||
// 如果会话进行中,调用停止请求函数
|
// 如果会话进行中,调用停止请求函数
|
||||||
|
|||||||
Reference in New Issue
Block a user