Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Autoplay next song in search list #501

Merged
Merged
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
5 changes: 4 additions & 1 deletion frontend/src/components/MusicPlayer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "./styles.scss";
import "../App.css";
import { CirclePlay, CirclePause, Rewind, VolumeX, Volume2, FastForward, Repeat2 } from "lucide-react";

function MusicPlayer({ currSong, shouldAutoPlay }) {
function MusicPlayer({ currSong, shouldAutoPlay, setSongHasEnded }) {
const [isLiked, setIsLiked] = useState(false);
const songName = currSong.name || "Reminder";
const songImage =
Expand Down Expand Up @@ -77,6 +77,9 @@ function MusicPlayer({ currSong, shouldAutoPlay }) {
className="rounded-lg bg-[#83ce89] text-black h-full"
src={audioUrl}
customIcons={customIcons}
onEnded={() => {
setSongHasEnded(true)
}}
/>
</div>
</div>
Expand Down
47 changes: 34 additions & 13 deletions frontend/src/components/Search/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
fetchArtistsbySongName,
} from "../../Utils";
import Footer from "../Footer";
import { PiLayout } from "react-icons/pi";
import { storeAlbum, storeSong } from "../Auth/StoreSong";
import { Link, NavLink, Outlet, useOutletContext } from "react-router-dom";

Expand Down Expand Up @@ -50,8 +49,7 @@ function SearchDefault() {
}

function AlbumElement({ album }) {
const imageUrl =
album.images && album.images[2] ? album.images[2].url : "defaultImageUrl";
const imageUrl = album.images && album.images[2] ? album.images[2].url : "defaultImageUrl";
const artistName =
album.primaryArtists && album.primaryArtists[0]
? album.primaryArtists[0].name
Expand Down Expand Up @@ -133,17 +131,19 @@ function Artists({ artists }) {
);
}

function SongElement({ song, setCurrSong, number, setShouldAutoPlay }) {
function SongElement({ song, setCurrSong, number, setShouldAutoPlay, currSongIdx }) {
const duration = secIntoMinSec(song.duration);

return (
<div
className="flex h-[18%] justify-between cursor-pointer p-4 rounded-lg hover:bg-gray-700 hover:shadow-lg"
onClick={() => {
currSongIdx.current = number;
setCurrSong(song);
setShouldAutoPlay(true);
storeSong(song);
console.log("song", song); }}
console.log("song", song);
}}
>
<div className="flex gap-4">
<div className="flex items-center">{number}</div>
Expand Down Expand Up @@ -171,7 +171,7 @@ function SongElement({ song, setCurrSong, number, setShouldAutoPlay }) {
);
}

function Songs({ topSongs, setCurrSong, setShouldAutoPlay }) {
function Songs({ topSongs, setCurrSong, setShouldAutoPlay, currSongIdx }) {
return (
<div className="bg-[#18181D] w-full h-full rounded-lg">
<div className="w-full h-full flex flex-col gap-1 pr-8 pb-3">
Expand All @@ -183,12 +183,13 @@ function Songs({ topSongs, setCurrSong, setShouldAutoPlay }) {
if (index > 0) {
return (
<SongElement
key={index}
number={index}
song={song}
setCurrSong={setCurrSong}
setShouldAutoPlay={setShouldAutoPlay}
onClick={() => storeSong(song)}
currSongIdx={currSongIdx}
key={index}
number={index}
song={song}
setCurrSong={setCurrSong}
setShouldAutoPlay={setShouldAutoPlay}
onClick={() => storeSong(song)}
/>
);
}
Expand All @@ -206,6 +207,21 @@ export function SearchResultAll() {
const setShouldAutoPlay = context.setShouldAutoPlay;
const albums = context.albums;
const artists = context.artists;
const songHasEnded = context.songHasEnded;
const setSongHasEnded = context.setSongHasEnded;

let currSongIdx = useRef(0); // TODO: maybe this should be a state variable as well

// auto play next song when song ends
useEffect(() => {
if (songHasEnded) {
if (currSongIdx.current < topSongs.length - 1) {
currSongIdx.current++;
setCurrSong(topSongs[currSongIdx.current]); // next song it not playing as current index is not updating
setSongHasEnded(false);
}
}
}, [songHasEnded]);

return (
<div className="w-full h-full rounded-xl flex flex-col gap-4">
Expand Down Expand Up @@ -242,7 +258,7 @@ export function SearchResultAll() {
const newSong = { ...topSongs[0] };
setCurrSong(newSong);
setShouldAutoPlay(true);
storeAlbum(topSongs)
storeAlbum(topSongs);
}}
>
<FaPlay className="w-[50%] h-[50%]" />
Expand All @@ -254,6 +270,7 @@ export function SearchResultAll() {
<div className="w-[70%] h-full ">
<div className="w-full h-full ">
<Songs
currSongIdx={currSongIdx}
topSongs={topSongs}
setCurrSong={setCurrSong}
setShouldAutoPlay={setShouldAutoPlay}
Expand Down Expand Up @@ -285,6 +302,7 @@ function Search({ setCurrPage }) {
const [albums, setAlbums] = useState([]);
const [artists, setArtist] = useState([]);
let [shouldAutoPlay, setShouldAutoPlay] = useState(false);
const [songHasEnded, setSongHasEnded] = useState(false);

const searchAllNavLinkElement = useRef(null);

Expand Down Expand Up @@ -384,6 +402,8 @@ function Search({ setCurrPage }) {
setShouldAutoPlay,
albums,
artists,
songHasEnded,
setSongHasEnded,
}}
/>
</div>
Expand All @@ -394,6 +414,7 @@ function Search({ setCurrPage }) {
<MusicPlayer
currSong={currSong}
shouldAutoPlay={shouldAutoPlay}
setSongHasEnded={setSongHasEnded}
/>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading