一分钟搭建影视站,支持Docker等部署方式
fix: restore playback position and auto-hide controls
plover7819f4683a0e
diff --git a/src/components/player-shell.tsx b/src/components/player-shell.tsxindex 8c0d786..221d6de 100644--- a/src/components/player-shell.tsx+++ b/src/components/player-shell.tsx@@ -85,6 +85,8 @@ export function PlayerShell({const videoErrorRetryUsedRef = useRef(false);// 进度恢复每集只执行一次(MANIFEST_PARSED 可能因代理回退再次触发)const restoredRef = useRef(false);+ // 某些 HLS 源在 MANIFEST_PARSED 时 duration 仍未就绪,延迟到媒体元数据可用后恢复+ const restoreCleanupRef = useRef<(() => void) | null>(null);// timeupdate 续跑预取的游标:必须按集清零,否则会沿用上一集的时间戳推迟首次续跑const lastPrefetchEnsureRef = useRef(0);// setupHls 可能改走代理形式,video:error 重试要用最近一次的地址@@ -154,36 +156,57 @@ export function PlayerShell({* 注意:不销毁 ArtPlayer,只销毁并重建 hls,从而保留网页全屏等播放器状态。*/const setupHls = (video: HTMLVideoElement, mediaUrl: string, allowProxyFallback: boolean) => {+ restoreCleanupRef.current?.();+ restoreCleanupRef.current = null;hlsRef.current?.destroy();currentMediaUrlRef.current = mediaUrl;const hls = new Hls(buildHlsConfig());hlsRef.current = hls;+ let restoreInFlight = false;+ const restoreAndStart = async () => {+ if (restoredRef.current || restoreInFlight) return;++ // MANIFEST_PARSED 早于 loadedmetadata 的 HLS 源很常见;没有有效时长时+ // 先不消费 restoredRef,等 loadedmetadata/durationchange 后再重试。+ const duration = artRef.current?.duration || video.duration || 0;+ if (!Number.isFinite(duration) || duration <= 0) return;++ restoreInFlight = true;+ try {+ const saved = (await propsRef.current.getRestorePosition?.()) ?? 0;+ const currentDuration = artRef.current?.duration || video.duration || duration;+ let prefetchAnchor = video.currentTime;+ if (saved > 10 && Number.isFinite(currentDuration) && saved < currentDuration - 2) {+ if (artRef.current) artRef.current.currentTime = saved;+ prefetchAnchor = saved;+ showHint(`已从 ${formatTime(saved)} 继续播放`);+ }+ restoredRef.current = true;+ restoreCleanupRef.current?.();+ restoreCleanupRef.current = null;+ ensurePrefetch(mediaUrl, prefetchAnchor);+ video.play().catch(() => {});+ } catch {+ // IndexedDB 读取失败时不阻塞播放,后续事件仍可再次尝试恢复。+ } finally {+ restoreInFlight = false;+ }+ };++ const restoreEvents = ['loadedmetadata', 'durationchange', 'canplay'] as const;+ const onRestoreEvent = () => { void restoreAndStart(); };+ restoreEvents.forEach((event) => video.addEventListener(event, onRestoreEvent));+ restoreCleanupRef.current = () => {+ restoreEvents.forEach((event) => video.removeEventListener(event, onRestoreEvent));+ };+hls.loadSource(mediaUrl);hls.attachMedia(video);- hls.on(Hls.Events.MANIFEST_PARSED, async () => {+ hls.on(Hls.Events.MANIFEST_PARSED, () => {recoveryRef.current?.markHealthy();- // 预取锚点:恢复进度时直接取恢复目标。赋值后 video.currentTime 未必立刻反映- // (Safari 系),而 ensure 是以「锚点未变」为前提复用 parsing 中的运行的——- // 这里读到旧值会把窗口建在片头,且白等一次纠错重建。- let prefetchAnchor = video.currentTime;- // 进度恢复(每集一次):优先 URL position,其次 IndexedDB 记录- if (!restoredRef.current) {- restoredRef.current = true;- try {- const saved = (await propsRef.current.getRestorePosition?.()) ?? 0;- const duration = artRef.current?.duration || 0;- if (saved > 10 && duration > 0 && saved < duration - 2) {- if (artRef.current) artRef.current.currentTime = saved;- prefetchAnchor = saved;- showHint(`已从 ${formatTime(saved)} 继续播放`);- }- } catch { /* 忽略恢复失败 */ }- }- // 新集立即预取(否则要等 timeupdate 的 30s 节流,起播初期无缓存)- ensurePrefetch(mediaUrl, prefetchAnchor);- video.play().catch(() => {});+ void restoreAndStart();});// 播放链路恢复(FRAG_LOADED / MANIFEST_PARSED):静默窗外清零连续失败计数hls.on(Hls.Events.FRAG_LOADED, () => recoveryRef.current?.markHealthy());@@ -260,6 +283,9 @@ export function PlayerShell({useEffect(() => {if (!containerRef.current || !propsRef.current.url) return;const initialUrl = propsRef.current.url;+ // ArtPlayer 默认 3 秒才隐藏控制栏;播放器失去鼠标悬停后缩短为 0.8 秒。+ const previousControlHideTime = Artplayer.CONTROL_HIDE_TIME;+ Artplayer.CONTROL_HIDE_TIME = 800;// 初始化每集状态mediaHealthyRef.current = true;@@ -402,6 +428,25 @@ export function PlayerShell({let originalRate = 1.0;const el = containerRef.current;+ // 全屏时播放器覆盖整个视口,无法依赖 mouseleave 判断“移出播放器”。+ // 改为按鼠标活动计时:停止移动后快速收起控制栏,移动时立即显示。+ let controlHideTimer: ReturnType<typeof setTimeout> | null = null;+ const showControlsOnPointerMove = () => {+ art.controls.show = true;+ if (controlHideTimer) clearTimeout(controlHideTimer);+ controlHideTimer = setTimeout(() => {+ if (!art.setting.show) art.controls.show = false;+ }, 800);+ };+ const hideControlsOnMouseLeave = () => {+ if (controlHideTimer) clearTimeout(controlHideTimer);+ controlHideTimer = null;+ art.controls.show = false;+ };+ el?.addEventListener('mousemove', showControlsOnPointerMove);+ el?.addEventListener('mouseleave', hideControlsOnMouseLeave);+ showControlsOnPointerMove();+const onTouchStart = (e: TouchEvent) => {if (art.video?.paused) return;// 已有触控在处理时忽略新手指:否则第二指会把 originalRate 捕获成 3.0,@@ -461,6 +506,12 @@ export function PlayerShell({el?.removeEventListener('touchcancel', onTouchEnd);el?.removeEventListener('touchmove', onTouchMove);el?.removeEventListener('contextmenu', onContextMenu);+ el?.removeEventListener('mousemove', showControlsOnPointerMove);+ el?.removeEventListener('mouseleave', hideControlsOnMouseLeave);+ if (controlHideTimer) clearTimeout(controlHideTimer);+ restoreCleanupRef.current?.();+ restoreCleanupRef.current = null;+ Artplayer.CONTROL_HIDE_TIME = previousControlHideTime;hlsRef.current?.destroy();hlsRef.current = null;recoveryRef.current?.dispose();