播放页OK
This commit is contained in:
1 parent
0ea21f735a
commit
7c4b841f69
1 file changed
+203
-91
@@ -47,14 +47,34 @@ body {
|
||||
|
||||
/* ---- 顶部栏 ---- */
|
||||
#topbar {
|
||||
display: flex;
|
||||
display: grid;
|
||||
grid-template-columns: 1fr auto 1fr;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1.2vw;
|
||||
flex: 0 0 auto;
|
||||
height: 5.2vh;
|
||||
}
|
||||
|
||||
#topbar-center {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
line-height: 1.05;
|
||||
}
|
||||
#clock-date { font-size: 1.3vh; color: #7d8fb0; letter-spacing: .04em; }
|
||||
#clock-time {
|
||||
font-size: 2.2vh; font-weight: 700; color: #f2f7ff;
|
||||
font-variant-numeric: tabular-nums; letter-spacing: .05em;
|
||||
}
|
||||
|
||||
#topbar-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
gap: 0.8vw;
|
||||
}
|
||||
|
||||
#brand {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@@ -99,6 +119,26 @@ body {
|
||||
#status-pill.off { background: rgba(239,68,68,.15); color: #f87171; border-color: rgba(239,68,68,.4); }
|
||||
#status-pill.busy { background: rgba(245,158,11,.16); color: #fbbf24; border-color: rgba(245,158,11,.4); }
|
||||
|
||||
#btn-home {
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
width: 4.2vh; height: 4.2vh;
|
||||
padding: 0;
|
||||
border-radius: 50%;
|
||||
font-size: 2.1vh;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
color: #9db0d0;
|
||||
background: rgba(148,163,184,.10);
|
||||
border: 1px solid rgba(148,163,184,.20);
|
||||
transition: background .15s, border-color .15s, color .15s;
|
||||
}
|
||||
#btn-home:hover, #btn-home:focus, #btn-home.tv-focus {
|
||||
color: #e4f2ff;
|
||||
background: rgba(76,201,240,.18);
|
||||
border-color: #3ec4f8;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
/* ---- 主区:横向两栏,占满剩余高度 ---- */
|
||||
#main-area {
|
||||
display: flex;
|
||||
@@ -644,7 +684,16 @@ body {
|
||||
<p>ai-speaker · JBL PS3500</p>
|
||||
</div>
|
||||
</div>
|
||||
<div id="topbar-center">
|
||||
<div id="clock-date">----</div>
|
||||
<div id="clock-time">--:--:--</div>
|
||||
</div>
|
||||
<div id="topbar-right">
|
||||
<div id="status-pill" class="off">连接中…</div>
|
||||
<button id="btn-home" class="ctrl-btn home-btn" title="返回首页" aria-label="返回首页">
|
||||
<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="M3 10.7 12 3l9 7.7"/><path d="M5.5 9.6V21h13V9.6"/><path d="M9.5 21v-6h5v6"/></svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 主区 -->
|
||||
@@ -767,7 +816,13 @@ body {
|
||||
remaining: null,
|
||||
resumable: false,
|
||||
startAt: 0,
|
||||
stoppedAt: null
|
||||
stoppedAt: null,
|
||||
// MCP 异步状态机字段
|
||||
id: null,
|
||||
state: "idle",
|
||||
error: null,
|
||||
playbackId: null,
|
||||
buffering: false
|
||||
},
|
||||
// 播放方式:queue=顺序排队 / shuffle=随机 / repeat-one=单曲循环
|
||||
playMode: "queue",
|
||||
@@ -779,6 +834,38 @@ body {
|
||||
volPanelOpen: false
|
||||
};
|
||||
|
||||
/* ============ 顶部日期时间 ============ */
|
||||
function tickClock() {
|
||||
var d = new Date();
|
||||
var pad = function (n) { return n < 10 ? "0" + n : String(n); };
|
||||
var week = ["日", "一", "二", "三", "四", "五", "六"][d.getDay()];
|
||||
var dateEl = $("clock-date");
|
||||
var timeEl = $("clock-time");
|
||||
if (dateEl) dateEl.textContent = d.getFullYear() + "-" + pad(d.getMonth()+1) + "-" + pad(d.getDate()) + " 周" + week;
|
||||
if (timeEl) timeEl.textContent = pad(d.getHours()) + ":" + pad(d.getMinutes()) + ":" + pad(d.getSeconds());
|
||||
}
|
||||
|
||||
/* ============ 返回默认首页 ============ */
|
||||
function goHome() {
|
||||
// 走 MCP 的 rollback_page(home):服务端下发 render.home,
|
||||
// 宿主会真正 restoreHomeScreen() 并清掉保存的动态页。
|
||||
// 不能走 AndroidBridge.onWebAction —— 那只会发 interaction.reply。
|
||||
log("正在返回默认首页…", "ok");
|
||||
if (typeof callTool === "function") {
|
||||
callTool("rollback_page", { device_id: DEV_ID, snapshot_id: "home" })
|
||||
.then(function (r) {
|
||||
if (r && r.ok) log("已返回默认首页", "ok");
|
||||
else log("返回首页失败:" + ((r && r.error) || "未知错误"), "err");
|
||||
})
|
||||
.catch(function (e) {
|
||||
log("返回首页失败:" + (e && e.message ? e.message : String(e)), "err");
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 兜底:加载宿主首页(asset loader 域名)
|
||||
location.href = "http://appassets.androidplatform.net/assets/pulsewall/host.html";
|
||||
}
|
||||
|
||||
/* ============ 日志 / 状态 ============ */
|
||||
function log(msg, kind) {
|
||||
var el = $("log-line");
|
||||
@@ -967,6 +1054,9 @@ body {
|
||||
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;
|
||||
p.id = (typeof r.id === "number") ? r.id : null;
|
||||
p.state = r.state || (r.playing ? "playing" : "stopped");
|
||||
p.error = r.error || null;
|
||||
// 暂停时服务端会把 elapsed/progress 置 null,用停播位置推导
|
||||
if (!p.playing && typeof p.stoppedAt === "number") {
|
||||
p.elapsed = Math.max(0, p.stoppedAt - p.startAt);
|
||||
@@ -988,6 +1078,9 @@ body {
|
||||
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;
|
||||
p.id = (typeof r.id === "number") ? r.id : null;
|
||||
p.state = r.state || (r.playing ? "playing" : "stopped");
|
||||
p.error = r.error || null;
|
||||
// 暂停时服务端 elapsed/progress 为 null,用 stopped_at - start_at 推导
|
||||
if (!p.playing && typeof p.stoppedAt === "number") {
|
||||
p.elapsed = Math.max(0, p.stoppedAt - p.startAt);
|
||||
@@ -1179,16 +1272,94 @@ body {
|
||||
return arr[((cur + delta) % n + n) % n];
|
||||
}
|
||||
|
||||
/* =========================================================
|
||||
统一处理 MCP 异步播放:
|
||||
play_file / resume 只返回 buffering + playback_id,
|
||||
必须等 wait_playback 返回 state='playing' 后才算真正开始。
|
||||
========================================================= */
|
||||
function startPlaybackByTool(toolName, args, targetPath, switching) {
|
||||
setPill("缓冲中…", "busy");
|
||||
state.player.buffering = true;
|
||||
state.player.state = "buffering";
|
||||
renderToggle();
|
||||
|
||||
if (switching && targetPath) {
|
||||
state.pendingSource = targetPath;
|
||||
state.pendingUntil = Date.now() + 6000;
|
||||
}
|
||||
|
||||
return callTool(toolName, args).then(function (r) {
|
||||
if (!r || !r.ok) {
|
||||
throw new Error((r && r.error) ? r.error : "启动播放失败");
|
||||
}
|
||||
var pid = r.playback_id;
|
||||
state.player.playbackId = pid;
|
||||
log("已提交,playback_id=" + pid + ",等待就绪…", "");
|
||||
|
||||
// 等待服务端真正进入 playing
|
||||
return callTool("wait_playback", { playback_id: pid, timeout: 8 }).then(function (st) {
|
||||
state.player.buffering = false;
|
||||
state.player.playbackId = pid;
|
||||
if (st && st.ok) {
|
||||
applyPlaybackState(st);
|
||||
setPill("已连接", "on");
|
||||
log("开始播放", "ok");
|
||||
state.pendingSource = null;
|
||||
state.pendingUntil = 0;
|
||||
startPolling();
|
||||
} else {
|
||||
setPill("已连接", "on");
|
||||
log("播放未就绪", "err");
|
||||
}
|
||||
return st;
|
||||
});
|
||||
}).catch(function (e) {
|
||||
state.player.buffering = false;
|
||||
state.pendingSource = null;
|
||||
state.pendingUntil = 0;
|
||||
setPill("已连接", "on");
|
||||
log(toolName + " 失败:" + e.message, "err");
|
||||
syncPlayerStatus({ silent: true });
|
||||
throw e;
|
||||
});
|
||||
}
|
||||
|
||||
/* 把 wait_playback / playback_status / player_status 的统一结构写入页面状态 */
|
||||
function applyPlaybackState(r) {
|
||||
var p = state.player;
|
||||
p.id = (typeof r.id === "number") ? r.id : p.id;
|
||||
p.state = r.state || p.state;
|
||||
p.playing = !!r.playing;
|
||||
p.source = r.source || p.source;
|
||||
p.title = r.title || p.title;
|
||||
p.duration = (typeof r.duration === "number") ? r.duration : null;
|
||||
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;
|
||||
p.error = r.error || 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();
|
||||
refreshPlayingMark();
|
||||
}
|
||||
|
||||
function playTrack(t, force) {
|
||||
if (!t) return;
|
||||
var path = t.path || t.file || "";
|
||||
if (!path) { log("缺少 path 字段", "err"); return; }
|
||||
|
||||
// 点当前曲目:
|
||||
// - 正在播放 -> 无操作,避免打断
|
||||
// - 已暂停且可续播 -> 走 resume,从上次位置继续
|
||||
// - 正在播放 -> 无操作
|
||||
// - 已暂停且可续播 -> 走 resume
|
||||
if (!force && state.player.source === path) {
|
||||
if (state.player.playing) {
|
||||
if (state.player.playing || state.player.buffering) {
|
||||
log("正在播放这首,无需切换", "");
|
||||
return;
|
||||
}
|
||||
@@ -1196,65 +1367,17 @@ body {
|
||||
return resumePlay();
|
||||
}
|
||||
}
|
||||
setPill("播放中…", "busy");
|
||||
log("正在播放:" + (t.name || path));
|
||||
// 注意:play_file 在部分实现里会阻塞到音频结束,因此不依赖它的返回值,
|
||||
// 改为播放请求发出后主动同步一次 player_status。
|
||||
callTool("play_file", { path: path, volume: state.volume }).catch(function (e) {
|
||||
log("播放返回异常:" + e.message, "err");
|
||||
});
|
||||
|
||||
// 本地先把高亮切到目标曲目,避免等待服务端响应期间的视觉延迟
|
||||
// 开启切歌冻结:服务端 source 追上之前,不允许被旧状态覆盖
|
||||
var isSwitch = (state.player.source !== path);
|
||||
if (isSwitch) {
|
||||
state.pendingSource = path;
|
||||
state.pendingUntil = Date.now() + 4000;
|
||||
}
|
||||
state.player.source = path;
|
||||
state.player.title = t.name || path;
|
||||
state.player.playing = true;
|
||||
|
||||
// 列表条目不变,只是高亮位置改变 —— 只更新那一条
|
||||
refreshPlayingMark();
|
||||
renderNowPlaying();
|
||||
renderToggle();
|
||||
|
||||
// 注意:play_file 的服务端 source 更新有延迟(ffmpeg 启动需时间),
|
||||
// 单次 600ms 同步可能仍拿到旧 source。改为短周期多次校验。
|
||||
return verifySwitch(path, 0);
|
||||
var switching = (state.player.source !== path);
|
||||
log("正在提交播放:" + (t.name || path), "");
|
||||
return startPlaybackByTool(
|
||||
"play_file",
|
||||
{ path: path, volume: state.volume, start_at: 0 },
|
||||
path,
|
||||
switching
|
||||
);
|
||||
}
|
||||
|
||||
/* 切歌后的服务端校验:最多重试 8 次,直到 source 与目标一致 */
|
||||
function verifySwitch(targetPath, attempt) {
|
||||
var MAX = 8;
|
||||
return new Promise(function (resolve) {
|
||||
setTimeout(function () {
|
||||
syncPlayerStatus({ silent: true }).then(function (p) {
|
||||
var matched = (p.source === targetPath);
|
||||
if (matched || attempt >= MAX) {
|
||||
if (!matched) {
|
||||
// 超时仍不匹配:以服务端为准回写,避免界面长期显示错误状态
|
||||
log("状态同步超时,以服务端为准", "err");
|
||||
// 超时必须解除冻结,否则后面所有状态同步都会被跳过
|
||||
state.pendingSource = null;
|
||||
state.pendingUntil = 0;
|
||||
refreshPlayingMark();
|
||||
renderNowPlaying();
|
||||
} else {
|
||||
log("正在播放", "ok");
|
||||
state.pendingSource = null;
|
||||
state.pendingUntil = 0;
|
||||
}
|
||||
startPolling();
|
||||
resolve(p);
|
||||
} else {
|
||||
resolve(verifySwitch(targetPath, attempt + 1));
|
||||
}
|
||||
});
|
||||
}, attempt === 0 ? 500 : 400);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function stopPlay() {
|
||||
@@ -1274,39 +1397,20 @@ body {
|
||||
/* 单按钮切换:服务端 playing 为准 */
|
||||
/* 从暂停位置继续播放:调用 MCP resume,不再从头播放 */
|
||||
function resumePlay() {
|
||||
if (state.player.buffering) {
|
||||
log("正在缓冲,请稍候", "");
|
||||
return Promise.resolve(state.player);
|
||||
}
|
||||
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);
|
||||
return startPlaybackByTool(
|
||||
"resume",
|
||||
{ volume: state.volume },
|
||||
state.player.source,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -1621,6 +1725,7 @@ body {
|
||||
var nodes = document.querySelectorAll(".track, .btn, .ctrl-btn, .icon-btn");
|
||||
foc = Array.prototype.slice.call(nodes).filter(function (el) {
|
||||
if (el.disabled) return false;
|
||||
if (el.classList.contains("home-btn")) return false; // 返回首页仅鼠标/触屏点击,避免遥控误触退出
|
||||
var r = el.getBoundingClientRect();
|
||||
return r.width > 4 && r.height > 4;
|
||||
});
|
||||
@@ -1761,6 +1866,11 @@ body {
|
||||
if (!isNaN(idx) && state.tracks[idx]) playTrack(state.tracks[idx]);
|
||||
});
|
||||
|
||||
var btnHome = $("btn-home");
|
||||
if (btnHome) btnHome.addEventListener("click", function (e) {
|
||||
e.preventDefault(); e.stopPropagation(); FX.press(btnHome); goHome();
|
||||
});
|
||||
|
||||
$("btn-refresh").addEventListener("click", function () {
|
||||
var ic = this;
|
||||
ic.classList.remove("spinning");
|
||||
@@ -1974,6 +2084,8 @@ body {
|
||||
|
||||
function boot() {
|
||||
bind();
|
||||
tickClock();
|
||||
setInterval(tickClock, 1000);
|
||||
renderVolume();
|
||||
renderList();
|
||||
renderToggle();
|
||||
|
||||
Reference in new issue
Block a user