import React, { useEffect, useMemo, useRef, useState } from "react"; import { motion } from "framer-motion"; import Fab from "@mui/material/Fab"; import IconButton from "@mui/material/IconButton"; import Slider from "@mui/material/Slider"; import Typography from "@mui/material/Typography"; import { setAudioRef, useAudioPlayback } from "@/utils/store/audioPlayback"; import useQueue from "@/utils/store/queue"; import { AnimatePresence } from "framer-motion"; import "./audioPlayer.scss"; import { getRuntimeMusic, secToTicks, ticksToSec } from "@/utils/date/time"; import PlayNextButton from "@/components/buttons/playNextButton"; import PlayPreviousButton from "@/components/buttons/playPreviousButtom"; import QueueButton from "@/components/buttons/queueButton"; import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; import { useApiInContext } from "@/utils/store/api"; import { useCentralStore } from "@/utils/store/central"; import { playItemFromQueue } from "@/utils/store/playback"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; import { useQuery } from "@tanstack/react-query"; import { useNavigate } from "@tanstack/react-router"; const AudioPlayer = () => { const api = useApiInContext((s) => s.api); const audioRef = useRef(null!); const [url, display, item] = useAudioPlayback((state) => [ state.url, state.display, state.item, ]); const [tracks, currentTrack] = useQueue((state) => [ state.tracks, state.currentItemIndex, ]); const user = useCentralStore((s) => s.currentUser); const playing = useMemo(() => { if (audioRef.current?.paused) return false; return true; }, [audioRef.current?.paused]); const [playerReady, setPlayerReady] = useState(false); const [volume, setVolume] = useState(0.8); const [isMuted, setIsMuted] = useState(false); const [progress, setProgress] = useState(0); const [isScrubbing, setIsScrubbing] = useState(false); const [sliderProgress, setSliderProgress] = useState(0); const [loading, setLoading] = useState(true); useEffect(() => { if (display) { // const audioUrlRequest = new Request(audioRef.current?.dataset.src, { // method: "GET", // headers: { // Authorization: api.authorizationHeader, // }, // }); // fetch(audioUrlRequest) // .then((res) => res) // .then(async (result) => { // const blob = await result.blob(); // const blobUrl = URL.createObjectURL(blob); // audioRef.current.src = blobUrl; // await audioRef.current.play(); // setLoading(false); // }); // setAudioRef(audioRef); } }, [url, tracks?.[currentTrack]?.Id]); const navigate = useNavigate(); const info = useMemo( () => !!api?.configuration && (
{tracks?.[currentTrack]?.Name music_note
{item?.Name} by {item?.Artists?.map((artist) => artist).join(",")}
), [tracks?.[currentTrack]?.Id, currentTrack], ); const controls = useMemo( () => (
audioRef.current.paused ? audioRef.current.play() : audioRef.current.pause() } >
{playing ? "pause" : "play_arrow"}
), [tracks?.[currentTrack]?.Id, loading, playing, tracks?.length], ); const buttons = useMemo( () => (
navigate({ to: "/player/audio" })}> info { setIsMuted(!isMuted); }} >
{isMuted ? "volume_mute" : "volume_up"}
{ Array.isArray(newVal) ? setVolume(newVal[0] / 100) : setVolume(newVal / 100); if (Array.isArray(newVal)) { audioRef.current.volume = newVal[0] / 100; } else { audioRef.current.volume = newVal / 100; } }} valueLabelDisplay="auto" valueLabelFormat={(value) => Math.floor(value)} size="small" sx={{ mr: 1, ml: 1, width: "10em", "& .MuiSlider-valueLabel": { lineHeight: 1.2, fontSize: 24, background: "rgb(0 0 0 / 0.5)", backdropFilter: "blur(5px)", padding: 1, borderRadius: "10px", border: "1px solid rgb(255 255 255 / 0.15)", boxShadow: "0 0 10px rgb(0 0 0 / 0.4) ", transform: "translatey(-120%) scale(0)", "&:before": { display: "none", }, "&.MuiSlider-valueLabelOpen": { transform: "translateY(-120%) scale(1)", }, "& > *": { transform: "rotate(0deg)", }, }, }} /> { useAudioPlayback.setState(useAudioPlayback.getInitialState()); }} >
close
), [volume, isMuted, currentTrack], ); if (audioRef.current) { audioRef.current.addEventListener("ended", () => { if (tracks?.[currentTrack + 1]?.Id) { playItemFromQueue("next", user?.Id, api); } }); } return ( {display && ( {info} )} ); }; export default AudioPlayer;