新增随机播放指令

This commit is contained in:
涵曦 2024-01-27 23:00:17 +08:00
parent 2a59d1f69c
commit d06b3cd2a5
2 changed files with 10 additions and 2 deletions

View File

@ -39,6 +39,7 @@ KEY_WORD_DICT = {
"下一首": "play_next",
"单曲循环": "set_play_type_one",
"全部循环": "set_play_type_all",
"随机播放": "random_play",
"关机": "stop",
"停止播放": "stop",
}

View File

@ -86,7 +86,7 @@ class XiaoMusic:
# 下载对象
self.download_proc = None
# 单曲循环,全部循环
self.play_type = PLAY_TYPE_ONE
self.play_type = PLAY_TYPE_ALL
self.cur_music = ""
self._next_timer = None
self._timeout = 0
@ -355,7 +355,7 @@ class XiaoMusic:
# 随机选择一个文件
music_file = random.choice(music_files)
(filename, extension) = os.path.splitext(music_file)
self.log.info(f"随机到歌曲{filename}.{extension}")
self.log.info(f"随机到歌曲{filename}{extension}")
return filename
# 获取文件播放时长
@ -454,6 +454,7 @@ class XiaoMusic:
async def play_next(self, **kwargs):
self.log.info("下一首")
(name, _) = os.path.splitext(os.path.basename(self.cur_music))
self.log.debug("play_next. name:%s, cur_music:%s", name, self.cur_music)
if self.play_type == PLAY_TYPE_ALL or name == "":
name = self.random_music()
if name == "":
@ -471,6 +472,12 @@ class XiaoMusic:
self.play_type = PLAY_TYPE_ALL
await self.do_tts(f"已经设置为全部循环")
# 随机播放
async def random_play(self, **kwargs):
self.play_type = PLAY_TYPE_ALL
await self.do_tts(f"已经设置为全部循环并随机播放")
await self.play_next()
async def stop(self, **kwargs):
if self._next_timer:
self._next_timer.cancel()