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 />