From dc49f63a3775f62edbe27ed3c260a9be0a7782e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sat, 29 Jun 2024 12:41:29 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=9B=B4=E6=8E=A5=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E9=93=BE=E6=8E=A5=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/config.py | 2 +- xiaomusic/httpserver.py | 7 +++++++ xiaomusic/static/app.js | 7 +++++++ xiaomusic/static/index.html | 6 ++++++ xiaomusic/xiaomusic.py | 28 +++++++++++++++++++--------- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/xiaomusic/config.py b/xiaomusic/config.py index 5b87f30..940b281 100644 --- a/xiaomusic/config.py +++ b/xiaomusic/config.py @@ -61,7 +61,7 @@ class Config: "XIAOMUSIC_SEARCH", "ytsearch:" ) # "bilisearch:" or "ytsearch:" ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin") - active_cmd: str = os.getenv("XIAOMUSIC_ACTIVE_CMD", "play,random_play,playlocal,play_music_list") + active_cmd: str = os.getenv("XIAOMUSIC_ACTIVE_CMD", "play,random_play,playlocal,play_music_list,stop") exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir") music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10")) disable_httpauth: bool = ( diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index c5bd28c..89906bf 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -167,6 +167,13 @@ def downloadjson(): def downloadlog(): return send_file(xiaomusic.config.log_file, as_attachment=True) +@app.route("/playurl", methods=["GET"]) +@auth.login_required +async def playurl(): + url = request.args.get('url') + log.info(f"play_url:{url}") + return await xiaomusic.call_main_thread_function(xiaomusic.play_url, arg1=url) + def static_path_handler(filename): log.debug(filename) log.debug(static_path) diff --git a/xiaomusic/static/app.js b/xiaomusic/static/app.js index 0ee9846..7628109 100644 --- a/xiaomusic/static/app.js +++ b/xiaomusic/static/app.js @@ -82,6 +82,13 @@ $(function(){ } }); + $("#playurl").on("click", () => { + var url = $("#music-url").val(); + $.get(`/playurl?url=${url}`, function(data, status) { + console.log(data); + }); + }); + function append_op_button_name(name) { append_op_button(name, name); } diff --git a/xiaomusic/static/index.html b/xiaomusic/static/index.html index 15b2aba..7fe979a 100644 --- a/xiaomusic/static/index.html +++ b/xiaomusic/static/index.html @@ -43,6 +43,12 @@ +
+
+ +
+ + diff --git a/xiaomusic/xiaomusic.py b/xiaomusic/xiaomusic.py index 81a523d..cc471f1 100644 --- a/xiaomusic/xiaomusic.py +++ b/xiaomusic/xiaomusic.py @@ -287,10 +287,13 @@ class XiaoMusic: await self.mina_service.text_to_speech(self.device_id, value) except Exception as e: self.log.error(f"Execption {e}") - + # 最大等8秒 + sec = min(8, int(len(value)/3.3)) + await asyncio.sleep(sec) self.log.debug(f"do_tts. cur_music:{self.cur_music}") if self._playing and not self.is_downloading(): # 继续播放歌曲 + self.log.info(f"继续播放歌曲") await self.play() async def do_set_volume(self, value): @@ -303,7 +306,10 @@ class XiaoMusic: self.log.error(f"Execption {e}") async def force_stop_xiaoai(self): - await self.mina_service.player_stop(self.device_id) + ret = await self.mina_service.player_pause(self.device_id) + self.log.debug(f"force_stop_xiaoai player_pause ret:{ret}") + ret = await self.mina_service.player_stop(self.device_id) + self.log.debug(f"force_stop_xiaoai player_stop ret:{ret}") # 是否在下载中 def is_downloading(self): @@ -587,7 +593,7 @@ class XiaoMusic: self.polling_event.clear() # stop polling when processing the question query = new_record.get("query", "").strip() ctrl_panel = new_record.get("ctrl_panel", False) - self.log.debug("收到消息:%s 控制面板:%s", query, ctrl_panel) + self.log.info("收到消息:%s 控制面板:%s", query, ctrl_panel) # 匹配命令 opvalue, oparg = self.match_cmd(query, ctrl_panel) @@ -633,7 +639,7 @@ class XiaoMusic: return (opvalue, oparg) if self._playing: self.log.info("未匹配到指令,自动停止") - return ("stop", {}) + return ("stop", "notts") return (None, None) # 判断是否播放下一首歌曲 @@ -667,7 +673,8 @@ class XiaoMusic: {"startaudioid": audio_id, "music": json.dumps(music)}, ) - async def play_url(self, url): + async def play_url(self, **kwargs): + url = kwargs.get("arg1", "") if self.config.use_music_api: ret = await self._play_by_music_url(self.device_id, url) self.log.debug( @@ -678,6 +685,7 @@ class XiaoMusic: self.log.debug( f"play_url play_by_url {self.config.hardware}. ret:{ret} url:{url}" ) + return ret # 播放本地歌曲 async def playlocal(self, **kwargs): @@ -704,7 +712,7 @@ class XiaoMusic: sec, url = await self.get_music_sec_url(name) self.log.info(f"播放 {url}") await self.force_stop_xiaoai() - await self.play_url(url) + await self.play_url(arg1 = url) self.log.info("已经开始播放了") # 设置下一首歌曲的播放定时器 await self.set_next_music_timeout(sec) @@ -809,6 +817,8 @@ class XiaoMusic: async def stop(self, **kwargs): self._playing = False + if kwargs.get("arg1", "") != "notts": + await self.do_tts(f"收到关机口令,再见") if self._next_timer: self._next_timer.cancel() self.log.info("定时器已取消") @@ -823,12 +833,12 @@ class XiaoMusic: async def _do_stop(): await asyncio.sleep(minute * 60) try: - await self.stop() + await self.stop(arg1="notts") except Exception as e: self.log.warning(f"执行出错 {str(e)}\n{traceback.format_exc()}") self._stop_timer = asyncio.ensure_future(_do_stop()) - self.log.info(f"{minute}分钟后将关机") + await self.do_tts(f"收到,{minute}分钟后将关机") async def set_volume(self, **kwargs): value = kwargs.get("arg1", 0) @@ -838,7 +848,7 @@ class XiaoMusic: playing_info = await self.mina_service.player_get_status(self.device_id) self.log.debug("get_volume. playing_info:%s", playing_info) self._volume = json.loads(playing_info.get("data", {}).get("info", "{}")).get( - "volume", 5 + "volume", 0 ) self.log.info("get_volume. volume:%s", self._volume)