From f0e7a813c64a791ca1bc42c8c02a7bf96c97f1a2 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Thu, 11 Dec 2025 21:17:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AF=AD=E9=9F=B3=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E4=B8=AD=E8=BD=AC=E5=9B=9E=E5=A3=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useVoiceChat.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/hooks/useVoiceChat.ts b/src/hooks/useVoiceChat.ts index e9de303..78bfab9 100644 --- a/src/hooks/useVoiceChat.ts +++ b/src/hooks/useVoiceChat.ts @@ -553,6 +553,17 @@ export function useVoiceChat({ const currentTime = audioContext.currentTime; let nextPlayTime = nextPlayTimeRef.current.get(userId) || currentTime; + // 检查播放队列是否堆积过多(说明网络延迟导致音频堆积) + const MAX_QUEUE_DELAY = 0.5; // 最大允许队列延迟 500ms + const queueDelay = nextPlayTime - currentTime; + + if (queueDelay > MAX_QUEUE_DELAY) { + // 播放队列堆积太多,丢弃这个音频包并重置队列 + console.warn(`[VoiceChat] Dropping audio from ${userId} due to queue buildup: ${(queueDelay * 1000).toFixed(0)}ms`); + nextPlayTimeRef.current.set(userId, currentTime); + return; // 丢弃这个音频包 + } + // 如果下一个播放时间已经过去,使用当前时间 if (nextPlayTime < currentTime) { nextPlayTime = currentTime; @@ -703,6 +714,11 @@ export function useVoiceChat({ // 服务器中转事件 const handleAudioChunk = (data: { userId: string; audioData: number[]; sampleRate?: number }) => { + // 过滤掉自己发送的音频,避免回声 + if (data.userId === socket.id) { + return; + } + if (strategy === 'server-only' || !peerConnectionsRef.current.has(data.userId)) { // 只有在服务器中转模式或WebRTC连接失败时才播放服务器中转的音频 playServerRelayAudio(data.userId, data.audioData, data.sampleRate || 16000);