refactor: replace wavesurfer with html audio player
This commit is contained in:
6 files changed
+187
-94
No files matched your search
@@ -132,7 +132,7 @@ export const AppBar = () => {
|
||||
location.pathname.includes("login") ||
|
||||
location.pathname.includes("setup") ||
|
||||
location.pathname.includes("server") ||
|
||||
location.pathname.includes("player") ||
|
||||
location.pathname === "/player/" ||
|
||||
location.pathname.includes("error") ||
|
||||
location.pathname === "/"
|
||||
) {
|
||||
|
||||
+68
-93
@@ -19,13 +19,13 @@ import Typography from "@mui/material/Typography";
|
||||
|
||||
import WaveSurfer from "wavesurfer.js";
|
||||
|
||||
import { useAudioPlayback } from "@/utils/store/audioPlayback";
|
||||
import { setAudioRef, useAudioPlayback } from "@/utils/store/audioPlayback";
|
||||
import useQueue from "@/utils/store/queue";
|
||||
|
||||
import { AnimatePresence } from "framer-motion";
|
||||
import "./audioPlayer.scss";
|
||||
|
||||
import { getRuntimeMusic, secToTicks } from "@/utils/date/time";
|
||||
import { getRuntimeMusic, secToTicks, ticksToSec } from "@/utils/date/time";
|
||||
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
@@ -33,22 +33,15 @@ 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 { useRouteContext } from "@tanstack/react-router";
|
||||
import { useNavigate, useRouteContext } from "@tanstack/react-router";
|
||||
|
||||
const AudioPlayer = () => {
|
||||
const api = useApiInContext((s) => s.api);
|
||||
const [
|
||||
url,
|
||||
display,
|
||||
item,
|
||||
// tracks,
|
||||
// currentTrack,
|
||||
setCurrentTrack,
|
||||
] = useAudioPlayback((state) => [
|
||||
const audioRef = useRef<HTMLAudioElement>(null!);
|
||||
const [url, display, item] = useAudioPlayback((state) => [
|
||||
state.url,
|
||||
state.display,
|
||||
state.item,
|
||||
state.playlistItemId,
|
||||
]);
|
||||
|
||||
const [tracks, currentTrack] = useQueue((state) => [
|
||||
@@ -66,93 +59,43 @@ const AudioPlayer = () => {
|
||||
enabled: Boolean(display) && Boolean(api),
|
||||
});
|
||||
|
||||
const waveSurferRef = useRef(null);
|
||||
const waveSurferContainerRef = useRef(null);
|
||||
|
||||
const playing = useMemo(() => {
|
||||
if (audioRef.current?.paused) return false;
|
||||
return true;
|
||||
}, [audioRef.current?.paused]);
|
||||
const [playerReady, setPlayerReady] = useState(false);
|
||||
const [playing, setPlaying] = useState(true);
|
||||
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(false);
|
||||
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const [showWaveform, setShowWaveform] = useState(false);
|
||||
|
||||
const [currentTime, setCurrentTime] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (display) {
|
||||
const waveSurfer = WaveSurfer.create({
|
||||
container: waveSurferContainerRef.current,
|
||||
dragToSeek: true,
|
||||
height: 25,
|
||||
cursorColor: "#fb2376",
|
||||
progressColor: "#fb2376",
|
||||
waveColor: "#ffffff2f",
|
||||
normalize: true,
|
||||
barHeight: 0.6,
|
||||
backend: "MediaElementWebAudio",
|
||||
const audioUrlRequest = new Request(audioRef.current?.dataset.src, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: api.authorizationHeader,
|
||||
},
|
||||
});
|
||||
waveSurfer.load(url);
|
||||
waveSurfer.on("load", () => {
|
||||
setLoading(true);
|
||||
});
|
||||
waveSurfer.on("ready", () => {
|
||||
waveSurferRef.current = waveSurfer;
|
||||
waveSurfer.play();
|
||||
waveSurfer.setVolume(volume);
|
||||
waveSurfer.setMuted(isMuted);
|
||||
setPlayerReady(true);
|
||||
setLoading(false);
|
||||
});
|
||||
waveSurfer.on("timeupdate", async (atime) => {
|
||||
setCurrentTime(atime);
|
||||
});
|
||||
waveSurfer.on("redraw", () => {
|
||||
setShowWaveform(true);
|
||||
});
|
||||
waveSurfer.on("play", () => {
|
||||
setPlaying(true);
|
||||
});
|
||||
waveSurfer.on("pause", () => {
|
||||
setPlaying(false);
|
||||
});
|
||||
waveSurfer.on("finish", () => {
|
||||
setPlaying(false);
|
||||
if (currentTrack !== tracks.length - 1) {
|
||||
setCurrentTrack(currentTrack + 1);
|
||||
setAudioUrl(
|
||||
`${api.basePath}/Audio/${
|
||||
tracks[currentTrack + 1].Id
|
||||
}/universal?deviceId=${api.deviceInfo.id}&userId=${
|
||||
user.data.Id
|
||||
}&api_key=${api.accessToken}`,
|
||||
);
|
||||
setAudioItem(tracks[currentTrack + 1]);
|
||||
setShowWaveform(false);
|
||||
}
|
||||
});
|
||||
waveSurfer.on("destroy", () => {
|
||||
waveSurfer.unAll();
|
||||
setLoading(true);
|
||||
});
|
||||
|
||||
return () => {
|
||||
waveSurfer.destroy();
|
||||
};
|
||||
fetch(audioUrlRequest)
|
||||
.then((res) => res)
|
||||
.then(async (result) => {
|
||||
const blob = await result.blob();
|
||||
const blobUrl = URL.createObjectURL(blob);
|
||||
audioRef.current.src = blobUrl;
|
||||
audioRef.current.play();
|
||||
setLoading(false);
|
||||
});
|
||||
setAudioRef(audioRef);
|
||||
}
|
||||
}, [url, currentTrack]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (playerReady) {
|
||||
waveSurferRef.current.setVolume(volume);
|
||||
waveSurferRef.current.setMuted(isMuted);
|
||||
}
|
||||
}, [playerReady, volume, isMuted]);
|
||||
|
||||
const handlePlayPause = () => {
|
||||
waveSurferRef.current.playPause();
|
||||
};
|
||||
const navigate = useNavigate();
|
||||
|
||||
const info = useMemo(
|
||||
() => (
|
||||
@@ -222,7 +165,14 @@ const AudioPlayer = () => {
|
||||
position: "relative",
|
||||
}}
|
||||
>
|
||||
<Fab size="small" onClick={handlePlayPause}>
|
||||
<Fab
|
||||
size="small"
|
||||
onClick={() =>
|
||||
audioRef.current.paused
|
||||
? audioRef.current.play()
|
||||
: audioRef.current.pause()
|
||||
}
|
||||
>
|
||||
<div
|
||||
className="material-symbols-rounded"
|
||||
style={{
|
||||
@@ -248,6 +198,9 @@ const AudioPlayer = () => {
|
||||
justifyContent: "flex-end",
|
||||
}}
|
||||
>
|
||||
<IconButton onClick={() => navigate({ to: "/player/audio" })}>
|
||||
<span className="material-symbols-rounded">info</span>
|
||||
</IconButton>
|
||||
<QueueButton />
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
@@ -262,7 +215,10 @@ const AudioPlayer = () => {
|
||||
value={isMuted ? 0 : volume * 100}
|
||||
step={1}
|
||||
max={100}
|
||||
onChange={(e, newVal) => setVolume(newVal / 100)}
|
||||
onChange={(e, newVal) => {
|
||||
setVolume(newVal / 100);
|
||||
audioRef.current.volume = newVal / 100;
|
||||
}}
|
||||
valueLabelDisplay="auto"
|
||||
valueLabelFormat={(value) => Math.floor(value)}
|
||||
size="small"
|
||||
@@ -325,7 +281,14 @@ const AudioPlayer = () => {
|
||||
className="audio-player glass"
|
||||
>
|
||||
{info}
|
||||
|
||||
<audio
|
||||
data-src={url}
|
||||
ref={audioRef}
|
||||
key={item?.Id}
|
||||
onTimeUpdate={(e) =>
|
||||
setProgress(secToTicks(e.currentTarget.currentTime))
|
||||
}
|
||||
/>
|
||||
<div className="audio-player-controls">
|
||||
{controls}
|
||||
<div
|
||||
@@ -344,12 +307,24 @@ const AudioPlayer = () => {
|
||||
opacity: 0.8,
|
||||
}}
|
||||
>
|
||||
{getRuntimeMusic(secToTicks(currentTime))}
|
||||
{getRuntimeMusic(progress)}
|
||||
</Typography>
|
||||
<div
|
||||
id="waveform"
|
||||
ref={waveSurferContainerRef}
|
||||
data-show={Boolean(showWaveform)}
|
||||
<Slider
|
||||
value={isScrubbing ? sliderProgress : progress}
|
||||
step={1}
|
||||
size="small"
|
||||
max={item?.RunTimeTicks}
|
||||
onChange={(e, value) => {
|
||||
setIsScrubbing(true);
|
||||
setSliderProgress(value);
|
||||
console.log(value);
|
||||
}}
|
||||
onChangeCommitted={(e, value) => {
|
||||
setIsScrubbing(false);
|
||||
// console.log(ticksToSec(value * item?.RunTimeTicks));
|
||||
setProgress(value);
|
||||
audioRef.current.currentTime = ticksToSec(value);
|
||||
}}
|
||||
/>
|
||||
<Typography
|
||||
variant="subtitle2"
|
||||
@@ -24,6 +24,7 @@ import { Route as SetupServerErrorImport } from './routes/setup/server.error'
|
||||
import { Route as SetupServerAddImport } from './routes/setup/server.add'
|
||||
import { Route as ApiSeriesIdImport } from './routes/_api/series/$id'
|
||||
import { Route as ApiPlaylistIdImport } from './routes/_api/playlist/$id'
|
||||
import { Route as ApiPlayerAudioImport } from './routes/_api/player/audio'
|
||||
import { Route as ApiPersonIdImport } from './routes/_api/person/$id'
|
||||
import { Route as ApiLoginManualImport } from './routes/_api/login/manual'
|
||||
import { Route as ApiLoginListImport } from './routes/_api/login/list'
|
||||
@@ -102,6 +103,11 @@ const ApiPlaylistIdRoute = ApiPlaylistIdImport.update({
|
||||
getParentRoute: () => ApiRoute,
|
||||
} as any)
|
||||
|
||||
const ApiPlayerAudioRoute = ApiPlayerAudioImport.update({
|
||||
path: '/player/audio',
|
||||
getParentRoute: () => ApiRoute,
|
||||
} as any)
|
||||
|
||||
const ApiPersonIdRoute = ApiPersonIdImport.update({
|
||||
path: '/person/$id',
|
||||
getParentRoute: () => ApiRoute,
|
||||
@@ -247,6 +253,13 @@ declare module '@tanstack/react-router' {
|
||||
preLoaderRoute: typeof ApiPersonIdImport
|
||||
parentRoute: typeof ApiImport
|
||||
}
|
||||
'/_api/player/audio': {
|
||||
id: '/_api/player/audio'
|
||||
path: '/player/audio'
|
||||
fullPath: '/player/audio'
|
||||
preLoaderRoute: typeof ApiPlayerAudioImport
|
||||
parentRoute: typeof ApiImport
|
||||
}
|
||||
'/_api/playlist/$id': {
|
||||
id: '/_api/playlist/$id'
|
||||
path: '/playlist/$id'
|
||||
@@ -337,6 +350,7 @@ export const routeTree = rootRoute.addChildren({
|
||||
ApiItemIdRoute,
|
||||
ApiLibraryIdRoute,
|
||||
ApiPersonIdRoute,
|
||||
ApiPlayerAudioRoute,
|
||||
ApiPlaylistIdRoute,
|
||||
ApiSeriesIdRoute,
|
||||
ApiFavoriteIndexRoute,
|
||||
@@ -380,6 +394,7 @@ export const routeTree = rootRoute.addChildren({
|
||||
"/_api/item/$id",
|
||||
"/_api/library/$id",
|
||||
"/_api/person/$id",
|
||||
"/_api/player/audio",
|
||||
"/_api/playlist/$id",
|
||||
"/_api/series/$id",
|
||||
"/_api/favorite/",
|
||||
@@ -436,6 +451,10 @@ export const routeTree = rootRoute.addChildren({
|
||||
"filePath": "_api/person/$id.tsx",
|
||||
"parent": "/_api"
|
||||
},
|
||||
"/_api/player/audio": {
|
||||
"filePath": "_api/player/audio.tsx",
|
||||
"parent": "/_api"
|
||||
},
|
||||
"/_api/playlist/$id": {
|
||||
"filePath": "_api/playlist/$id.jsx",
|
||||
"parent": "/_api"
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
.audio-info {
|
||||
&-container {
|
||||
display: flex;
|
||||
gap: 1em;
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
}
|
||||
&-image {
|
||||
width: 100%;
|
||||
aspect-ratio: 1;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
&-container {
|
||||
width: 25em;
|
||||
aspect-ratio: 1;
|
||||
position: relative;
|
||||
border-radius: $border-radius-default;
|
||||
overflow: hidden;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
import { useAudioPlayback } from "@/utils/store/audioPlayback";
|
||||
import useQueue from "@/utils/store/queue";
|
||||
import { Typography } from "@mui/material";
|
||||
import { createFileRoute } from "@tanstack/react-router";
|
||||
import React from "react";
|
||||
|
||||
import "./audio.scss";
|
||||
|
||||
export const Route = createFileRoute("/_api/player/audio")({
|
||||
component: AudioPlayerRoute,
|
||||
});
|
||||
|
||||
function AudioPlayerRoute() {
|
||||
const item = useAudioPlayback((s) => s.item);
|
||||
const api = useApiInContext((s) => s.api);
|
||||
const [queue, currentTrack] = useQueue((s) => [
|
||||
s.currentItemIndex,
|
||||
s.currentItemIndex,
|
||||
]);
|
||||
return (
|
||||
<div className="scrollY padded-top">
|
||||
<div className="audio-info-container">
|
||||
<div className="audio-info-image-container">
|
||||
{item?.ImageTags ? (
|
||||
<img
|
||||
alt={item.Name ?? "Music"}
|
||||
src={api.getItemImageUrl(item.Id, "Primary", {
|
||||
tag: item.ImageTags.Primary,
|
||||
})}
|
||||
className="audio-info-image"
|
||||
/>
|
||||
) : (
|
||||
<span className="material-symbols-rounded fill">music_note</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="audio-info">
|
||||
<Typography variant="h4" fontWeight={300}>
|
||||
{item?.Name}
|
||||
</Typography>
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
fontWeight={300}
|
||||
className="opacity-07"
|
||||
>
|
||||
by {item?.Artists?.join(", ")}
|
||||
</Typography>
|
||||
</div>
|
||||
</div>
|
||||
<div className="audio-lyrics-container"></div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2,17 +2,30 @@ import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
||||
import { create } from "zustand";
|
||||
import { setQueue } from "./queue";
|
||||
|
||||
import type React from "react";
|
||||
import { secToTicks } from "../date/time";
|
||||
|
||||
type AudioPlaybackStore = {
|
||||
display: boolean;
|
||||
url: string;
|
||||
item: BaseItemDto | undefined | null;
|
||||
playlistItemId: string | undefined;
|
||||
player: {
|
||||
currentTick: number;
|
||||
playing: boolean;
|
||||
ref: React.Ref<HTMLAudioElement> | null;
|
||||
};
|
||||
};
|
||||
export const useAudioPlayback = create<AudioPlaybackStore>(() => ({
|
||||
display: false,
|
||||
url: "",
|
||||
item: undefined,
|
||||
playlistItemId: undefined,
|
||||
player: {
|
||||
currentTick: 0,
|
||||
playing: false,
|
||||
ref: null,
|
||||
},
|
||||
}));
|
||||
|
||||
export const playAudio = (
|
||||
@@ -43,4 +56,16 @@ export const generateAudioStreamUrl = (
|
||||
};
|
||||
const urlParams = new URLSearchParams(urlOptions).toString();
|
||||
return `${basePath}/Audio/${itemId}/universal?${urlParams}`;
|
||||
};
|
||||
|
||||
export const setAudioRef = (ref) => {
|
||||
const state = useAudioPlayback.getState();
|
||||
state.player.ref = ref;
|
||||
useAudioPlayback.setState(state);
|
||||
};
|
||||
|
||||
export const setProgress = (ticks: number) => {
|
||||
const state = useAudioPlayback.getState();
|
||||
state.player.currentTick = ticks;
|
||||
useAudioPlayback.setState(state);
|
||||
};
|
||||
Reference in new issue
Block a user