diff --git a/src/pages/ReportEditor.tsx b/src/pages/ReportEditor.tsx index e03e7a7..6cdf32a 100644 --- a/src/pages/ReportEditor.tsx +++ b/src/pages/ReportEditor.tsx @@ -925,13 +925,14 @@ export default function ReportEditor() { const toggleListening = async () => { if (isListening) { setIsListening(false); + // 1. 发送结束帧告诉服务器录音结束 if (xfWsRef.current && xfWsRef.current.readyState === WebSocket.OPEN) { try { const endFrame = { data: { status: 2, format: 'audio/L16;rate=16000', encoding: 'raw', audio: '' } }; xfWsRef.current.send(JSON.stringify(endFrame)); } catch {} } - if (xfWsRef.current) { try { xfWsRef.current.close(); } catch {} xfWsRef.current = null; } + // 2. 仅关闭本地麦克风收音,保留 WebSocket 等待服务器返回最终结果 if (xfAudioContextRef.current) { try { xfAudioContextRef.current.close(); } catch {} xfAudioContextRef.current = null; } if (xfMediaStreamRef.current) { xfMediaStreamRef.current.getTracks().forEach(t => t.stop()); xfMediaStreamRef.current = null; } return; @@ -961,7 +962,10 @@ export default function ReportEditor() { const pcmBuffer = floatTo16BitPCM(inputData); const base64Audio = arrayBufferToBase64(pcmBuffer); const frame: any = { data: { status: frameStatus, format: 'audio/L16;rate=16000', encoding: 'raw', audio: base64Audio } }; - if (frameStatus === 0) { frame.common = { app_id: xfConfig.appId }; frame.business = { language: 'zh_cn', domain: 'iat', accent: 'mandarin', dwa: 'wpgs' }; } + if (frameStatus === 0) { + frame.common = { app_id: xfConfig.appId }; + frame.business = { language: 'zh_cn', domain: 'iat', accent: 'mandarin' }; + } ws.send(JSON.stringify(frame)); frameStatus = 1; }; @@ -989,6 +993,12 @@ export default function ReportEditor() { if (jsonData.data.result.ls) { transcript += seg; setChatInput(transcript); } else { setChatInput(transcript + seg); } } + // 当接收到服务端的最终状态码 status === 2 时,才彻底断开 websocket + if (jsonData.data?.status === 2) { + ws.close(); + xfWsRef.current = null; + setIsListening(false); + } } catch {} }; ws.onerror = () => { alert('讯飞语音连接失败'); setIsListening(false); };