Skip to content

Commit

Permalink
fix: #81 修复播放列表没有继续播放上次播放的歌曲,并把随机播放,全部循环,单曲循环状态落地
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jul 7, 2024
1 parent 5aff72d commit 2da12e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class Config:
enable_force_stop: bool = (
os.getenv("XIAOMUSIC_ENABLE_FORCE_STOP", "false").lower() == "true"
)
play_type: int = int(os.getenv("XIAOMUSIC_PLAY_TYPE", "2"))

def append_keyword(self, keys, action):
for key in keys.split(","):
Expand Down
35 changes: 28 additions & 7 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time
import traceback
import urllib.parse
from dataclasses import asdict
from logging.handlers import RotatingFileHandler
from pathlib import Path

Expand Down Expand Up @@ -64,8 +65,6 @@ def __init__(self, config: Config):

# 下载对象
self.download_proc = None
# 单曲循环,全部循环
self.play_type = PLAY_TYPE_RND
self.cur_music = ""
self._next_timer = None
self._timeout = 0
Expand Down Expand Up @@ -102,6 +101,8 @@ def __init__(self, config: Config):
self.log.info(f"Startup OK. {debug_config}")

def init_config(self):
# 单曲循环,全部循环
self.play_type = self.config.play_type
self.music_path = self.config.music_path
self.conf_path = self.config.conf_path
if not self.conf_path:
Expand Down Expand Up @@ -854,18 +855,21 @@ async def play_next(self, **kwargs):
# 单曲循环
async def set_play_type_one(self, **kwargs):
self.play_type = PLAY_TYPE_ONE
self.save_play_type()
await self.do_tts("已经设置为单曲循环")

# 全部循环
async def set_play_type_all(self, **kwargs):
self.play_type = PLAY_TYPE_ALL
self._gen_play_list()
self.save_play_type()
await self.do_tts("已经设置为全部循环")

# 随机播放
async def random_play(self, **kwargs):
self.play_type = PLAY_TYPE_RND
self._gen_play_list()
self.save_play_type()
await self.do_tts("已经设置为随机播放")

# 刷新列表
Expand Down Expand Up @@ -897,7 +901,8 @@ def find_real_music_list_name(self, list_name):
if real_name:
self.log.info(f"根据【{list_name}】找到播放列表【{real_name}】")
list_name = real_name
self.log.info(f"没找到播放列表【{list_name}】")
else:
self.log.info(f"没找到播放列表【{list_name}】")
return list_name

# 播放一个播放列表
Expand All @@ -917,8 +922,6 @@ async def play_music_list(self, **kwargs):
music_name = ""
if len(parts) > 1:
music_name = parts[1]
else:
music_name = self.get_next_music()
await self.play(arg1=music_name)

async def stop(self, **kwargs):
Expand Down Expand Up @@ -1014,12 +1017,30 @@ def try_init_setting(self):

# 保存配置并重新启动
async def saveconfig(self, data):
# 配置文件落地
self.do_saveconfig(data)
# 更新配置
self.update_config_from_setting(data)
# 重新初始化
await self.call_main_thread_function(self.reinit)

# 配置文件落地
def do_saveconfig(self, data):
# 默认暂时配置保存到 music 目录下
filename = self.getsettingfile()
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
self.update_config_from_setting(data)
await self.call_main_thread_function(self.reinit)

# 把当前配置落地
def save_cur_config(self):
data = asdict(self.config)
self.do_saveconfig(data)
self.log.info("save_cur_config ok")

# 播放类型落地
def save_play_type(self):
self.config.play_type = self.play_type
self.save_cur_config()

def update_config_from_setting(self, data):
# 兼容旧配置:一段时间后清理这里的旧代码
Expand Down

0 comments on commit 2da12e1

Please sign in to comment.