From 7c89ecdc76f71fa5c1623b12d4e9dee02776c89e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 9 Aug 2025 06:27:49 +0000 Subject: [PATCH] Implement song position restoration and playback control improvements Co-authored-by: gautamdaksh29 --- .../audioComponents/AudioComponent.jsx | 74 ++++++++++--------- .../audioComponents/AudioVisulizer.jsx | 1 - src/Contexts/playerContext.jsx | 2 + 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/Components/audioComponents/AudioComponent.jsx b/src/Components/audioComponents/AudioComponent.jsx index c034558..ca4bd84 100644 --- a/src/Components/audioComponents/AudioComponent.jsx +++ b/src/Components/audioComponents/AudioComponent.jsx @@ -11,7 +11,7 @@ const AudioComponent = () => { const ContextShowPlaylists= useContext(showPlaylistsContext) - const { currentSong, isPlaying , setIsPlaying , durationRef , currentTimeRef , audioRef, nextTrack, context} = usePlayer(); + const { currentSong, isPlaying , setIsPlaying , durationRef , currentTimeRef , audioRef, nextTrack, context, positionSec} = usePlayer(); const { data: session } = useSession(); @@ -22,36 +22,46 @@ const AudioComponent = () => { durationRef.current = (audioRef.current?.duration); if (audioRef.current) { - audioRef.current - .play() - .then(() => { - ContextShowRight.setShowRight(true); - if(window.innerWidth <= 1280){ - ContextShowPlaylists.setShowPlaylists(false) - } - setIsPlaying(true); // ✅ only after successful play - - // Record song in recents - if (session && currentSong) { - try { - fetch("/api/recents", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - entityType: "Song", - entityId: currentSong._id, - songId: currentSong._id, - parent: context - }) - }); - } catch (err) { - console.error("Failed to record song in recents:", err); - } - } - }) - .catch((err) => { - console.warn("Play failed:", err); - }); + // Seek to last known position if any + if (positionSec && positionSec > 0) { + try { + audioRef.current.currentTime = positionSec; + } catch {} + } + + // Only play if user intended playback + if (isPlaying) { + audioRef.current + .play() + .then(() => { + ContextShowRight.setShowRight(true); + if(window.innerWidth <= 1280){ + ContextShowPlaylists.setShowPlaylists(false) + } + setIsPlaying(true); // ✅ only after successful play + + // Record song in recents + if (session && currentSong) { + try { + fetch("/api/recents", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + entityType: "Song", + entityId: currentSong._id, + songId: currentSong._id, + parent: context + }) + }); + } catch (err) { + console.error("Failed to record song in recents:", err); + } + } + }) + .catch((err) => { + console.warn("Play failed:", err); + }); + } } }; @@ -60,7 +70,6 @@ const AudioComponent = () => { - // Update current time while playing const onTimeUpdate = () => { currentTimeRef.current = (audioRef.current?.currentTime); @@ -140,7 +149,6 @@ const AudioComponent = () => { onEnded={nextTrack} ref={audioRef} src={currentSong?.fileUrl || null} - autoPlay > ); diff --git a/src/Components/audioComponents/AudioVisulizer.jsx b/src/Components/audioComponents/AudioVisulizer.jsx index ee60f52..a8fedce 100644 --- a/src/Components/audioComponents/AudioVisulizer.jsx +++ b/src/Components/audioComponents/AudioVisulizer.jsx @@ -46,7 +46,6 @@ const AudioVisualizer = () => { src="https://eren2yeager.github.io/songs/audios/Legends%20Never%20Die%20(ft.%20Against%20The%20Current)%20%EF%BD%9C%20Worlds%202017%20-%20League%20of%20Legends%20(1).mp3" controls className="mb-4" - autoPlay />
{bars.map((value, index) => ( diff --git a/src/Contexts/playerContext.jsx b/src/Contexts/playerContext.jsx index 7ea38fa..a9fbb0e 100644 --- a/src/Contexts/playerContext.jsx +++ b/src/Contexts/playerContext.jsx @@ -66,6 +66,8 @@ export const PlayerProvider = ({ children }) => { setCurrentPlayOrderIndex(newPlayOrder.indexOf(startIndex)); setUserInsertQueue([]); setPositionSec(0); + // User-initiated play should start playback + setIsPlaying(true); }, [isShuffling, buildPlayOrder]); const conditionCheckForSong = (item) => {