From 75e4e56cb3f66cf08fdcede1fa3e8251c399a10b Mon Sep 17 00:00:00 2001 From: admin <572701190@qq.com> Date: Mon, 20 Apr 2026 01:54:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=AF=E9=A3=9E=E8=AF=AD=E9=9F=B3?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E5=81=9C=E6=AD=A2=E5=90=8E=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E6=9C=AA=E5=86=99=E5=85=A5=E8=BE=93=E5=85=A5=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 停止录音时不再立即关闭WebSocket,仅发送结束帧+关闭麦克风 - 移除dwa: wpgs动态修正,避免返回数据结构复杂导致拼接混乱 - ws.onmessage检测到服务端status:2时才彻底断开连接并写入最终文字 --- src/pages/ReportEditor.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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); };