修复音乐播放卡顿和断连的bug

This commit is contained in:
李健豪
2025-03-24 09:28:01 +08:00
parent fe24529fce
commit 49cb98a606
@@ -28,15 +28,15 @@ async def sendAudioMessage(conn, audios, text, text_index=0):
# 播放音频
async def sendAudio(conn, audios):
# 流控参数优化
frame_duration = 62 # 帧时长(毫秒),增加余量
frame_duration = 60 # 帧时长(毫秒),匹配 Opus 编码
start_time = time.perf_counter()
play_position = 0 # 已播放时长(毫秒)
play_position = 0
# 预发送前 n
pre_buffer = min(5, len(audios))
# 预缓冲:发送前 3
pre_buffer = min(3, len(audios))
for i in range(pre_buffer):
await conn.websocket.send(audios[i])
conn.logger.bind(tag=TAG).debug(f"预缓冲帧 {i}")
conn.logger.bind(tag=TAG).debug(f"预缓冲帧 {i}, 时间: {(time.perf_counter() - start_time) * 1000:.2f}ms")
# 正常播放剩余帧
for opus_packet in audios[pre_buffer:]:
@@ -50,15 +50,11 @@ async def sendAudio(conn, audios):
if delay > 0:
await asyncio.sleep(delay)
send_start = time.perf_counter()
await conn.websocket.send(opus_packet)
conn.logger.bind(tag=TAG).debug(f"发送帧,位置: {play_position}ms, 实际间隔: {(time.perf_counter() - current_time) * 1000:.2f}ms")
send_duration = (time.perf_counter() - send_start) * 1000
logger.bind(tag=TAG).debug(f"发送帧,位置: {play_position}ms, 实际间隔: {(time.perf_counter() - current_time) * 1000:.2f}ms, 发送耗时: {send_duration:.2f}ms")
play_position += frame_duration
# 动态调整下次延迟,补偿发送耗时
play_position += frame_duration # 更新播放位置
async def send_tts_message(conn, state, text=None):