27 lines
831 B
JavaScript
27 lines
831 B
JavaScript
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;
|
|
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){} }
|
|
};
|
|
} |