From 33e3ec714e38a4c54cca6f89fba7ddae3e1920a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=82=A6?= Date: Tue, 6 Jan 2026 10:59:14 +0800 Subject: [PATCH] feat(ui): add clear cache and optimize media session - Add button in side drawer settings to clear local cache data - Refactor Media Session position updates to trigger on specific events (seek, play/pause, load) instead of every time update - Add finite number validation for seek operations --- index.html | 67 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/index.html b/index.html index 6478b7d..049df05 100644 --- a/index.html +++ b/index.html @@ -319,7 +319,7 @@ ); - const SideDrawer = ({ isOpen, onClose, view, setView, quality, setQuality }) => { + const SideDrawer = ({ isOpen, onClose, view, setView, quality, setQuality, onClearCache }) => { if (!isOpen) return null; return ( @@ -355,8 +355,8 @@
设置
-
+ +
+ +
@@ -719,17 +734,6 @@ audio.volume = volume; const updateTime = () => { setCurrentTime(audio.currentTime); - if ('mediaSession' in navigator && !isNaN(audio.duration)) { - try { - navigator.mediaSession.setPositionState({ - duration: audio.duration, - playbackRate: audio.playbackRate, - position: audio.currentTime - }); - } catch (e) { - // Ignore errors - } - } }; const updateDuration = () => setDuration(audio.duration); const onEnded = () => playNext(true); @@ -878,8 +882,11 @@ }; const handleSeek = (time) => { - audioRef.current.currentTime = time; - setCurrentTime(time); + if (Number.isFinite(time)) { + audioRef.current.currentTime = time; + setCurrentTime(time); + updateMediaSessionPosition(); + } }; const deleteFromPlaylist = (index) => { @@ -915,6 +922,18 @@ // --- Media Session Integration --- + const updateMediaSessionPosition = () => { + if ('mediaSession' in navigator && audioRef.current && !isNaN(audioRef.current.duration)) { + try { + navigator.mediaSession.setPositionState({ + duration: audioRef.current.duration, + playbackRate: audioRef.current.playbackRate, + position: audioRef.current.currentTime + }); + } catch (e) { console.error("MediaSession Position Error:", e); } + } + }; + // Update refs for Media Session actions useEffect(() => { playNextRef.current = playNext; @@ -958,24 +977,34 @@ } }, [currentSong]); - // Media Session Playback State + // Media Session Playback State & Position useEffect(() => { if ('mediaSession' in navigator) { navigator.mediaSession.playbackState = isPlaying ? 'playing' : 'paused'; + updateMediaSessionPosition(); } }, [isPlaying]); + // Update position when song loads + useEffect(() => { + const audio = audioRef.current; + const onLoadedMetadata = () => updateMediaSessionPosition(); + audio.addEventListener('loadedmetadata', onLoadedMetadata); + return () => audio.removeEventListener('loadedmetadata', onLoadedMetadata); + }, []); + // --- Render --- return (
- setShowSideDrawer(false)} view={view} setView={setView} quality={quality} setQuality={setQuality} + onClearCache={handleClearCache} /> {/* Top Navigation / Search Bar */}