diff --git a/src/app/play/page.tsx b/src/app/play/page.tsx index b0a1040..09d328a 100644 --- a/src/app/play/page.tsx +++ b/src/app/play/page.tsx @@ -5,7 +5,7 @@ import Artplayer from 'artplayer'; import Hls from 'hls.js'; import { Heart } from 'lucide-react'; -import { useSearchParams } from 'next/navigation'; +import { useRouter, useSearchParams } from 'next/navigation'; import { Suspense, useEffect, useRef, useState } from 'react'; import { @@ -33,6 +33,7 @@ declare global { } function PlayPageClient() { + const router = useRouter(); const searchParams = useSearchParams(); // ----------------------------------------------------------------------------- @@ -50,7 +51,7 @@ function PlayPageClient() { const [favorited, setFavorited] = useState(false); // 去广告开关(从 localStorage 继承,默认 true) - const [blockAdEnabled, _setBlockAdEnabled] = useState(() => { + const [blockAdEnabled, setBlockAdEnabled] = useState(() => { if (typeof window !== 'undefined') { const v = localStorage.getItem('enable_blockad'); if (v !== null) return v === 'true'; @@ -953,12 +954,22 @@ function PlayPageClient() { onClick() { const newVal = !blockAdEnabled; try { - saveCurrentPlayProgress(); localStorage.setItem('enable_blockad', String(newVal)); + if (artPlayerRef.current) { + resumeTimeRef.current = artPlayerRef.current.currentTime; + if ( + artPlayerRef.current.video && + artPlayerRef.current.video.hls + ) { + artPlayerRef.current.video.hls.destroy(); + } + artPlayerRef.current.destroy(); + artPlayerRef.current = null; + } + setBlockAdEnabled(newVal); } catch (_) { // ignore } - window.location.reload(); return newVal ? '当前开启' : '当前关闭'; }, }, @@ -1067,7 +1078,7 @@ function PlayPageClient() { console.error('创建播放器失败:', err); setError('播放器初始化失败'); } - }, [Artplayer, Hls, videoUrl, loading]); + }, [Artplayer, Hls, videoUrl, loading, blockAdEnabled]); // --------------------------------------------------------------------------- // 视频元素事件监听 @@ -1194,7 +1205,7 @@ function PlayPageClient() { if (loading) { return ( -
+
{/* 动画影院图标 */}
@@ -1283,7 +1294,7 @@ function PlayPageClient() { if (error) { return ( -
+
{/* 错误图标 */}
@@ -1327,10 +1338,8 @@ function PlayPageClient() {