From 2c71d76e00e3f4fb2ee05f0ae04a91fa6876f602 Mon Sep 17 00:00:00 2001 From: shinya Date: Thu, 26 Jun 2025 01:09:02 +0800 Subject: [PATCH] feat: resume play time when changing source --- src/app/play/page.tsx | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/app/play/page.tsx b/src/app/play/page.tsx index be50264..c333fd2 100644 --- a/src/app/play/page.tsx +++ b/src/app/play/page.tsx @@ -311,7 +311,13 @@ function PlayPageClient() { resumeTimeRef.current > 0 ) { try { - playerRef.current.currentTime = resumeTimeRef.current; + const duration = playerRef.current.duration || 0; + let target = resumeTimeRef.current; + // 如果目标时间距离结尾过近,为避免自动触发下一集,向前偏移 5 秒 + if (duration && target >= duration - 2) { + target = Math.max(0, duration - 5); + } + playerRef.current.currentTime = target; } catch (err) { console.warn('恢复播放进度失败:', err); } @@ -506,6 +512,9 @@ function PlayPageClient() { newTitle: string ) => { try { + // 记录当前播放进度(仅在同一集数切换时恢复) + const currentPlayTime = playerRef.current?.currentTime || 0; + // 显示换源加载状态 setSourceChanging(true); setError(null); @@ -536,6 +545,14 @@ function PlayPageClient() { targetIndex = 0; } + // 如果仍然是同一集数且播放进度有效,则在播放器就绪后恢复到原始进度 + if (targetIndex === currentEpisodeIndex && currentPlayTime > 1) { + resumeTimeRef.current = currentPlayTime; + } else { + // 否则从头开始播放,防止影响后续选集逻辑 + resumeTimeRef.current = 0; + } + // 更新URL参数(不刷新页面) const newUrl = new URL(window.location.href); newUrl.searchParams.set('source', newSource);