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