mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 00:53:54 +08:00
update:优化音频播放方法
This commit is contained in:
@@ -216,24 +216,16 @@ async def play_local_music(conn, specific_file=None):
|
||||
text = _get_random_play_prompt(selected_music)
|
||||
await send_stt_message(conn, text)
|
||||
conn.dialogue.put(Message(role="assistant", content=text))
|
||||
conn.tts_first_text_index = 0
|
||||
conn.tts_last_text_index = 0
|
||||
|
||||
tts_file = await asyncio.to_thread(conn.tts.to_tts, text)
|
||||
if tts_file is not None and os.path.exists(tts_file):
|
||||
conn.tts_last_text_index = 1
|
||||
opus_packets, _ = conn.tts.audio_to_opus_data(tts_file)
|
||||
conn.audio_play_queue.put((opus_packets, None, 0))
|
||||
os.remove(tts_file)
|
||||
conn.recode_first_last_text(text, 0)
|
||||
future = conn.executor.submit(conn.speak_and_play, None, text, 0)
|
||||
conn.tts_queue.put((future, 0))
|
||||
|
||||
conn.recode_first_last_text(text, 1)
|
||||
future = conn.executor.submit(conn.speak_and_play, music_path, None, 1)
|
||||
conn.tts_queue.put((future, 1))
|
||||
conn.llm_finish_task = True
|
||||
|
||||
if music_path.endswith(".p3"):
|
||||
opus_packets, _ = p3.decode_opus_from_file(music_path)
|
||||
else:
|
||||
opus_packets, _ = conn.tts.audio_to_opus_data(music_path)
|
||||
conn.audio_play_queue.put((opus_packets, None, conn.tts_last_text_index))
|
||||
|
||||
except Exception as e:
|
||||
conn.logger.bind(tag=TAG).error(f"播放音乐失败: {str(e)}")
|
||||
conn.logger.bind(tag=TAG).error(f"详细错误: {traceback.format_exc()}")
|
||||
|
||||
Reference in New Issue
Block a user