新增直接播放链接的接口

This commit is contained in:
涵曦 2024-06-29 12:41:29 +00:00
parent 6837841872
commit dc49f63a37
5 changed files with 40 additions and 10 deletions

View File

@ -61,7 +61,7 @@ class Config:
"XIAOMUSIC_SEARCH", "ytsearch:" "XIAOMUSIC_SEARCH", "ytsearch:"
) # "bilisearch:" or "ytsearch:" ) # "bilisearch:" or "ytsearch:"
ffmpeg_location: str = os.getenv("XIAOMUSIC_FFMPEG_LOCATION", "./ffmpeg/bin") 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") exclude_dirs: str = os.getenv("XIAOMUSIC_EXCLUDE_DIRS", "@eaDir")
music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10")) music_path_depth: int = int(os.getenv("XIAOMUSIC_MUSIC_PATH_DEPTH", "10"))
disable_httpauth: bool = ( disable_httpauth: bool = (

View File

@ -167,6 +167,13 @@ def downloadjson():
def downloadlog(): def downloadlog():
return send_file(xiaomusic.config.log_file, as_attachment=True) 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): def static_path_handler(filename):
log.debug(filename) log.debug(filename)
log.debug(static_path) log.debug(static_path)

View File

@ -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) { function append_op_button_name(name) {
append_op_button(name, name); append_op_button(name, name);
} }

View File

@ -43,6 +43,12 @@
<button id="play_music_list">播放列表歌曲</button> <button id="play_music_list">播放列表歌曲</button>
<button id="del_music">删除选中歌曲</button> <button id="del_music">删除选中歌曲</button>
<hr>
<div>
<input id="music-url" type="text" placeholder="链接(http://ngcdn001.cnr.cn/live/zgzs/index.m3u8)"></input>
</div>
<button id="playurl">播放链接</button>
<footer> <footer>
<p>Powered by <a href="https://github.com/hanxi/xiaomusic" target="_blank">xiaomusic</a></p> <p>Powered by <a href="https://github.com/hanxi/xiaomusic" target="_blank">xiaomusic</a></p>
</footer> </footer>

View File

@ -287,10 +287,13 @@ class XiaoMusic:
await self.mina_service.text_to_speech(self.device_id, value) await self.mina_service.text_to_speech(self.device_id, value)
except Exception as e: except Exception as e:
self.log.error(f"Execption {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}") self.log.debug(f"do_tts. cur_music:{self.cur_music}")
if self._playing and not self.is_downloading(): if self._playing and not self.is_downloading():
# 继续播放歌曲 # 继续播放歌曲
self.log.info(f"继续播放歌曲")
await self.play() await self.play()
async def do_set_volume(self, value): async def do_set_volume(self, value):
@ -303,7 +306,10 @@ class XiaoMusic:
self.log.error(f"Execption {e}") self.log.error(f"Execption {e}")
async def force_stop_xiaoai(self): 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): def is_downloading(self):
@ -587,7 +593,7 @@ class XiaoMusic:
self.polling_event.clear() # stop polling when processing the question self.polling_event.clear() # stop polling when processing the question
query = new_record.get("query", "").strip() query = new_record.get("query", "").strip()
ctrl_panel = new_record.get("ctrl_panel", False) 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) opvalue, oparg = self.match_cmd(query, ctrl_panel)
@ -633,7 +639,7 @@ class XiaoMusic:
return (opvalue, oparg) return (opvalue, oparg)
if self._playing: if self._playing:
self.log.info("未匹配到指令,自动停止") self.log.info("未匹配到指令,自动停止")
return ("stop", {}) return ("stop", "notts")
return (None, None) return (None, None)
# 判断是否播放下一首歌曲 # 判断是否播放下一首歌曲
@ -667,7 +673,8 @@ class XiaoMusic:
{"startaudioid": audio_id, "music": json.dumps(music)}, {"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: if self.config.use_music_api:
ret = await self._play_by_music_url(self.device_id, url) ret = await self._play_by_music_url(self.device_id, url)
self.log.debug( self.log.debug(
@ -678,6 +685,7 @@ class XiaoMusic:
self.log.debug( self.log.debug(
f"play_url play_by_url {self.config.hardware}. ret:{ret} url:{url}" f"play_url play_by_url {self.config.hardware}. ret:{ret} url:{url}"
) )
return ret
# 播放本地歌曲 # 播放本地歌曲
async def playlocal(self, **kwargs): async def playlocal(self, **kwargs):
@ -704,7 +712,7 @@ class XiaoMusic:
sec, url = await self.get_music_sec_url(name) sec, url = await self.get_music_sec_url(name)
self.log.info(f"播放 {url}") self.log.info(f"播放 {url}")
await self.force_stop_xiaoai() await self.force_stop_xiaoai()
await self.play_url(url) await self.play_url(arg1 = url)
self.log.info("已经开始播放了") self.log.info("已经开始播放了")
# 设置下一首歌曲的播放定时器 # 设置下一首歌曲的播放定时器
await self.set_next_music_timeout(sec) await self.set_next_music_timeout(sec)
@ -809,6 +817,8 @@ class XiaoMusic:
async def stop(self, **kwargs): async def stop(self, **kwargs):
self._playing = False self._playing = False
if kwargs.get("arg1", "") != "notts":
await self.do_tts(f"收到关机口令,再见")
if self._next_timer: if self._next_timer:
self._next_timer.cancel() self._next_timer.cancel()
self.log.info("定时器已取消") self.log.info("定时器已取消")
@ -823,12 +833,12 @@ class XiaoMusic:
async def _do_stop(): async def _do_stop():
await asyncio.sleep(minute * 60) await asyncio.sleep(minute * 60)
try: try:
await self.stop() await self.stop(arg1="notts")
except Exception as e: except Exception as e:
self.log.warning(f"执行出错 {str(e)}\n{traceback.format_exc()}") self.log.warning(f"执行出错 {str(e)}\n{traceback.format_exc()}")
self._stop_timer = asyncio.ensure_future(_do_stop()) 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): async def set_volume(self, **kwargs):
value = kwargs.get("arg1", 0) value = kwargs.get("arg1", 0)
@ -838,7 +848,7 @@ class XiaoMusic:
playing_info = await self.mina_service.player_get_status(self.device_id) playing_info = await self.mina_service.player_get_status(self.device_id)
self.log.debug("get_volume. playing_info:%s", playing_info) self.log.debug("get_volume. playing_info:%s", playing_info)
self._volume = json.loads(playing_info.get("data", {}).get("info", "{}")).get( self._volume = json.loads(playing_info.get("data", {}).get("info", "{}")).get(
"volume", 5 "volume", 0
) )
self.log.info("get_volume. volume:%s", self._volume) self.log.info("get_volume. volume:%s", self._volume)