Files
Blink/src/utils/store/audioPlayback.ts
T
1xDed fcf61012db fix(ui): unify titlebar and playback state
- window buttons live in the page header on library/search pages and in a shared chrome strip where a page has no header; removed the 44px root padding toggle that made pages jump on navigation
- video player gets its own titlebar row with window controls; the audio player hides the global app bar
- starting a video stops music and vice versa; a single track queues the rest of its album, or the artist's songs when AlbumId is missing
- album page no longer crashes on albums without AlbumArtists
- queue summary is localized
2026-12-01 02:47:40 +03:00

173 lines
4.3 KiB
TypeScript

import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
import type React from "react";
import { shallow } from "zustand/shallow";
import { createWithEqualityFn } from "zustand/traditional";
import playbackProfile from "../playback-profiles";
import {
getPlayerMuted,
getPlayerVolume,
setPlayerMuted,
setPlayerVolume,
} from "../storage/player";
type AudioPlaybackStore = {
display: boolean;
url: string;
item: BaseItemDto | undefined | null;
playlistItemId: string | undefined;
/** Initial position when resuming/transferring playback. */
startTimeTicks: number;
player: {
currentTick: number;
playing: boolean;
ref: React.RefObject<HTMLAudioElement | null> | null;
volume: number;
isMuted: boolean;
};
};
export const useAudioPlayback = createWithEqualityFn<AudioPlaybackStore>(
() => ({
display: false,
url: "",
item: undefined,
playlistItemId: undefined,
startTimeTicks: 0,
player: {
currentTick: 0,
playing: false,
ref: null,
volume: 0.8,
isMuted: false,
},
}),
shallow,
);
// Initialize volume from storage
(async () => {
const volume = await getPlayerVolume("audio");
const isMuted = await getPlayerMuted("audio");
useAudioPlayback.setState((state) => ({
player: { ...state.player, volume, isMuted },
}));
})();
export const playAudio = (
url: string,
item: BaseItemDto | undefined | null,
playlistItemId?: string | undefined,
startTimeTicks = 0,
): void => {
// Only one medium at a time: starting music stops video playback.
if (typeof document !== "undefined") {
const video = (document
.querySelector("hls-video")
?.shadowRoot?.querySelector("video") ??
document.querySelector("video")) as HTMLVideoElement | null;
if (video && !video.paused) video.pause();
}
void import("./playback").then(({ usePlaybackStore }) => {
const state = usePlaybackStore.getState();
if (state.mediaSource?.id || state.playerState.isPlayerPlaying) {
usePlaybackStore.setState(usePlaybackStore.getInitialState());
}
});
useAudioPlayback.setState({
display: true,
url,
item,
playlistItemId,
startTimeTicks,
});
};
export const generateAudioStreamUrl = (
itemId: string,
userId: string,
deviceId: string,
basePath: string,
api_key: string,
) => {
const transcodingProfile = playbackProfile.TranscodingProfiles?.filter(
(val) => val.Type === "Audio" && val.Context === "Streaming",
)[0];
let directPlayContainers = "";
if (playbackProfile.DirectPlayProfiles) {
for (const p of playbackProfile.DirectPlayProfiles) {
if (p.Type === "Audio") {
if (directPlayContainers) {
directPlayContainers += `, ${p.Container}`;
} else {
directPlayContainers = p.Container ?? "";
}
if (p.AudioCodec) {
directPlayContainers += `| ${p.AudioCodec}`;
}
}
}
}
const urlOptions = {
userId,
deviceId,
ApiKey: api_key,
container: directPlayContainers,
maxAudioChannels: transcodingProfile?.MaxAudioChannels,
transcodingContainer: transcodingProfile?.Container,
transcodingProtocol: transcodingProfile?.Protocol,
audioCodec: transcodingProfile?.AudioCodec,
playSessionId: Date.now(),
startTimeTicks: 0,
enableRemoteMedia: false,
enableAudioVbrEncoding: true,
};
const urlParams = new URLSearchParams(urlOptions as any).toString();
return `${basePath}/Audio/${itemId}/universal?${urlParams}`;
};
// Player updates must replace `player` (not mutate it) so zustand notifies subscribers.
const updatePlayer = (patch: Partial<AudioPlaybackStore["player"]>) => {
useAudioPlayback.setState((state) => ({
player: { ...state.player, ...patch },
}));
};
export const setAudioRef = (ref: React.RefObject<HTMLAudioElement | null>) => {
updatePlayer({ ref });
};
export const setProgress = (ticks: number) => {
updatePlayer({ currentTick: ticks });
};
export const setVolume = (volume: number) => {
updatePlayer({ volume });
setPlayerVolume("audio", volume);
};
export const setIsMuted = (isMuted: boolean) => {
updatePlayer({ isMuted });
setPlayerMuted("audio", isMuted);
};
export const stopPlayback = () => {
const current = useAudioPlayback.getState();
useAudioPlayback.setState({
display: false,
url: "",
item: undefined,
playlistItemId: undefined,
startTimeTicks: 0,
player: {
currentTick: 0,
playing: false,
ref: null,
volume: current.player.volume,
isMuted: current.player.isMuted,
},
});
};