This commit is contained in:
jmj committed 2026-11-30 11:58:17 +08:00
1 parent d07ab09fca
commit 0ea21f735a
1 file changed
+132 -35
@@ -561,6 +561,10 @@ body {
.track.playing { background: rgba(34,197,94,.13); border-color: rgba(34,197,94,.38); }
.track.playing .t-name { color: #86efac; }
/* 当前曲目已暂停:琥珀色,区别于播放中的绿色 */
.track.paused { background: rgba(245,158,11,.10); border-color: rgba(245,158,11,.34); }
.track.paused .t-name { color: #fbbf24; }
.t-num {
width: 4vh; height: 4vh;
flex: 0 0 auto;
@@ -571,6 +575,8 @@ body {
color: #7dd3fc;
}
.track.playing .t-num { background: rgba(34,197,94,.2); color: #4ade80; }
.track.paused .t-num { background: rgba(245,158,11,.16); color: #fbbf24; }
.track.paused .t-go { color: #fbbf24; opacity: 1; }
/* 播放中:序号位改为跳动的音量条 */
.t-num-play { padding: 0; }
@@ -723,12 +729,16 @@ body {
loop: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 2l3 3-3 3"/><path d="M3 11V9a4 4 0 0 1 4-4h13"/><path d="M7 22l-3-3 3-3"/><path d="M21 13v2a4 4 0 0 1-4 4H4"/></svg>',
// 随机(交叉箭头)
shuffle: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 3h5v5"/><path d="M4 20 21 3"/><path d="M21 16v5h-5"/><path d="M15 15l6 6"/><path d="M4 4l5 5"/></svg>',
// 唱片(同心圆):正在播放时显示
disc: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="1.6"><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="2.6"/></svg>',
// 耳机:未播放时显示
head: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M4 14v-2a8 8 0 0 1 16 0v2"/><path d="M4 14a2 2 0 0 1 2-2h1v6H6a2 2 0 0 1-2-2z"/><path d="M20 14a2 2 0 0 0-2-2h-1v6h1a2 2 0 0 0 2-2z"/></svg>',
// 单曲循环(带 1 的循环)
single: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 2l3 3-3 3"/><path d="M3 11V9a4 4 0 0 1 4-4h13"/><path d="M7 22l-3-3 3-3"/><path d="M21 13v2a4 4 0 0 1-4 4H4"/><path d="M10.6 15.2l1.8-1.1V11"/></svg>',
// 正在播放:声波
wave: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="currentColor"><rect x="3" y="9" width="2.4" height="6" rx="1.2"/><rect x="7.6" y="6" width="2.4" height="12" rx="1.2"/><rect x="12.2" y="3.5" width="2.4" height="17" rx="1.2"/><rect x="16.8" y="7.5" width="2.4" height="9" rx="1.2"/></svg>',
// 普通:播放箭头
arrow: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="currentColor"><path d="M8 5.2v13.6L19 12z"/></svg>',
single: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 2l3 3-3 3"/><path d="M3 11V9a4 4 0 0 1 4-4h13"/><path d="M7 22l-3-3 3-3"/><path d="M21 13v2a4 4 0 0 1-4 4H4"/><path d="M10.6 15.2l1.8-1.1V11"/></svg>'
arrow: '<svg viewBox="0 0 24 24" width="1em" height="1em" fill="currentColor"><path d="M8 5.2v13.6L19 12z"/></svg>'
};
// UI 音量上限。服务端 volume 比例基数与之一致(都是 100),
// 因此换算直接用 VOL_MAX,不再单独维护基数常量。
@@ -754,7 +764,10 @@ body {
duration: null,
elapsed: null,
progress: null,
remaining: null
remaining: null,
resumable: false,
startAt: 0,
stoppedAt: null
},
// 播放方式:queue=顺序排队 / shuffle=随机 / repeat-one=单曲循环
playMode: "queue",
@@ -951,6 +964,15 @@ body {
p.elapsed = (typeof r.elapsed_sec === "number") ? r.elapsed_sec : null;
p.progress = (typeof r.progress_pct === "number") ? r.progress_pct : null;
p.remaining = (typeof r.remaining_sec === "number") ? r.remaining_sec : null;
p.resumable = !!r.resumable;
p.startAt = (typeof r.start_at === "number") ? r.start_at : 0;
p.stoppedAt = (typeof r.stopped_at === "number") ? r.stopped_at : null;
// 暂停时服务端会把 elapsed/progress 置 null,用停播位置推导
if (!p.playing && typeof p.stoppedAt === "number") {
p.elapsed = Math.max(0, p.stoppedAt - p.startAt);
p.progress = p.duration > 0 ? (p.elapsed / p.duration) * 100 : 0;
p.remaining = Math.max(0, p.duration - p.elapsed);
}
renderNowPlaying();
renderToggle();
return p;
@@ -963,6 +985,15 @@ body {
p.elapsed = (typeof r.elapsed_sec === "number") ? r.elapsed_sec : null;
p.progress = (typeof r.progress_pct === "number") ? r.progress_pct : null;
p.remaining = (typeof r.remaining_sec === "number") ? r.remaining_sec : null;
p.resumable = !!r.resumable;
p.startAt = (typeof r.start_at === "number") ? r.start_at : 0;
p.stoppedAt = (typeof r.stopped_at === "number") ? r.stopped_at : null;
// 暂停时服务端 elapsed/progress 为 null,用 stopped_at - start_at 推导
if (!p.playing && typeof p.stoppedAt === "number") {
p.elapsed = Math.max(0, p.stoppedAt - p.startAt);
p.progress = p.duration > 0 ? (p.elapsed / p.duration) * 100 : 0;
p.remaining = Math.max(0, p.duration - p.elapsed);
}
renderNowPlaying();
renderToggle();
// 注意:这里不能调 renderList()。
@@ -1153,11 +1184,17 @@ body {
var path = t.path || t.file || "";
if (!path) { log("缺少 path 字段", "err"); return; }
// 如果点的就是当前正在播放的这首,不做任何操作,避免重新开始播放。
// force=true 用于「继续播放」等明确需要重放的场景。
if (!force && state.player.playing && state.player.source === path) {
log("正在播放这首,无需切换", "");
return;
// 点当前曲目:
// - 正在播放 -> 无操作,避免打断
// - 已暂停且可续播 -> 走 resume,从上次位置继续
if (!force && state.player.source === path) {
if (state.player.playing) {
log("正在播放这首,无需切换", "");
return;
}
if (state.player.resumable) {
return resumePlay();
}
}
setPill("播放中…", "busy");
log("正在播放:" + (t.name || path));
@@ -1235,10 +1272,50 @@ body {
}
/* 单按钮切换:服务端 playing 为准 */
/* 从暂停位置继续播放:调用 MCP resume,不再从头播放 */
function resumePlay() {
var start = Math.round(state.player.startAt || 0);
setPill("续播中…", "busy");
log("从 " + fmtDur(start) + " 续播", "");
callTool("resume", { volume: state.volume }).catch(function (e) {
// resume 失败时回退为从头播放
log("续播失败:" + e.message + ",改为从头播放", "err");
var t = currentTrackInfo();
if (t && t.path) playTrack(t, true);
});
// 短暂等待后同步服务端状态
return verifyResume(0);
}
function verifyResume(attempt) {
return new Promise(function (resolve) {
setTimeout(function () {
syncPlayerStatus({ silent: true }).then(function (p) {
if (p.playing || attempt >= 8) {
setPill(p.playing ? "已连接" : "已连接", "on");
if (p.playing) {
log("继续播放", "ok");
startPolling();
}
resolve(p);
} else {
resolve(verifyResume(attempt + 1));
}
});
}, attempt === 0 ? 400 : 350);
});
}
function togglePlay() {
if (state.player.playing) {
return stopPlay();
}
// MCP 已支持续播:优先从上次停止位置继续
if (state.player.resumable) {
return resumePlay();
}
var t = currentTrackInfo();
if (!t || !t.path) {
log("还没有可播放的曲目,请先从右侧选一首", "err");
@@ -1353,7 +1430,7 @@ body {
var wrap = $("prog-wrap"), fill = $("prog-fill");
if (!p.source) {
if (disc) { disc.className = ""; disc.textContent = "🎧"; }
if (disc) { disc.className = ""; disc.innerHTML = ICON.head; }
if (title) title.textContent = "未在播放";
if (sub) sub.textContent = "从右侧选一首,或让 AI 帮你找歌";
if (wrap) wrap.style.visibility = "hidden";
@@ -1361,6 +1438,7 @@ body {
}
if (title) title.textContent = p.title || p.source;
// 曲目变化时给唱片一次切换动效
if (disc && p.source !== FX._lastSource) {
FX._lastSource = p.source;
@@ -1368,21 +1446,28 @@ body {
}
if (p.playing) {
if (disc) { disc.className = "spinning"; disc.textContent = "🎵"; }
if (sub) {
var bits = [];
if (typeof p.duration === "number") bits.push("总长 " + fmtDur(p.duration));
if (typeof p.remaining === "number") bits.push("剩余 " + fmtDur(p.remaining));
sub.textContent = bits.join(" · ") || "正在播放";
}
if (wrap) wrap.style.visibility = "visible";
if (fill) fill.style.width = Math.max(0, Math.min(100, p.progress || 0)) + "%";
if (disc) { disc.className = "spinning"; disc.innerHTML = ICON.disc; }
} else {
// 暂停时唱片停止旋转,显示暂停图标
if (disc) { disc.className = ""; disc.innerHTML = ICON.pause; }
if (sub) sub.textContent = "已暂停 · 按播放键继续";
if (wrap) wrap.style.visibility = "hidden";
}
// 时间信息:播放中 / 暂停都显示,暂停时明确标注
if (sub) {
var bits = [];
if (typeof p.elapsed === "number") bits.push("已播 " + fmtDur(p.elapsed));
if (typeof p.duration === "number") bits.push("总长 " + fmtDur(p.duration));
// 暂停时 remaining 由 stoppedAt-startAt 推导出来,也应显示
if (typeof p.remaining === "number") {
bits.push("剩余 " + fmtDur(p.remaining));
}
var text = bits.join(" · ") || "—";
sub.textContent = p.playing ? text : ("已暂停 · " + text);
}
// 关键修改:暂停时进度条不再隐藏,保留当前进度
if (wrap) wrap.style.visibility = "visible";
if (fill) fill.style.width = Math.max(0, Math.min(100, p.progress || 0)) + "%";
}
/* 只刷新「哪一行在播放」的高亮,不重建 DOM。
@@ -1421,12 +1506,13 @@ body {
var idx = parseInt(row.getAttribute("data-idx"), 10);
var t = (isNaN(idx) ? null : state.tracks[idx]);
var path = t ? (t.path || t.file || "") : "";
var playing = !!(src && path && src === path);
var isCurrent = !!(src && path && src === path);
var playing = isCurrent && state.player.playing;
var paused = isCurrent && !state.player.playing;
if (playing) row.classList.add("playing");
else row.classList.remove("playing");
if (playing) row.classList.add("playing"); else row.classList.remove("playing");
if (paused) row.classList.add("paused"); else row.classList.remove("paused");
// 序号位:数字 <-> 跳动的音量条
var num = row.querySelector(".t-num");
if (num) {
if (playing) {
@@ -1436,15 +1522,13 @@ body {
}
} else {
num.classList.remove("t-num-play");
num.textContent = String(idx + 1);
num.innerHTML = paused ? ICON.pause : String(idx + 1);
}
}
// 右侧图标:箭头 <-> 声波
var go = row.querySelector(".t-go");
if (go) go.innerHTML = playing ? ICON.wave : ICON.arrow;
if (go) go.innerHTML = playing ? ICON.wave : (paused ? ICON.play : ICON.arrow);
}
// 记录当前渲染绑定的源
renderList._lastSource = src;
}
@@ -1491,23 +1575,29 @@ body {
var t = arr[i] || {};
var name = t.name || t.path || ("曲目 " + (i + 1));
var path = t.path || "";
// 用服务端 player_status 的 source 判定,state.playing 是已废弃字段
// 当前曲目分三态:播放中 / 已暂停 / 普通
var src = state.player.source;
var playing = !!(src && path && src === path);
var isCurrent = !!(src && path && src === path);
var playing = isCurrent && state.player.playing;
var paused = isCurrent && !state.player.playing;
var stateClass = playing ? " playing" : (paused ? " paused" : "");
var bits = [];
if (t.duration_sec) bits.push(fmtDur(t.duration_sec));
if (t.size_mb) bits.push(Number(t.size_mb).toFixed(1) + " MB");
html +=
'<div class="track' + (playing ? " playing" : "") + '" data-idx="' + i + '" tabindex="0">' +
'<div class="track' + stateClass + '" data-idx="' + i + '" tabindex="0">' +
'<div class="t-num' + (playing ? " t-num-play" : "") + '">' +
(playing ? '<span class="eq"><i></i><i></i><i></i></span>' : (i + 1)) +
(playing ? '<span class="eq"><i></i><i></i><i></i></span>'
: (paused ? ICON.pause : (i + 1))) +
'</div>' +
'<div class="t-info">' +
'<div class="t-name">' + esc(name) + '</div>' +
'<div class="t-meta">' + esc(bits.join(" · ") || "—") + '</div>' +
'</div>' +
'<div class="t-go">' + (playing ? ICON.wave : ICON.arrow) + '</div>' +
'<div class="t-go">' +
(playing ? ICON.wave : (paused ? ICON.play : ICON.arrow)) +
'</div>' +
'</div>';
}
wrap.innerHTML = html;
@@ -1563,7 +1653,14 @@ body {
function focusFirst() {
collect();
if (foc.length) { setFocus(foc[0]); focIdx = 0; }
// 页面刷新/重新进入时默认聚焦播放/暂停,遥控器一按 OK 即可操作
var playBtn = $("btn-toggle");
var idx = playBtn ? foc.indexOf(playBtn) : -1;
if (idx < 0) idx = 0;
if (foc.length && idx < foc.length) {
setFocus(foc[idx]);
focIdx = idx;
}
}
function nextInDir(dir) {