1
0
mirror of https://github.com/hanxi/xiaomusic.git synced 2026-03-15 08:13:16 +08:00

feat: 移除第三方设备播放功能

This commit is contained in:
涵曦
2026-01-05 13:16:16 +08:00
parent b436870dcd
commit d00e8a2cbc
8 changed files with 13 additions and 608 deletions

View File

@@ -1,5 +0,0 @@
async def setmyvolume(arg1):
global log, xiaomusic
log.info(f"code1:{arg1}")
did = xiaomusic._cur_did
await xiaomusic.set_myvolume(did, arg1)

View File

@@ -21,7 +21,6 @@ dependencies = [
"python-multipart>=0.0.12",
"requests>=2.32.3",
"sentry-sdk[fastapi]==1.45.1",
"python-socketio>=5.12.1",
"pyjwt>=2.10.1",
"fake-useragent>=2.2.0",
"miservice-fork",

View File

@@ -40,7 +40,7 @@ def main():
from xiaomusic import __version__
from xiaomusic.config import Config
from xiaomusic.httpserver import HttpInit
from xiaomusic.httpserver import socketio_app as HttpApp
from xiaomusic.httpserver import app as HttpApp
from xiaomusic.xiaomusic import XiaoMusic
parser = argparse.ArgumentParser()

View File

@@ -14,7 +14,6 @@ from typing import TYPE_CHECKING, Annotated
from urllib.parse import urlparse
import jwt
import socketio
from fastapi import WebSocket, WebSocketDisconnect
if TYPE_CHECKING:
@@ -128,47 +127,6 @@ app = FastAPI(
openapi_url=None,
)
# 创建Socket.IO实例
sio = socketio.AsyncServer(
async_mode="asgi",
cors_allowed_origins="*", # 允许所有跨域请求,生产环境应限制
)
# 将Socket.IO挂载到FastAPI应用
socketio_app = socketio.ASGIApp(
socketio_server=sio, other_asgi_app=app, socketio_path="/socket.io"
)
# Socket.IO事件处理
@sio.event
async def connect(sid, environ, auth):
global onlines
print(f"客户端连接: {sid}")
onlines.update([sid])
await sio.emit("message", {"data": "欢迎连接"}, room=sid)
@sio.event
async def disconnect(sid):
print(f"客户端断开: {sid}")
onlines.discard(sid)
@sio.on("message")
async def custom_event(sid, data):
log.info(f"收到来自 {sid} 的数据: {data}")
await sio.emit("response", {"action": "切歌", "status": data})
@app.post("/thdaction")
async def thdaction(item: Item):
await sio.emit(
"response",
{"action": item.action, "args": item.args, "status": item.args},
)
return onlines
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许访问的源

Binary file not shown.

View File

@@ -1,499 +0,0 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- PWA必须的配置 -->
<link rel="manifest" href="/manifest.json">
<title>Mini音乐播放器</title>
<style>
/* 新增音量控制样式 */
.volume-control {
position: relative;
align-items: center;
display: flex;
align-items: center;
gap: 10px;
margin-left: 30px;
}
.btn-volume {
background: none;
border: none;
cursor: pointer;
font-size: 20px;
padding: 0;
}
.volume-slider {
width: 100px;
height: 4px;
-webkit-appearance: none;
background: #ddd;
border-radius: 2px;
outline: none;
opacity: 0.8;
transition: opacity 0.2s;
}
.volume-slider:hover {
opacity: 1;
}
/* 自定义滑动条样式 */
.volume-slider::-webkit-slider-thumb {
-webkit-appearance: none;
width: 12px;
height: 12px;
background: #ff4d4d;
border-radius: 50%;
cursor: pointer;
}
body {
background: #f5f5f5;
font-family: Arial, "Microsoft Yahei";
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
/* 播放器容器 */
.player {
background: #fff;
border-radius: 15px;
padding: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
/* 封面图片 */
.cover {
width: 200px;
height: 200px;
border-radius: 10px;
margin: 0 auto 20px;
overflow: hidden;
background: #ddd;
}
.cover img {
width: 100%;
height: 100%;
object-fit: cover;
}
/* 歌曲信息 */
.song-info {
text-align: center;
margin-bottom: 20px;
}
.song-title {
font-size: 18px;
margin: 5px 0;
}
.artist {
color: #666;
font-size: 14px;
}
/* 进度条 */
.progress-container {
width: 100%;
height: 4px;
background: #eee;
border-radius: 2px;
margin: 15px 0;
cursor: pointer;
}
.progress {
height: 100%;
background: #ff4d4d;
width: 0%;
border-radius: 2px;
transition: width 0.1s linear;
}
/* 时间显示 */
.time {
display: flex;
justify-content: space-between;
font-size: 12px;
color: #666;
}
/* 控制按钮 */
.controls {
display: flex;
justify-content: center;
align-items: center;
gap: 30px;
margin-top: 20px;
}
.btn {
cursor: pointer;
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s;
}
.btn-play {
background: #ff4d4d;
color: white;
width: 50px;
height: 50px;
}
.btn:hover {
background: #ff6666;
}
/* 歌曲列表 */
.playlist {
margin-top: 20px;
list-style: none;
padding: 0;
}
.playlist li {
padding: 10px;
cursor: pointer;
border-radius: 5px;
}
.playlist li:hover {
background: #f8f8f8;
}
.playlist li.playing {
background: #ffe5e5;
color: #ff4d4d;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.min.js"></script>
</head>
<body>
<script> // 连接到服务端
const socket = io("/", { transports: ["websocket"] });
socket.on('connect', () => {
console.log('Connected to the server');
socket.emit('my_event', 'Hello, server!');
});
// 接收广播消息
socket.on("response", (data) => { const div = document.createElement("div");
div.textContent = `${data.action}: ${data.status}`;
if( data.action=='play')
{
playlist[0].src=data.args;
playlist[0].artist="小爱在线"
playlist[0].title="小爱在线"
console.log(data.args)
playSong(0) ;
}
if( data.action=='stop')
{
togglePlay()
}
const audio = document.getElementById('audio');
const volumeSlider = document.getElementById('volumeSlider');
if( data.action=='volume')
{
audio.volume=data.args/100
volumeSlider.value=audio.volume
}
if( data.action=='down')
{
audio.volume=audio.volume-0.2
volumeSlider.value=audio.volume
}
if( data.action=='up')
{
audio.volume=audio.volume+0.2
volumeSlider.value=audio.volume
}
document.getElementById("messages").appendChild(div); });
</script>
<div class="player">
<div class="cover">
<img id="cover-image" src="https://tse4-mm.cn.bing.net/th/id/OIP-C.ou-y715Of4TbrAPN-j96sQHaEj?w=309&h=188&c=7&r=0&o=5&pid=1.7" alt="封面">
</div>
<div class="song-info">
<h2 class="song-title" id="song-title">Let Go</h2>
<p class="artist" id="artist">Avril Lavigne</p>
</div>
<div class="progress-container" id="progress-container">
<div class="progress" id="progress"></div>
</div>
<div class="time">
<span id="current-time">00:00</span>
<span id="duration">00:00</span>
</div>
<div class="controls">
<div class="btn btn-prev" onclick="prevSong()"></div>
<div class="btn btn-play" id="playBtn" onclick="togglePlay()"></div>
<div class="btn btn-next" onclick="nextSong()"></div>
</div>
<br>
<!-- 新增音量控制 -->
<div class="volume-control">
<button class="btn-volume" onclick="toggleMute()">🔊</button>
<input type="range"
id="volumeSlider"
class="volume-slider"
min="0"
max="1"
step="0.1"
value="1">
</div>
<ul class="playlist" id="playlist"></ul>
</div>
<audio id="audio" src="" ></audio>
<script>
// 在初始化代码中添加
document.body.addEventListener('touchstart', function initAudio() {
audio.load(); // iOS需要预加载
document.body.removeEventListener('touchstart', initAudio);
});
// 音乐数据
const playlist = [
{
title: "一剪梅",
artist: "未知",
cover: "https://tse4-mm.cn.bing.net/th/id/OIP-C.ou-y715Of4TbrAPN-j96sQHaEj?w=309&h=188&c=7&r=0&o=5&pid=1.7",
src: "/static/3thdplay.mp3"
}
];
const audio = document.getElementById('audio');
const playBtn = document.getElementById('playBtn');
let currentSongIndex = 0;
let isPlaying = false;
let wakeLock = null;
let audioContext = null;
// 在播放前预处理链接
function ensureRangeSupport(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('HEAD', url);
xhr.onload = () => {
if (xhr.getResponseHeader('Accept-Ranges')) {
resolve(url);
} else {
resolve(`${url}?force_range=true`); // 兼容不支持的服务端
}
};
xhr.send();
});
}
// 初始化播放列表
function initPlaylist() {
const playlistEl = document.getElementById('playlist');
playlist.forEach((song, index) => {
const li = document.createElement('li');
li.innerHTML = `${song.title} - ${song.artist}`;
li.onclick = () => playSong(index);
playlistEl.appendChild(li);
});
// 仅加载第一首但不播放
const firstSong = playlist[0];
document.getElementById('song-title').textContent = firstSong.title;
document.getElementById('artist').textContent = firstSong.artist;
document.getElementById('cover-image').src = firstSong.cover;
audio.src = firstSong.src;
}
let isFirstPlay = true;
// 播放歌曲
async function playSong(index) {
currentSongIndex = index;
const song = playlist[index];
const src = await ensureRangeSupport(song.src);
document.getElementById('song-title').textContent = song.title;
document.getElementById('artist').textContent = song.artist;
document.getElementById('cover-image').src = song.cover;
audio.src = src
// 更新播放列表样式
document.querySelectorAll('#playlist li').forEach((li, i) => {
li.classList.toggle('playing', i === index);
});
// 只有当不是首次播放时才自动播放
if (!isFirstPlay) {
audio.play().catch(error => {
console.log('播放请求被阻止:', error);
});
playBtn.textContent = '⏸';
isPlaying = true;
}
}
// 切换播放/暂停
async function togglePlay() {
if (isFirstPlay) {
// 第一次点击时触发用户手势
console.log("---firest play-----")
await audio.play()
.then(() => {
isFirstPlay = false;
isPlaying = true;
playBtn.textContent = '⏸';
// audio.play(); // 暂停等待用户操作
})
.catch(error => {
alert('请点击此处开始播放');
});
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
const source = audioContext.createMediaElementSource(audio);
source.connect(audioContext.destination);
// 请求保持唤醒
console.log('---------init audiocontest')
}
await requestWakeLock();
return;
}
if (isPlaying) {
audio.pause();
playBtn.textContent = '▶';
} else {
await audio.play();
playBtn.textContent = '⏸';
}
isPlaying = !isPlaying;
}
// 下一首
function nextSong() {
currentSongIndex = (currentSongIndex + 1) % playlist.length;
playSong(currentSongIndex);
}
// 上一首
function prevSong() {
currentSongIndex = (currentSongIndex - 1 + playlist.length) % playlist.length;
playSong(currentSongIndex);
}
// 播放进度更新
audio.addEventListener('timeupdate', function() {
const progress = (audio.currentTime / audio.duration) * 100;
document.getElementById('progress').style.width = `${progress}%`;
document.getElementById('current-time').textContent =
formatTime(audio.currentTime);
});
// 总时长显示
audio.addEventListener('loadedmetadata', function() {
document.getElementById('duration').textContent =
formatTime(audio.duration);
});
// 点击进度条跳转
const progressContainer = document.getElementById('progress-container');
progressContainer.addEventListener('click', function(e) {
const width = this.clientWidth;
const clickX = e.offsetX;
audio.currentTime = (clickX / width) * audio.duration;
});
// 格式化时间(秒 → mm:ss
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
seconds = Math.floor(seconds % 60);
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
// 在 JavaScript 中新增以下代码
const volumeSlider = document.getElementById('volumeSlider');
let isMuted = false;
let lastVolume = 1;
// 音量滑动条控制
volumeSlider.addEventListener('input', (e) => {
audio.volume = e.target.value;
if (audio.volume === '0') {
document.querySelector('.btn-volume').textContent = '🔇';
} else {
document.querySelector('.btn-volume').textContent =
audio.volume > 0.5 ? '🔊' : '🔉';
}
isMuted = false; // 取消静音状态
});
// 静音切换
function toggleMute() {
isMuted = !isMuted;
if (isMuted) {
lastVolume = audio.volume;
audio.volume = 0;
volumeSlider.value = 0;
document.querySelector('.btn-volume').textContent = '🔇';
} else {
audio.volume = lastVolume;
volumeSlider.value = lastVolume;
document.querySelector('.btn-volume').textContent =
lastVolume > 0.5 ? '🔊' : '🔉';
}
}
async function requestWakeLock() {
try {
if ('wakeLock' in navigator) {
wakeLock = await navigator.wakeLock.request('screen');
wakeLock.addEventListener('release', () => {
updateStatus('屏幕锁已释放');
});
}
} catch (err) {
console.warn('Wake Lock 错误:', err);
}
// 兼容iOS处理
if (/(iPhone|iPad)/i.test(navigator.userAgent)) {
audio.setAttribute('playsinline', 'true');
audio.setAttribute('webkit-playsinline', 'true');
}
}
// 初始化Service Worker
//console.log('Service Worker 注册成功');
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(() => {
console.log('Service Worker 注册成功');
});
}
// 初始化音量默认1
audio.volume = 1;
// 自动播放下一首
audio.addEventListener('ended', nextSong);
// 初始化
initPlaylist();
// playSong(0);
</script>
<div id="messages"></div>
</body>
</html>

