feat: add mediaSession api support
This commit is contained in:
7 files changed
+145
-64
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import { IconButton } from "@mui/material";
|
import { IconButton } from "@mui/material";
|
||||||
import React from "react";
|
import React, { useEffect } from "react";
|
||||||
import { useShallow } from "zustand/shallow";
|
import { useShallow } from "zustand/shallow";
|
||||||
import { usePlaybackStore } from "@/utils/store/playback";
|
import { usePlaybackStore } from "@/utils/store/playback";
|
||||||
|
|
||||||
@@ -10,6 +10,16 @@ const ForwardButton = () => {
|
|||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
navigator.mediaSession.setActionHandler("seekforward", (details) => {
|
||||||
|
if (details.seekOffset) {
|
||||||
|
seekForward(details.seekOffset);
|
||||||
|
} else {
|
||||||
|
seekForward(10); // Default to 10 seconds
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, [seekForward]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IconButton onClick={() => seekForward(15)}>
|
<IconButton onClick={() => seekForward(15)}>
|
||||||
<span className="material-symbols-rounded fill">fast_forward</span>
|
<span className="material-symbols-rounded fill">fast_forward</span>
|
||||||
|
|||||||
@@ -9,6 +9,17 @@ const RewindButton = () => {
|
|||||||
seekBackward: state.seekBackward,
|
seekBackward: state.seekBackward,
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
navigator.mediaSession.setActionHandler("seekbackward", (details) => {
|
||||||
|
if (details.seekOffset) {
|
||||||
|
seekBackward(details.seekOffset);
|
||||||
|
} else {
|
||||||
|
seekBackward(10); // Default to 10 seconds
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, [seekBackward]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
|||||||
+1
-1
@@ -41,7 +41,7 @@ const router = createRouter({
|
|||||||
defaultPreloadStaleTime: 0,
|
defaultPreloadStaleTime: 0,
|
||||||
defaultViewTransition: true,
|
defaultViewTransition: true,
|
||||||
scrollRestoration: true,
|
scrollRestoration: true,
|
||||||
scrollRestorationBehavior: "smooth",
|
scrollRestorationBehavior: "auto",
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register the router instance for type safety
|
// Register the router instance for type safety
|
||||||
|
|||||||
@@ -279,7 +279,7 @@ function ItemDetail() {
|
|||||||
const setBackdrop = useBackdropStore(useShallow((state) => state.setBackdrop));
|
const setBackdrop = useBackdropStore(useShallow((state) => state.setBackdrop));
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
if (api && item.isSuccess && item.data) {
|
if (api && item.data) {
|
||||||
if (item.data?.BackdropImageTags?.length) {
|
if (item.data?.BackdropImageTags?.length) {
|
||||||
setBackdrop(
|
setBackdrop(
|
||||||
item.data.ImageBlurHashes?.Backdrop?.[
|
item.data.ImageBlurHashes?.Backdrop?.[
|
||||||
@@ -290,7 +290,7 @@ function ItemDetail() {
|
|||||||
setBackdrop("");
|
setBackdrop("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [item.isSuccess]);
|
}, [item.data.Id]);
|
||||||
|
|
||||||
const pageRef = useRef(null);
|
const pageRef = useRef(null);
|
||||||
const { scrollYProgress } = useScroll({
|
const { scrollYProgress } = useScroll({
|
||||||
|
|||||||
@@ -1,12 +1,21 @@
|
|||||||
import React, {
|
import {
|
||||||
useState,
|
type BaseItemDto,
|
||||||
useEffect,
|
type BaseItemDtoQueryResult,
|
||||||
type ChangeEvent,
|
BaseItemKind,
|
||||||
useMemo,
|
CollectionType,
|
||||||
useCallback,
|
ItemFilter,
|
||||||
type MouseEvent,
|
ItemSortBy,
|
||||||
} from "react";
|
SortOrder,
|
||||||
|
VideoType,
|
||||||
|
} from "@jellyfin/sdk/lib/generated-client";
|
||||||
|
import { getArtistsApi } from "@jellyfin/sdk/lib/utils/api/artists-api";
|
||||||
|
import { getGenresApi } from "@jellyfin/sdk/lib/utils/api/genres-api";
|
||||||
|
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
|
||||||
|
import { getMusicGenresApi } from "@jellyfin/sdk/lib/utils/api/music-genres-api";
|
||||||
|
import { getPersonsApi } from "@jellyfin/sdk/lib/utils/api/persons-api";
|
||||||
|
import { getStudiosApi } from "@jellyfin/sdk/lib/utils/api/studios-api";
|
||||||
|
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
|
||||||
|
import { FormControl, Menu, Stack, useScrollTrigger } from "@mui/material";
|
||||||
import Button from "@mui/material/Button";
|
import Button from "@mui/material/Button";
|
||||||
import Checkbox from "@mui/material/Checkbox";
|
import Checkbox from "@mui/material/Checkbox";
|
||||||
import Chip from "@mui/material/Chip";
|
import Chip from "@mui/material/Chip";
|
||||||
@@ -18,41 +27,28 @@ import MenuItem from "@mui/material/MenuItem";
|
|||||||
import TextField from "@mui/material/TextField";
|
import TextField from "@mui/material/TextField";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
import { useQuery, useSuspenseQuery } from "@tanstack/react-query";
|
import { useQuery, useSuspenseQuery } from "@tanstack/react-query";
|
||||||
|
|
||||||
import { Card } from "@/components/card/card";
|
|
||||||
import { EmptyNotice } from "@/components/notices/emptyNotice/emptyNotice";
|
|
||||||
import { getArtistsApi } from "@jellyfin/sdk/lib/utils/api/artists-api";
|
|
||||||
import { getGenresApi } from "@jellyfin/sdk/lib/utils/api/genres-api";
|
|
||||||
import { getItemsApi } from "@jellyfin/sdk/lib/utils/api/items-api";
|
|
||||||
import { getMusicGenresApi } from "@jellyfin/sdk/lib/utils/api/music-genres-api";
|
|
||||||
import { getPersonsApi } from "@jellyfin/sdk/lib/utils/api/persons-api";
|
|
||||||
import { getStudiosApi } from "@jellyfin/sdk/lib/utils/api/studios-api";
|
|
||||||
|
|
||||||
import MusicTrack from "@/components/musicTrack";
|
|
||||||
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
|
|
||||||
import { useBackdropStore } from "@/utils/store/backdrop";
|
|
||||||
import {
|
|
||||||
type BaseItemDto,
|
|
||||||
type BaseItemDtoQueryResult,
|
|
||||||
BaseItemKind,
|
|
||||||
CollectionType,
|
|
||||||
ItemFilter,
|
|
||||||
ItemSortBy,
|
|
||||||
SortOrder,
|
|
||||||
VideoType,
|
|
||||||
} from "@jellyfin/sdk/lib/generated-client";
|
|
||||||
|
|
||||||
import { getUserLibraryApi } from "@jellyfin/sdk/lib/utils/api/user-library-api";
|
|
||||||
import { FormControl, Menu, Stack, useScrollTrigger } from "@mui/material";
|
|
||||||
import { useWindowVirtualizer } from "@tanstack/react-virtual";
|
import { useWindowVirtualizer } from "@tanstack/react-virtual";
|
||||||
import type { AxiosResponse } from "axios";
|
import type { AxiosResponse } from "axios";
|
||||||
|
import React, {
|
||||||
|
type ChangeEvent,
|
||||||
|
type MouseEvent,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
|
import { Card } from "@/components/card/card";
|
||||||
|
import MusicTrack from "@/components/musicTrack";
|
||||||
|
import { EmptyNotice } from "@/components/notices/emptyNotice/emptyNotice";
|
||||||
|
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
|
||||||
|
import { useBackdropStore } from "@/utils/store/backdrop";
|
||||||
import "./library.scss";
|
import "./library.scss";
|
||||||
|
import { createFileRoute } from "@tanstack/react-router";
|
||||||
import LibraryItemsSkeleton from "@/components/skeleton/libraryItems";
|
import LibraryItemsSkeleton from "@/components/skeleton/libraryItems";
|
||||||
import getImageUrlsApi from "@/utils/methods/getImageUrlsApi";
|
import getImageUrlsApi from "@/utils/methods/getImageUrlsApi";
|
||||||
import { getLibraryQueryOptions } from "@/utils/queries/library";
|
import { getLibraryQueryOptions } from "@/utils/queries/library";
|
||||||
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 { createFileRoute } from "@tanstack/react-router";
|
|
||||||
|
|
||||||
type SortByObject = { title: string; value: string };
|
type SortByObject = { title: string; value: string };
|
||||||
type ViewObject = { title: string; value: BaseItemKind | "Artist" };
|
type ViewObject = { title: string; value: BaseItemKind | "Artist" };
|
||||||
@@ -75,7 +71,9 @@ function Library() {
|
|||||||
|
|
||||||
const user = useCentralStore((s) => s.currentUser);
|
const user = useCentralStore((s) => s.currentUser);
|
||||||
|
|
||||||
const currentLib = useSuspenseQuery(getLibraryQueryOptions(api, user?.Id, id));
|
const currentLib = useSuspenseQuery(
|
||||||
|
getLibraryQueryOptions(api, user?.Id, id),
|
||||||
|
);
|
||||||
|
|
||||||
const genres = useQuery({
|
const genres = useQuery({
|
||||||
queryKey: ["library", "genreItem", id],
|
queryKey: ["library", "genreItem", id],
|
||||||
@@ -725,7 +723,7 @@ function Library() {
|
|||||||
threshold: 20,
|
threshold: 20,
|
||||||
});
|
});
|
||||||
|
|
||||||
const setBackdrop = useBackdropStore((s) => s.setBackdrop);
|
// const setBackdrop = useBackdropStore((s) => s.setBackdrop);
|
||||||
|
|
||||||
const backdropItems = useMemo(() => {
|
const backdropItems = useMemo(() => {
|
||||||
if (items.isSuccess) {
|
if (items.isSuccess) {
|
||||||
@@ -733,12 +731,12 @@ function Library() {
|
|||||||
(item) => item.BackdropImageTags?.length,
|
(item) => item.BackdropImageTags?.length,
|
||||||
);
|
);
|
||||||
if (temp?.[0]?.Id && api) {
|
if (temp?.[0]?.Id && api) {
|
||||||
setBackdrop(
|
// setBackdrop(
|
||||||
getImageUrlsApi(api).getItemImageUrlById(temp?.[0]?.Id, "Backdrop", {
|
// getImageUrlsApi(api).getItemImageUrlById(temp?.[0]?.Id, "Backdrop", {
|
||||||
tag: temp?.[0]?.BackdropImageTags?.[0],
|
// tag: temp?.[0]?.BackdropImageTags?.[0],
|
||||||
}),
|
// }),
|
||||||
temp?.[0]?.BackdropImageTags?.[0] ?? "none",
|
// temp?.[0]?.BackdropImageTags?.[0] ?? "none",
|
||||||
);
|
// );
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
@@ -748,18 +746,18 @@ function Library() {
|
|||||||
if (backdropItems?.length && api) {
|
if (backdropItems?.length && api) {
|
||||||
const intervalId = setInterval(() => {
|
const intervalId = setInterval(() => {
|
||||||
const itemIndex = Math.floor(Math.random() * backdropItems?.length);
|
const itemIndex = Math.floor(Math.random() * backdropItems?.length);
|
||||||
const backdropItem = backdropItems[itemIndex];
|
const _backdropItem = backdropItems[itemIndex];
|
||||||
// enqueueSnackbar("Running");
|
// enqueueSnackbar("Running");
|
||||||
setBackdrop(
|
// setBackdrop(
|
||||||
getImageUrlsApi(api).getItemImageUrlById(
|
// getImageUrlsApi(api).getItemImageUrlById(
|
||||||
backdropItem.Id ?? "",
|
// backdropItem.Id ?? "",
|
||||||
"Backdrop",
|
// "Backdrop",
|
||||||
{
|
// {
|
||||||
tag: backdropItem.BackdropImageTags?.[0],
|
// tag: backdropItem.BackdropImageTags?.[0],
|
||||||
},
|
// },
|
||||||
),
|
// ),
|
||||||
backdropItem.BackdropImageTags?.[0] ?? "none",
|
// backdropItem.BackdropImageTags?.[0] ?? "none",
|
||||||
);
|
// );
|
||||||
}, 14000); // Update backdrop every 14s
|
}, 14000); // Update backdrop every 14s
|
||||||
return () => clearInterval(intervalId);
|
return () => clearInterval(intervalId);
|
||||||
}
|
}
|
||||||
@@ -1138,9 +1136,7 @@ function Library() {
|
|||||||
control={
|
control={
|
||||||
<Checkbox
|
<Checkbox
|
||||||
checked={filters.isHD}
|
checked={filters.isHD}
|
||||||
onChange={(
|
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||||
e: ChangeEvent<HTMLInputElement>,
|
|
||||||
) => {
|
|
||||||
setFilters((s) => ({
|
setFilters((s) => ({
|
||||||
...s,
|
...s,
|
||||||
isHD: e.target.checked,
|
isHD: e.target.checked,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api/playstate-api";
|
import { getPlaystateApi } from "@jellyfin/sdk/lib/utils/api/playstate-api";
|
||||||
import CircularProgress from "@mui/material/CircularProgress";
|
import CircularProgress from "@mui/material/CircularProgress";
|
||||||
import { WebviewWindow as appWindow } from "@tauri-apps/api/webviewWindow";
|
import { WebviewWindow as appWindow } from "@tauri-apps/api/webviewWindow";
|
||||||
import React, { useCallback, useEffect, useRef, useState } from "react";
|
import React, { useCallback, useEffect, useRef } from "react";
|
||||||
import ReactPlayer from "react-player";
|
import ReactPlayer from "react-player";
|
||||||
import { secToTicks, ticksToSec } from "@/utils/date/time";
|
import { secToTicks, ticksToSec } from "@/utils/date/time";
|
||||||
import { playItemFromQueue, usePlaybackStore } from "@/utils/store/playback";
|
import { playItemFromQueue, usePlaybackStore } from "@/utils/store/playback";
|
||||||
@@ -9,6 +9,7 @@ import { playItemFromQueue, usePlaybackStore } from "@/utils/store/playback";
|
|||||||
import "./videoPlayer.scss";
|
import "./videoPlayer.scss";
|
||||||
|
|
||||||
import { PlayMethod, RepeatMode } from "@jellyfin/sdk/lib/generated-client";
|
import { PlayMethod, RepeatMode } from "@jellyfin/sdk/lib/generated-client";
|
||||||
|
import { useMutation } from "@tanstack/react-query";
|
||||||
import { createFileRoute, useRouter } from "@tanstack/react-router";
|
import { createFileRoute, useRouter } from "@tanstack/react-router";
|
||||||
import JASSUB from "jassub";
|
import JASSUB from "jassub";
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
@@ -25,6 +26,7 @@ import VideoPlayerControls from "@/components/playback/videoPlayer/controls";
|
|||||||
import LoadingIndicator from "@/components/playback/videoPlayer/LoadingIndicator";
|
import LoadingIndicator from "@/components/playback/videoPlayer/LoadingIndicator";
|
||||||
import UpNextFlyout from "@/components/playback/videoPlayer/upNextFlyout";
|
import UpNextFlyout from "@/components/playback/videoPlayer/upNextFlyout";
|
||||||
import VolumeChangeOverlay from "@/components/playback/videoPlayer/VolumeChangeOverlay";
|
import VolumeChangeOverlay from "@/components/playback/videoPlayer/VolumeChangeOverlay";
|
||||||
|
import getImageUrlsApi from "@/utils/methods/getImageUrlsApi";
|
||||||
import { useApiInContext } from "@/utils/store/api";
|
import { useApiInContext } from "@/utils/store/api";
|
||||||
import { useBackdropStore } from "@/utils/store/backdrop";
|
import { useBackdropStore } from "@/utils/store/backdrop";
|
||||||
import { useCentralStore } from "@/utils/store/central";
|
import { useCentralStore } from "@/utils/store/central";
|
||||||
@@ -110,6 +112,9 @@ export function VideoPlayer() {
|
|||||||
toggleIsPlayerFullscreen,
|
toggleIsPlayerFullscreen,
|
||||||
setIsUserHovering,
|
setIsUserHovering,
|
||||||
handleOnSeek,
|
handleOnSeek,
|
||||||
|
itemName,
|
||||||
|
isEpisode,
|
||||||
|
episodeTitle,
|
||||||
} = usePlaybackStore(
|
} = usePlaybackStore(
|
||||||
useShallow((state) => ({
|
useShallow((state) => ({
|
||||||
itemId: state.metadata.item?.Id,
|
itemId: state.metadata.item?.Id,
|
||||||
@@ -121,6 +126,9 @@ export function VideoPlayer() {
|
|||||||
isPlayerPlaying: state.playerState.isPlayerPlaying,
|
isPlayerPlaying: state.playerState.isPlayerPlaying,
|
||||||
isPlayerMuted: state.playerState.isPlayerMuted,
|
isPlayerMuted: state.playerState.isPlayerMuted,
|
||||||
isPlayerReady: state.playerState.isPlayerReady,
|
isPlayerReady: state.playerState.isPlayerReady,
|
||||||
|
itemName: state.metadata.item?.Name,
|
||||||
|
isEpisode: state.metadata.isEpisode,
|
||||||
|
episodeTitle: state.metadata.episodeTitle,
|
||||||
|
|
||||||
setPlayerReady: state.setPlayerReady,
|
setPlayerReady: state.setPlayerReady,
|
||||||
mediaSource: state.mediaSource,
|
mediaSource: state.mediaSource,
|
||||||
@@ -148,9 +156,64 @@ export function VideoPlayer() {
|
|||||||
const setBackdrop = useBackdropStore(useShallow((s) => s.setBackdrop));
|
const setBackdrop = useBackdropStore(useShallow((s) => s.setBackdrop));
|
||||||
useEffect(() => setBackdrop(""), []);
|
useEffect(() => setBackdrop(""), []);
|
||||||
|
|
||||||
|
const handlePlayNext = useMutation({
|
||||||
|
mutationKey: ["playNextButton"],
|
||||||
|
mutationFn: () => playItemFromQueue("next", user?.Id, api),
|
||||||
|
onError: (error) => console.error(error),
|
||||||
|
});
|
||||||
|
const handlePlayPrev = useMutation({
|
||||||
|
mutationKey: ["playPreviousButton"],
|
||||||
|
mutationFn: () => playItemFromQueue("previous", user?.Id, api),
|
||||||
|
onError: (error) => [console.error(error)],
|
||||||
|
});
|
||||||
|
|
||||||
const handleReady = async () => {
|
const handleReady = async () => {
|
||||||
if (api && !isPlayerReady && player.current) {
|
if (api && !isPlayerReady && player.current) {
|
||||||
player.current.currentTime = ticksToSec(userDataLastPlayedPositionTicks ?? 0);
|
player.current.currentTime = ticksToSec(userDataLastPlayedPositionTicks ?? 0);
|
||||||
|
|
||||||
|
if (navigator.mediaSession && mediaSource) {
|
||||||
|
navigator.mediaSession.metadata = new MediaMetadata({
|
||||||
|
title: isEpisode ? episodeTitle : (itemName ?? "Blink"),
|
||||||
|
album: isEpisode ? (itemName ?? "Blink") : undefined,
|
||||||
|
artwork: [
|
||||||
|
{
|
||||||
|
src: getImageUrlsApi(api).getItemImageUrlById(
|
||||||
|
itemId ?? "",
|
||||||
|
"Primary",
|
||||||
|
{
|
||||||
|
quality: 80,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
sizes: "512x512",
|
||||||
|
type: "image/png",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
navigator.mediaSession.setActionHandler("play", () => {
|
||||||
|
toggleIsPlaying();
|
||||||
|
});
|
||||||
|
navigator.mediaSession.setActionHandler("pause", () => {
|
||||||
|
toggleIsPlaying();
|
||||||
|
});
|
||||||
|
navigator.mediaSession.setActionHandler("stop", () => {
|
||||||
|
handleExitPlayer();
|
||||||
|
});
|
||||||
|
navigator.mediaSession.setActionHandler("nexttrack", () => {
|
||||||
|
if (queue?.length !== currentQueueItemIndex + 1) {
|
||||||
|
handlePlayNext.mutate();
|
||||||
|
} else {
|
||||||
|
console.warn("No next item in the queue");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
navigator.mediaSession.setActionHandler("previoustrack", () => {
|
||||||
|
if (currentQueueItemIndex > 0) {
|
||||||
|
handlePlayPrev.mutate();
|
||||||
|
} else {
|
||||||
|
console.warn("No previous item in the queue");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
registerPlayerActions({
|
registerPlayerActions({
|
||||||
seekTo: (seconds: number) => {
|
seekTo: (seconds: number) => {
|
||||||
const internalPlayer = player.current;
|
const internalPlayer = player.current;
|
||||||
@@ -377,7 +440,6 @@ export function VideoPlayer() {
|
|||||||
// }
|
// }
|
||||||
// }, [playerVolume]);
|
// }, [playerVolume]);
|
||||||
|
|
||||||
const [_areControlsVisible, _setAreControlsVisiblee] = useState(false);
|
|
||||||
const idleTimerRef = useRef<NodeJS.Timeout | null>(null);
|
const idleTimerRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
// Function to clear the existing idle timer
|
// Function to clear the existing idle timer
|
||||||
|
|||||||
@@ -630,6 +630,8 @@ export const playItem = (args: {
|
|||||||
state.metadata = args.metadata;
|
state.metadata = args.metadata;
|
||||||
state.playerState.currentTime = args.userDataPlayedPositionTicks;
|
state.playerState.currentTime = args.userDataPlayedPositionTicks;
|
||||||
state.userId = args.userId;
|
state.userId = args.userId;
|
||||||
|
state.playerState.isPlayerPlaying = true;
|
||||||
|
state.playerState.isPlayerReady = false; // Reset player ready state
|
||||||
});
|
});
|
||||||
|
|
||||||
setQueue(args.queueItems, args.queueItemIndex ?? 0);
|
setQueue(args.queueItems, args.queueItemIndex ?? 0);
|
||||||
|
|||||||
Reference in new issue
Block a user