diff --git a/src/components/playback/audioPlayer/index.tsx b/src/components/playback/audioPlayer/index.tsx index fe2d908..683f46e 100644 --- a/src/components/playback/audioPlayer/index.tsx +++ b/src/components/playback/audioPlayer/index.tsx @@ -2,7 +2,10 @@ import type { BaseItemDto, SessionInfoDto, } from "@jellyfin/sdk/lib/generated-client"; -import { PlaystateCommand } from "@jellyfin/sdk/lib/generated-client"; +import { + GeneralCommandType, + PlaystateCommand, +} from "@jellyfin/sdk/lib/generated-client"; import { getSessionApi } from "@jellyfin/sdk/lib/utils/api/session-api"; import Divider from "@mui/material/Divider"; import IconButton from "@mui/material/IconButton"; @@ -77,6 +80,10 @@ const AudioPlayer = () => { const [progress, setProgress] = useState(0); const [selectedRemoteId, setSelectedRemoteId] = useState(null); const [moreAnchor, setMoreAnchor] = useState(null); + const [remoteVolumeDraft, setRemoteVolumeDraft] = useState( + null, + ); + useEffect(() => setRemoteVolumeDraft(null), [selectedRemoteId]); const sessions = useQuery({ queryKey: ["remoteSessions"], @@ -136,6 +143,14 @@ const AudioPlayer = () => { const isPlaying = remote ? !remote.PlayState?.IsPaused : playing; + const remoteRepeat = remote?.PlayState?.RepeatMode ?? "RepeatNone"; + const remoteShuffled = remote?.PlayState?.PlaybackOrder === "Shuffle"; + const remoteVolume = + remoteVolumeDraft ?? + (remote?.PlayState?.VolumeLevel != null + ? remote.PlayState.VolumeLevel / 100 + : 1); + const remoteMuted = remote?.PlayState?.IsMuted ?? false; const displayProgress = remote ? (remote.PlayState?.PositionTicks ?? 0) : progress; @@ -366,8 +381,60 @@ const AudioPlayer = () => { if (remote) remoteCommand(PlaystateCommand.PreviousTrack); else if (api && user?.Id) playItemFromQueue("previous", user.Id, api); }; - const handleShuffle = () => shuffleUpcoming(); - const handleRepeat = () => cycleRepeatMode(); + const sendGeneral = async ( + name: GeneralCommandType, + args?: Record, + ) => { + if (!api || !remote?.Id) return; + try { + await getSessionApi(api).sendFullGeneralCommand({ + sessionId: remote.Id, + generalCommand: { + Name: name, + Arguments: args, + ControllingUserId: user?.Id, + }, + }); + await sessions.refetch(); + } catch (error) { + console.error("Remote general command failed", error); + enqueueSnackbar("Remote command failed", { variant: "error" }); + } + }; + + const handleShuffle = () => { + if (remote) { + sendGeneral(GeneralCommandType.SetShuffleQueue, { + ShuffleMode: remoteShuffled ? "Sorted" : "Shuffle", + }); + } else { + shuffleUpcoming(); + } + }; + const handleRepeat = () => { + if (remote) { + const next = + remoteRepeat === "RepeatNone" + ? "RepeatAll" + : remoteRepeat === "RepeatAll" + ? "RepeatOne" + : "RepeatNone"; + sendGeneral(GeneralCommandType.SetRepeatMode, { RepeatMode: next }); + } else { + cycleRepeatMode(); + } + }; + const handleMute = () => { + if (remote) { + sendGeneral( + remoteMuted + ? GeneralCommandType.Unmute + : GeneralCommandType.Mute, + ); + } else { + handleMuteToggle(); + } + }; const handleRemoteSeek = (ticks: number) => { remoteCommand(PlaystateCommand.Seek, Math.round(ticks)); }; @@ -454,13 +521,15 @@ const AudioPlayer = () => {
- {!remote && ( - - - shuffle - - - )} + + + shuffle + + skip_previous @@ -479,27 +548,37 @@ const AudioPlayer = () => { skip_next - {!remote && ( - + - - - {repeatMode === "one" ? "repeat_one" : "repeat"} - - - - )} + + {(remote ? remoteRepeat === "RepeatOne" : repeatMode === "one") + ? "repeat_one" + : "repeat"} + + +
{
- {remote ? ( + {remote && ( { - ) : ( - <> - - - - - {isMuted || volume === 0 ? "volume_off" : "volume_up"} - - - - { - const next = Array.isArray(value) ? value[0] : value; - handleVolumeChange(next); - }} - /> - )} + {!remote && } + + + + {remote + ? remoteMuted || remoteVolume === 0 + ? "volume_off" + : "volume_up" + : isMuted || volume === 0 + ? "volume_off" + : "volume_up"} + + + + { + const next = Array.isArray(value) ? value[0] : value; + if (remote) { + setRemoteVolumeDraft(next); + } else { + handleVolumeChange(next); + } + }} + onChangeCommitted={(_, value) => { + if (!remote) return; + const next = Array.isArray(value) ? value[0] : value; + setRemoteVolumeDraft(null); + sendGeneral(GeneralCommandType.SetVolume, { + Volume: String(Math.round(next * 100)), + }); + }} + />