fix: 讯飞语音识别停止后内容未写入输入框
- 停止录音时不再立即关闭WebSocket,仅发送结束帧+关闭麦克风 - 移除dwa: wpgs动态修正,避免返回数据结构复杂导致拼接混乱 - ws.onmessage检测到服务端status:2时才彻底断开连接并写入最终文字
This commit is contained in:
@@ -925,13 +925,14 @@ export default function ReportEditor() {
|
|||||||
const toggleListening = async () => {
|
const toggleListening = async () => {
|
||||||
if (isListening) {
|
if (isListening) {
|
||||||
setIsListening(false);
|
setIsListening(false);
|
||||||
|
// 1. 发送结束帧告诉服务器录音结束
|
||||||
if (xfWsRef.current && xfWsRef.current.readyState === WebSocket.OPEN) {
|
if (xfWsRef.current && xfWsRef.current.readyState === WebSocket.OPEN) {
|
||||||
try {
|
try {
|
||||||
const endFrame = { data: { status: 2, format: 'audio/L16;rate=16000', encoding: 'raw', audio: '' } };
|
const endFrame = { data: { status: 2, format: 'audio/L16;rate=16000', encoding: 'raw', audio: '' } };
|
||||||
xfWsRef.current.send(JSON.stringify(endFrame));
|
xfWsRef.current.send(JSON.stringify(endFrame));
|
||||||
} catch {}
|
} 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 (xfAudioContextRef.current) { try { xfAudioContextRef.current.close(); } catch {} xfAudioContextRef.current = null; }
|
||||||
if (xfMediaStreamRef.current) { xfMediaStreamRef.current.getTracks().forEach(t => t.stop()); xfMediaStreamRef.current = null; }
|
if (xfMediaStreamRef.current) { xfMediaStreamRef.current.getTracks().forEach(t => t.stop()); xfMediaStreamRef.current = null; }
|
||||||
return;
|
return;
|
||||||
@@ -961,7 +962,10 @@ export default function ReportEditor() {
|
|||||||
const pcmBuffer = floatTo16BitPCM(inputData);
|
const pcmBuffer = floatTo16BitPCM(inputData);
|
||||||
const base64Audio = arrayBufferToBase64(pcmBuffer);
|
const base64Audio = arrayBufferToBase64(pcmBuffer);
|
||||||
const frame: any = { data: { status: frameStatus, format: 'audio/L16;rate=16000', encoding: 'raw', audio: base64Audio } };
|
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));
|
ws.send(JSON.stringify(frame));
|
||||||
frameStatus = 1;
|
frameStatus = 1;
|
||||||
};
|
};
|
||||||
@@ -989,6 +993,12 @@ export default function ReportEditor() {
|
|||||||
if (jsonData.data.result.ls) { transcript += seg; setChatInput(transcript); }
|
if (jsonData.data.result.ls) { transcript += seg; setChatInput(transcript); }
|
||||||
else { setChatInput(transcript + seg); }
|
else { setChatInput(transcript + seg); }
|
||||||
}
|
}
|
||||||
|
// 当接收到服务端的最终状态码 status === 2 时,才彻底断开 websocket
|
||||||
|
if (jsonData.data?.status === 2) {
|
||||||
|
ws.close();
|
||||||
|
xfWsRef.current = null;
|
||||||
|
setIsListening(false);
|
||||||
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
};
|
};
|
||||||
ws.onerror = () => { alert('讯飞语音连接失败'); setIsListening(false); };
|
ws.onerror = () => { alert('讯飞语音连接失败'); setIsListening(false); };
|
||||||
|
|||||||
Reference in New Issue
Block a user