From d7d43b270113611ebd99719c9cb1522f36ea0bf0 Mon Sep 17 00:00:00 2001 From: hgy667 <3088417118@qq.com> Date: Sat, 22 Feb 2025 18:30:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9=E9=9F=B3=E4=B9=90=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=98=E7=9A=84=E4=BC=98=E9=9B=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On branch Music-playback Your branch is up to date with 'origin/Music-playback'. Changes to be committed: modified: config.yaml modified: core/handle/musicHandler.py renamed: "music/\344\270\200\345\277\265\345\215\203\345\271\264_\345\233\275\351\243\216\347\211\210.mp3" -> "music/mp3/\344\270\200\345\277\265\345\215\203\345\271\264_\345\233\275\351\243\216\347\211\210.mp3" renamed: "music/\344\270\255\347\247\213\346\234\210.mp3" -> "music/mp3/\344\270\255\347\247\213\346\234\210.mp3" renamed: "music/\345\273\211\346\263\242\350\200\201\347\237\243\357\274\214\345\260\232\350\203\275\351\245\255\345\220\246.mp3" -> "music/mp3/\345\273\211\346\263\242\350\200\201\347\237\243\357\274\214\345\260\232\350\203\275\351\245\255\345\220\246.mp3" new file: music/music_config.yaml --- config.yaml | 14 -------------- core/handle/musicHandler.py | 17 +++++++++++++++-- music/{ => mp3}/一念千年_国风版.mp3 | Bin music/{ => mp3}/中秋月.mp3 | Bin music/{ => mp3}/廉波老矣,尚能饭否.mp3 | Bin music/music_config.yaml | 12 ++++++++++++ 6 files changed, 27 insertions(+), 16 deletions(-) rename music/{ => mp3}/一念千年_国风版.mp3 (100%) rename music/{ => mp3}/中秋月.mp3 (100%) rename music/{ => mp3}/廉波老矣,尚能饭否.mp3 (100%) create mode 100644 music/music_config.yaml diff --git a/config.yaml b/config.yaml index 9c68f7bf7..a0ce2e1b4 100644 --- a/config.yaml +++ b/config.yaml @@ -285,17 +285,3 @@ module_test: - "你好,请介绍一下你自己" - "What's the weather like today?" - "请用100字概括量子计算的基本原理和应用前景" - -# 本地音乐播放配置 -music_commands: -#下方可自定义播放音乐指令,也可直接说出想要播放的歌曲名称 - - "播放音乐" - - "来点音乐" - - "背景音乐" - - "放首歌" - - "播放歌曲" - - "来点背景音乐" - - "我想听歌" - - "我要听歌" - - "放点音乐" -music_dir: "./music" # 将音乐文件存放在此目录 diff --git a/core/handle/musicHandler.py b/core/handle/musicHandler.py index 6dc115dab..c09f2431e 100644 --- a/core/handle/musicHandler.py +++ b/core/handle/musicHandler.py @@ -9,6 +9,7 @@ import traceback import time import threading import websockets +import yaml TAG = __name__ logger = setup_logging() @@ -16,7 +17,18 @@ logger = setup_logging() class MusicHandler: def __init__(self, config): self.config = config - self.music_dir = os.path.abspath(config.get("music_dir", "./music")) + # 加载独立音乐配置 + music_config_path = os.path.abspath("music/music_config.yaml") + if os.path.exists(music_config_path): + with open(music_config_path, 'r') as f: + self.music_config = yaml.safe_load(f) + else: + self.music_config = {} + logger.bind(tag=TAG).warning("未找到独立音乐配置文件,使用默认配置") + + self.music_dir = os.path.abspath( + self.music_config.get("music_dir", "./music/mp3") # 默认路径修改 + ) self.is_playing = False self.stop_event = threading.Event() @@ -39,7 +51,7 @@ class MusicHandler: return True # 检查是否是通用播放音乐命令 - music_related_keywords = self.config.get("music_commands", []) + music_related_keywords = self.music_config.get("music_commands", []) if any(cmd in clean_text for cmd in music_related_keywords): await self.play_local_music(conn) return True @@ -76,6 +88,7 @@ class MusicHandler: logger.bind(tag=TAG).error(f"音乐目录不存在: {self.music_dir}") return + # 确保路径正确性 if specific_file: music_path = os.path.join(self.music_dir, specific_file) if not os.path.exists(music_path): diff --git a/music/一念千年_国风版.mp3 b/music/mp3/一念千年_国风版.mp3 similarity index 100% rename from music/一念千年_国风版.mp3 rename to music/mp3/一念千年_国风版.mp3 diff --git a/music/中秋月.mp3 b/music/mp3/中秋月.mp3 similarity index 100% rename from music/中秋月.mp3 rename to music/mp3/中秋月.mp3 diff --git a/music/廉波老矣,尚能饭否.mp3 b/music/mp3/廉波老矣,尚能饭否.mp3 similarity index 100% rename from music/廉波老矣,尚能饭否.mp3 rename to music/mp3/廉波老矣,尚能饭否.mp3 diff --git a/music/music_config.yaml b/music/music_config.yaml new file mode 100644 index 000000000..0336f8f8b --- /dev/null +++ b/music/music_config.yaml @@ -0,0 +1,12 @@ +# 本地音乐播放配置 +music_commands: + - "播放音乐" + - "来点音乐" + - "背景音乐" + - "放首歌" + - "播放歌曲" + - "来点背景音乐" + - "我想听歌" + - "我要听歌" + - "放点音乐" +music_dir: "./music/mp3" # 音乐文件存放路径