"use client"; import { useEffect, useState } from "react"; import { BsPauseFill, BsPlayFill } from "react-icons/bs"; import { HiSpeakerWave, HiSpeakerXMark } from "react-icons/hi2"; import { AiFillStepBackward, AiFillStepForward } from "react-icons/ai"; import { Song } from "@/types"; import usePlayer from "@/hooks/usePlayer"; import LikeButton from "./LikeButton"; import MediaItem from "./MediaItem"; import Slider from "./Slider"; import useSound from 'use-sound'; interface PlayerContentProps { song: Song; songUrl: string; } const PlayerContent: React.FC = ({ song, songUrl }) => { const player = usePlayer(); const [volume, setVolume] = useState(1); const [isPlaying, setIsPlaying] = useState(false); const Icon = isPlaying ? BsPauseFill : BsPlayFill; const VolumeIcon = volume === 0 ? HiSpeakerXMark : HiSpeakerWave; const onPlayNext = () => { if (player.ids.length === 0) { return; } const currentIndex = player.ids.findIndex((id) => id === player.activeId); const nextSong = player.ids[currentIndex + 1]; if (!nextSong) { return player.setId(player.ids[0]); } player.setId(nextSong); } const onPlayPrevious = () => { if (player.ids.length === 0) { return; } const currentIndex = player.ids.findIndex((id) => id === player.activeId); const previousSong = player.ids[currentIndex - 1]; if (!previousSong) { return player.setId(player.ids[player.ids.length - 1]); } player.setId(previousSong); } const [play, { pause, sound }] = useSound( songUrl, { volume: volume, onplay: () => setIsPlaying(true), onend: () => { setIsPlaying(false); onPlayNext(); }, onpause: () => setIsPlaying(false), format: ['mp3'] } ); useEffect(() => { sound?.play(); return () => { sound?.unload(); } }, [sound]); const handlePlay = () => { if (!isPlaying) { play(); } else { pause(); } } const toggleMute = () => { if (volume === 0) { setVolume(1); } else { setVolume(0); } } return (
setVolume(value)} />
); } export default PlayerContent;