Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 41 additions & 33 deletions src/Components/audioComponents/AudioComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();


Expand All @@ -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);
});
}
}
};

Expand All @@ -60,7 +70,6 @@ const AudioComponent = () => {




// Update current time while playing
const onTimeUpdate = () => {
currentTimeRef.current = (audioRef.current?.currentTime);
Expand Down Expand Up @@ -140,7 +149,6 @@ const AudioComponent = () => {
onEnded={nextTrack}
ref={audioRef}
src={currentSong?.fileUrl || null}
autoPlay
></audio>
</div>
);
Expand Down
1 change: 0 additions & 1 deletion src/Components/audioComponents/AudioVisulizer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
/>
<div className="flex items-end h-32 gap-1 w-full max-w-xl">
{bars.map((value, index) => (
Expand Down
2 changes: 2 additions & 0 deletions src/Contexts/playerContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down