2026-01-11 18:27:51 +08:00

28 lines
911 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// scripts/voice.js
export function initVoice(panel, onResultCallback) {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) return { supportSpeech: false };
const recognition = new SpeechRecognition();
recognition.lang = "zh-CN";
recognition.continuous = false; // 改为 false确保每次停止都能立即结算
recognition.interimResults = false;
recognition.onresult = (event) => {
const text = event.results[0][0].transcript.trim();
if (text) {
console.log("🎤 识别结果:", text);
onResultCallback(text);
}
};
recognition.onerror = (event) => {
if (event.error !== 'aborted') console.error("语音错误:", event.error);
};
return {
supportSpeech: true,
start: () => { try { recognition.start(); } catch(e){} },
stop: () => { try { recognition.stop(); } catch(e){} }
};
}