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 { 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, }, }); const currentTrackItem = tracks[currentTrack]; 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(); if ("mediaSession" in navigator) { navigator.mediaSession.playbackState = "playing"; navigator.mediaSession.metadata = new MediaMetadata({ title: currentTrackItem.Name ?? "Unknown audio", artist: currentTrackItem.Artists?.join(", "), album: currentTrackItem.Album ?? "", artwork: [ { src: api.getItemImageUrl(currentTrackItem.Id, "Primary", { tag: currentTrackItem.ImageTags?.Primary, width: 96, height: 96, }), sizes: "96x96", type: "image/jpeg", }, { src: api.getItemImageUrl(currentTrackItem.Id, "Primary", { tag: currentTrackItem.ImageTags?.Primary, width: 128, height: 128, }), sizes: "128x128", type: "image/jpeg", }, { src: api.getItemImageUrl(currentTrackItem.Id, "Primary", { tag: currentTrackItem.ImageTags?.Primary, width: 192, height: 192, }), sizes: "192x192", type: "image/jpeg", }, { src: api.getItemImageUrl(currentTrackItem.Id, "Primary", { tag: currentTrackItem.ImageTags?.Primary, width: 256, height: 256, }), sizes: "256x256", type: "image/jpeg", }, { src: api.getItemImageUrl(currentTrackItem.Id, "Primary", { tag: currentTrackItem.ImageTags?.Primary, width: 384, height: 384, }), sizes: "384x384", type: "image/jpeg", }, { src: api.getItemImageUrl(currentTrackItem.Id, "Primary", { tag: currentTrackItem.ImageTags?.Primary, width: 512, height: 512, }), sizes: "512x512", type: "image/jpeg", }, ], }); console.log("Using MediaSession Api"); // navigator.mediaSession.setActionHandler("play", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("pause", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("stop", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("seekbackward", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("seekforward", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("seekto", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("previoustrack", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("nexttrack", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("skipad", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("togglecamera", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("togglemicrophone", () => { // /* Code excerpted. */ // }); // navigator.mediaSession.setActionHandler("hangup", () => { // /* Code excerpted. */ // }); } setLoading(false); }); setAudioRef(audioRef); } }, [url, tracks[currentTrack]?.Id]); const navigate = useNavigate(); const info = useMemo( () => (
{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); 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;