fix: audio queue not moving after 2 changes #182
This commit is contained in:
5 files changed
+63
-43
No files matched your search
@@ -17,6 +17,7 @@ import getImageUrlsApi from "@/utils/methods/getImageUrlsApi";
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
const QueueButton = () => {
|
||||
const api = useApiInContext((s) => s.api);
|
||||
if (!api) return null;
|
||||
const [queueItems, currentItemIndex] = useQueue((state) => [
|
||||
state.tracks,
|
||||
state.currentItemIndex,
|
||||
@@ -76,7 +77,7 @@ const QueueButton = () => {
|
||||
position: "relative",
|
||||
}}
|
||||
>
|
||||
{queueItems[currentItemIndex]?.Id && (
|
||||
{queueItems?.[currentItemIndex]?.Id && (
|
||||
<>
|
||||
<Typography px="1em" mb={2} variant="h5" fontWeight={300}>
|
||||
Currently Playing:
|
||||
@@ -111,11 +112,11 @@ const QueueButton = () => {
|
||||
<img
|
||||
className="queue-item-image"
|
||||
src={getImageUrlsApi(api).getItemImageUrlById(
|
||||
queueItems[currentItemIndex]?.AlbumId,
|
||||
queueItems?.[currentItemIndex]?.AlbumId ?? "",
|
||||
"Primary",
|
||||
{
|
||||
tag: queueItems[currentItemIndex]
|
||||
.AlbumPrimaryImageTag[0],
|
||||
tag: queueItems?.[currentItemIndex]
|
||||
.AlbumPrimaryImageTag?.[0],
|
||||
},
|
||||
)}
|
||||
alt={queueItems[currentItemIndex].Name ?? "image"}
|
||||
@@ -163,14 +164,14 @@ const QueueButton = () => {
|
||||
</MenuItem>
|
||||
</>
|
||||
)}
|
||||
{queueItems.slice(currentItemIndex + 1, queueItems.length - 1)
|
||||
{queueItems?.slice(currentItemIndex + 1, queueItems.length - 1)
|
||||
.length > 0 && (
|
||||
<Typography px="1em" my={2} variant="h5" fontWeight={300}>
|
||||
Queue:
|
||||
</Typography>
|
||||
)}
|
||||
{queueItems
|
||||
.slice(currentItemIndex + 1, queueItems.length - 1)
|
||||
?.slice(currentItemIndex + 1, queueItems.length - 1)
|
||||
.map((item, index) => {
|
||||
return (
|
||||
<MenuItem
|
||||
|
||||
@@ -22,13 +22,12 @@ 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<HTMLAudioElement>(null!);
|
||||
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||
const [url, display, item] = useAudioPlayback((state) => [
|
||||
state.url,
|
||||
state.display,
|
||||
@@ -46,15 +45,12 @@ const AudioPlayer = () => {
|
||||
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, {
|
||||
@@ -82,7 +78,7 @@ const AudioPlayer = () => {
|
||||
|
||||
const info = useMemo(
|
||||
() =>
|
||||
!!api?.configuration && (
|
||||
api && (
|
||||
<div
|
||||
// initial={{
|
||||
// filter: "opacity(0)",
|
||||
@@ -100,7 +96,7 @@ const AudioPlayer = () => {
|
||||
alt={tracks?.[currentTrack]?.Name ?? "track"}
|
||||
className="audio-player-image"
|
||||
src={getImageUrlsApi(api).getItemImageUrlById(
|
||||
(!item?.ImageTags?.Primary ? item?.AlbumId : item?.Id) ?? "",
|
||||
(!item?.ImageTags?.Primary ? item?.AlbumId : item.Id) ?? "",
|
||||
"Primary",
|
||||
{
|
||||
quality: 85,
|
||||
@@ -152,9 +148,9 @@ const AudioPlayer = () => {
|
||||
<Fab
|
||||
size="small"
|
||||
onClick={() =>
|
||||
audioRef.current.paused
|
||||
audioRef.current?.paused
|
||||
? audioRef.current.play()
|
||||
: audioRef.current.pause()
|
||||
: audioRef.current?.pause()
|
||||
}
|
||||
>
|
||||
<div
|
||||
@@ -170,7 +166,7 @@ const AudioPlayer = () => {
|
||||
<PlayNextButton />
|
||||
</div>
|
||||
),
|
||||
[tracks?.[currentTrack]?.Id, loading, playing, tracks?.length],
|
||||
[tracks?.[currentTrack]?.Id, playing, tracks?.length],
|
||||
);
|
||||
|
||||
const buttons = useMemo(
|
||||
@@ -187,7 +183,7 @@ const AudioPlayer = () => {
|
||||
</IconButton>
|
||||
<QueueButton />
|
||||
<IconButton
|
||||
onClick={(e) => {
|
||||
onClick={() => {
|
||||
setIsMuted(!isMuted);
|
||||
}}
|
||||
>
|
||||
@@ -200,7 +196,13 @@ const AudioPlayer = () => {
|
||||
step={1}
|
||||
max={100}
|
||||
onChange={(_, newVal) => {
|
||||
Array.isArray(newVal) ? setVolume(newVal[0] / 100) : setVolume(newVal / 100);
|
||||
if (!audioRef.current) {
|
||||
console.error("Audio ref not found");
|
||||
return;
|
||||
}
|
||||
Array.isArray(newVal)
|
||||
? setVolume(newVal[0] / 100)
|
||||
: setVolume(newVal / 100);
|
||||
if (Array.isArray(newVal)) {
|
||||
audioRef.current.volume = newVal[0] / 100;
|
||||
} else {
|
||||
@@ -248,13 +250,14 @@ const AudioPlayer = () => {
|
||||
[volume, isMuted, currentTrack],
|
||||
);
|
||||
|
||||
if (audioRef.current) {
|
||||
audioRef.current.addEventListener("ended", () => {
|
||||
if (tracks?.[currentTrack + 1]?.Id) {
|
||||
playItemFromQueue("next", user?.Id, api);
|
||||
}
|
||||
});
|
||||
}
|
||||
// if (audioRef.current) {
|
||||
// audioRef.current.addEventListener("change", () => {
|
||||
// console.log("Audio Player changed state : ", currentTrack);
|
||||
// });
|
||||
// audioRef.current.addEventListener("ended", () => {
|
||||
|
||||
// });
|
||||
// }
|
||||
|
||||
return (
|
||||
<AnimatePresence mode="sync">
|
||||
@@ -286,10 +289,15 @@ const AudioPlayer = () => {
|
||||
autoPlay
|
||||
src={url}
|
||||
ref={audioRef}
|
||||
key={item?.Id}
|
||||
key={item?.Id ?? "noItem"}
|
||||
onTimeUpdate={(e) =>
|
||||
setProgress(secToTicks(e.currentTarget.currentTime))
|
||||
}
|
||||
onEnded={() => {
|
||||
if (tracks?.[currentTrack + 1]?.Id && api && user?.Id) {
|
||||
playItemFromQueue("next", user.Id, api);
|
||||
}
|
||||
}}
|
||||
id="audio-player"
|
||||
/>
|
||||
<div className="audio-player-controls">
|
||||
@@ -324,8 +332,12 @@ const AudioPlayer = () => {
|
||||
: setSliderProgress(value);
|
||||
}}
|
||||
onChangeCommitted={(_, value) => {
|
||||
if (!audioRef.current) {
|
||||
console.error("Audio ref not found");
|
||||
return;
|
||||
}
|
||||
setIsScrubbing(false);
|
||||
// console.log(ticksToSec(value * item?.RunTimeTicks));
|
||||
// console.log(ticksToSec(value * item.RunTimeTicks));
|
||||
Array.isArray(value)
|
||||
? setProgress(value[0])
|
||||
: setProgress(value);
|
||||
|
||||
@@ -231,11 +231,8 @@ function MusicAlbumTitlePage() {
|
||||
<div className="item-info-track-container ">
|
||||
{allDiscs.length > 1 ? (
|
||||
allDiscs.map((disc) => (
|
||||
<>
|
||||
<div
|
||||
className="item-info-disc-header flex flex-row flex-align-center"
|
||||
key={disc}
|
||||
>
|
||||
<div className="item-info-disc-container" key={disc}>
|
||||
<div className="item-info-disc-header flex flex-row flex-align-center">
|
||||
<span
|
||||
className="material-symbols-rounded"
|
||||
style={{ fontSize: "2em" }}
|
||||
@@ -244,7 +241,7 @@ function MusicAlbumTitlePage() {
|
||||
</span>
|
||||
<Typography variant="h5">Disc {disc}</Typography>
|
||||
</div>
|
||||
<div key={disc} className="item-info-track header">
|
||||
<div className="item-info-track header">
|
||||
<span className="material-symbols-rounded index">
|
||||
tag
|
||||
</span>
|
||||
@@ -314,7 +311,7 @@ function MusicAlbumTitlePage() {
|
||||
</div>
|
||||
),
|
||||
)}
|
||||
</>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<>
|
||||
|
||||
@@ -15,7 +15,7 @@ import type IntroMediaInfo from "../types/introMediaInfo";
|
||||
import type subtitlePlaybackInfo from "../types/subtitlePlaybackInfo";
|
||||
import { axiosClient } from "./api";
|
||||
import { generateAudioStreamUrl, playAudio } from "./audioPlayback";
|
||||
import useQueue, { setQueue, setTrackIndex } from "./queue";
|
||||
import useQueue, { setQueue } from "./queue";
|
||||
|
||||
type PlaybackStore = {
|
||||
itemName: string | React.Component | undefined | null;
|
||||
@@ -135,19 +135,26 @@ export const playItemFromQueue = async (
|
||||
: index === "previous"
|
||||
? currentItemIndex - 1
|
||||
: index;
|
||||
const prevItem = queueItems[currentItemIndex];
|
||||
const prevItem = queueItems?.[currentItemIndex];
|
||||
const prevPlaySessionId = usePlaybackStore.getState().playsessionId;
|
||||
const prevMediaSourceId = usePlaybackStore.getState().mediaSource.id;
|
||||
const item = queueItems[requestedItemIndex];
|
||||
if (item.Type === "Audio") {
|
||||
const item = queueItems?.[requestedItemIndex];
|
||||
|
||||
console.log("requestedItemIndex", queueItems);
|
||||
|
||||
if (!item?.Id) {
|
||||
console.error("No item found in queue");
|
||||
return;
|
||||
}
|
||||
if (item?.Type === "Audio" && userId) {
|
||||
const playbackUrl = generateAudioStreamUrl(
|
||||
item.Id,
|
||||
userId,
|
||||
api?.deviceInfo.id,
|
||||
api?.basePath,
|
||||
api?.accessToken,
|
||||
api.deviceInfo.id,
|
||||
api.basePath,
|
||||
api.accessToken,
|
||||
);
|
||||
console.log(item);
|
||||
// console.log(item);
|
||||
playAudio(playbackUrl, item, undefined);
|
||||
setQueue(queueItems, requestedItemIndex);
|
||||
} else {
|
||||
|
||||
@@ -4,6 +4,9 @@ import { createWithEqualityFn } from "zustand/traditional";
|
||||
|
||||
interface QueueStore {
|
||||
tracks: BaseItemDto[] | undefined;
|
||||
/**
|
||||
* This index number should be equal to index of the item in tracks array to allow seamless playback of items having broken IndexNumber field.
|
||||
*/
|
||||
currentItemIndex: number;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user