让音乐配置变的优雅

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
This commit is contained in:
hgy667 2025-02-22 18:30:57 +08:00
parent b1c4c69c69
commit d7d43b2701
6 changed files with 27 additions and 16 deletions

View file

@ -285,17 +285,3 @@ module_test:
- "你好,请介绍一下你自己" - "你好,请介绍一下你自己"
- "What's the weather like today?" - "What's the weather like today?"
- "请用100字概括量子计算的基本原理和应用前景" - "请用100字概括量子计算的基本原理和应用前景"
# 本地音乐播放配置
music_commands:
#下方可自定义播放音乐指令,也可直接说出想要播放的歌曲名称
- "播放音乐"
- "来点音乐"
- "背景音乐"
- "放首歌"
- "播放歌曲"
- "来点背景音乐"
- "我想听歌"
- "我要听歌"
- "放点音乐"
music_dir: "./music" # 将音乐文件存放在此目录

View file

@ -9,6 +9,7 @@ import traceback
import time import time
import threading import threading
import websockets import websockets
import yaml
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
@ -16,7 +17,18 @@ logger = setup_logging()
class MusicHandler: class MusicHandler:
def __init__(self, config): def __init__(self, config):
self.config = 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.is_playing = False
self.stop_event = threading.Event() self.stop_event = threading.Event()
@ -39,7 +51,7 @@ class MusicHandler:
return True 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): if any(cmd in clean_text for cmd in music_related_keywords):
await self.play_local_music(conn) await self.play_local_music(conn)
return True return True
@ -76,6 +88,7 @@ class MusicHandler:
logger.bind(tag=TAG).error(f"音乐目录不存在: {self.music_dir}") logger.bind(tag=TAG).error(f"音乐目录不存在: {self.music_dir}")
return return
# 确保路径正确性
if specific_file: if specific_file:
music_path = os.path.join(self.music_dir, specific_file) music_path = os.path.join(self.music_dir, specific_file)
if not os.path.exists(music_path): if not os.path.exists(music_path):

12
music/music_config.yaml Normal file
View file

@ -0,0 +1,12 @@
# 本地音乐播放配置
music_commands:
- "播放音乐"
- "来点音乐"
- "背景音乐"
- "放首歌"
- "播放歌曲"
- "来点背景音乐"
- "我想听歌"
- "我要听歌"
- "放点音乐"
music_dir: "./music/mp3" # 音乐文件存放路径