View File

@@ -240,27 +240,6 @@ def traverse_music_directory(directory, depth, exclude_dirs, support_extension):
return result
# 发送给网页3thplay用于三者设备播放
async def thdplay(
action, args="/static/3thdplay.mp3", target="HTTP://192.168.1.10:58090/thdaction"
):
# 接口地址 target,在参数文件指定
data = {"action": action, "args": args}
try:
async with aiohttp.ClientSession() as session:
async with session.post(
target, json=data, timeout=5
) as response: # 增加超时以避免长时间挂起
# 如果响应不是200引发异常
response.raise_for_status()
# 读取响应文本
text = await response.text()
return "[]" not in text
except Exception as e:
log.error(f"Error thdplay: {e}")
return False
async def downloadfile(url):
# 清理和验证URL
# 解析URL

View File

@@ -67,7 +67,6 @@ from xiaomusic.utils import (
parse_str_to_dict,
save_picture_by_base64,
set_music_tag_to_file,
thdplay,
traverse_music_directory,
try_add_access_control_param,
)
@@ -1972,33 +1971,12 @@ class XiaoMusic:
async def get_volume(self, did="", **kwargs):
return await self.devices[did].get_volume()
# 3thdplay.html 的音量设置消息发送 需要配置文件加入自定义指令
# "user_key_word_dict": {
# "音量": "set_myvolume",
# "继续": "stop",
# "大点音": "exec#setmyvolume(\"up\")",
# "小点音": "exec#setmyvolume(\"down\")",
async def set_myvolume(self, did="", arg1=0, **kwargs):
if did not in self.devices:
self.log.info(f"设备 did:{did} 不存在, 不能设置音量")
return
if arg1 == "up":
await thdplay("up", "", self.thdtarget)
elif arg1 == "down":
await thdplay("down", "", self.thdtarget)
else:
volume = chinese_to_number(arg1)
await thdplay("volume", str(volume), self.thdtarget)
# 设置音量
async def set_volume(self, did="", arg1=0, **kwargs):
if did not in self.devices:
self.log.info(f"设备 did:{did} 不存在, 不能设置音量")
return
volume = int(arg1)
await thdplay("volume", str(volume), self.thdtarget)
return await self.devices[did].set_volume(volume)
# 搜索音乐
@@ -2386,23 +2364,19 @@ class XiaoMusicDevice:
sec, url = await self.xiaomusic.get_music_sec_url(name, true_url)
await self.group_force_stop_xiaoai()
self.log.info(f"播放 {url}")
# 有3方设备打开 /static/3thplay.html 通过socketio连接返回true 忽律小爱音箱的播放
online = await thdplay("play", url, self.xiaomusic.thdtarget)
self.log.info(f"IS online {online}")
if not online:
results = await self.group_player_play(url, name)
if all(ele is None for ele in results):
self.log.info(f"播放 {name} 失败. 失败次数: {self._play_failed_cnt}")
await asyncio.sleep(1)
if (
self.isplaying()
and self._last_cmd != "stop"
and self._play_failed_cnt < 10
):
self._play_failed_cnt = self._play_failed_cnt + 1
await self._play_next()
return
results = await self.group_player_play(url, name)
if all(ele is None for ele in results):
self.log.info(f"播放 {name} 失败. 失败次数: {self._play_failed_cnt}")
await asyncio.sleep(1)
if (
self.isplaying()
and self._last_cmd != "stop"
and self._play_failed_cnt < 10
):
self._play_failed_cnt = self._play_failed_cnt + 1
await self._play_next()
return
# 重置播放失败次数
self._play_failed_cnt = 0
@@ -2788,7 +2762,6 @@ class XiaoMusicDevice:
await self.do_tts(self.config.stop_tts_msg)
await asyncio.sleep(3) # 等它说完
# 取消组内所有的下一首歌曲的定时器
await thdplay("stop", "", self.xiaomusic.thdtarget)
self.cancel_group_next_timer()
await self.group_force_stop_xiaoai()
self.log.info("stop now")