Skip to content

Commit

Permalink
fix: 尝试修复延迟问题,修复播放停止不了的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jul 30, 2024
1 parent 6e98b5e commit ff06958
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
4 changes: 3 additions & 1 deletion xiaomusic/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ async def do_cmd(data: DidCmd):
return {"ret": "Did not exist"}

if len(cmd) > 0:
asyncio.create_task(xiaomusic.do_check_cmd(did=did, query=cmd))
await xiaomusic.cancel_all_tasks()
task = asyncio.create_task(xiaomusic.do_check_cmd(did=did, query=cmd))
xiaomusic.append_running_task(task)
return {"ret": "OK"}
return {"ret": "Unknow cmd"}

Expand Down
49 changes: 32 additions & 17 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self, config: Config):
self._all_radio = {} # 电台列表
self.music_list = {} # 播放列表 key 为目录名, value 为 play_list
self.devices = {} # key 为 did
self.running_task = []

# 初始化配置
self.init_config()
Expand Down Expand Up @@ -309,10 +310,10 @@ def _get_last_query(self, device_id, data):

def get_filename(self, name):
if name not in self.all_music:
self.log.debug(f"get_filename not in. name:{name}")
self.log.info(f"get_filename not in. name:{name}")
return ""
filename = self.all_music[name]
self.log.debug(f"try get_filename. filename:{filename}")
self.log.info(f"try get_filename. filename:{filename}")
if os.path.exists(filename):
return filename
return ""
Expand Down Expand Up @@ -343,6 +344,7 @@ def is_web_music(self, name):
async def get_music_sec_url(self, name):
sec = 0
url = self.get_music_url(name)
self.log.info(f"get_music_sec_url. name:{name} url:{url}")
if self.is_web_radio_music(name):
self.log.info("电台不会有播放时长")
return 0, url
Expand All @@ -354,6 +356,7 @@ async def get_music_sec_url(self, name):
self.log.info(f"网络歌曲 {name} : {origin_url} {url} 的时长 {sec} 秒")
else:
filename = self.get_filename(name)
self.log.info(f"get_music_sec_url. name:{name} filename:{filename}")
duration = await get_local_music_duration(filename)
sec = math.ceil(duration)
self.log.info(f"本地歌曲 {name} : {filename} {url} 的时长 {sec} 秒")
Expand All @@ -365,7 +368,7 @@ async def get_music_sec_url(self, name):
def get_music_url(self, name):
if self.is_web_music(name):
url = self.all_music[name]
self.log.debug(f"get_music_url web music. name:{name}, url:{url}")
self.log.info(f"get_music_url web music. name:{name}, url:{url}")
return url

filename = self.get_filename(name)
Expand All @@ -383,7 +386,7 @@ def get_music_url(self, name):
filename = filename[len(self.config.music_path) :]
if filename.startswith("/"):
filename = filename[1:]
self.log.debug(f"get_music_url local music. name:{name}, filename:{filename}")
self.log.info(f"get_music_url local music. name:{name}, filename:{filename}")
encoded_name = urllib.parse.quote(filename)
return f"http://{self.hostname}:{self.public_port}/music/{encoded_name}"

Expand Down Expand Up @@ -505,6 +508,19 @@ async def do_check_cmd(self, did="", query="", ctrl_panel=True, **kwargs):
except Exception as e:
self.log.exception(f"Execption {e}")

def append_running_task(self, task):
self.running_task.append(task)

async def cancel_all_tasks(self):
if len(self.running_task) == 0:
self.log.info("cancel_all_tasks no task")
return
for task in self.running_task:
self.log.info(f"cancel_all_tasks {task}")
task.cancel()
await asyncio.gather(*self.running_task, return_exceptions=True)
self.running_task = []

async def check_replay(self, did):
return await self.devices[did].check_replay()

Expand Down Expand Up @@ -916,7 +932,7 @@ async def _playmusic(self, name):
self.log.info(f"播放 {name} 失败")
await asyncio.sleep(1)
if self.isplaying() and self._last_cmd != "stop":
await self._play_next()
await self._play_next()
return

self.log.info(f"【{name}】已经开始播放了")
Expand Down Expand Up @@ -1125,21 +1141,19 @@ async def play_one_url(self, device_id, url):

# 设置下一首歌曲的播放定时器
async def set_next_music_timeout(self, sec):
if self._next_timer:
self._next_timer.cancel()
self.log.info("旧定时器已取消")

self.cancel_next_timer()
self._timeout = sec

async def _do_next():
await asyncio.sleep(self._timeout)
try:
self.log.info("定时器时间到了")
await self.play_next()
self._next_timer = None
await self._play_next()
except Exception as e:
self.log.error(f"Execption {e}")

self._next_timer = asyncio.ensure_future(_do_next())
self._next_timer = asyncio.create_task(_do_next())
self.log.info(f"{sec} 秒后将会播放下一首歌曲")

async def set_volume(self, volume: int):
Expand Down Expand Up @@ -1179,10 +1193,8 @@ async def stop(self, arg1=""):
self._playing = False
if arg1 != "notts":
await self.do_tts(self.config.stop_tts_msg)
if self._next_timer:
self._next_timer.cancel()
self._next_timer = None
self.log.info("定时器已取消")
# 取消组内所有的下一首歌曲的定时器
self.cancel_group_next_timer()
await self.group_force_stop_xiaoai()
self.log.info("stop now")

Expand All @@ -1207,13 +1219,14 @@ async def _do_stop():
except Exception as e:
self.log.exception(f"Execption {e}")

self._stop_timer = asyncio.ensure_future(_do_stop())
self._stop_timer = asyncio.create_task(_do_stop())
await self.do_tts(f"收到,{minute}分钟后将关机")

def cancel_next_timer(self):
if self._next_timer:
self._next_timer.cancel()
self.log.info("下一曲定时器已取消")
self.log.info(f"下一曲定时器已取消 {self.device_id}")
self._next_timer = None

def cancel_group_next_timer(self):
devices = self.xiaomusic.get_group_devices(self.group_name)
Expand All @@ -1228,10 +1241,12 @@ def cancel_all_timer(self):
self.log.info("in cancel_all_timer")
if self._next_timer:
self._next_timer.cancel()
self._next_timer = None
self.log.info("cancel_all_timer _next_timer.cancel")

if self._stop_timer:
self._stop_timer.cancel()
self._stop_timer = None
self.log.info("cancel_all_timer _stop_timer.cancel")

@classmethod
Expand Down

0 comments on commit ff06958

Please sign in to comment.