fix(player): keep transferred video at its start position
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Continuous Integration / build_web (push) Failing after 1s

The HLS wrapper can reset to the manifest start after a seek. Track and re-apply the pending transfer/resume target for 15 seconds, read progress from the real video element, and let a manual seek cancel the pending target.
This commit is contained in:
1xDed committed 2026-11-28 01:57:47 +03:00
1 parent 75b4ddd598
commit 3cfa52ffc9
1 file changed
+50 -34
+50 -34
View File
@@ -84,6 +84,16 @@ export function VideoPlayer() {
const api = useApiInContext((s) => s.api); const api = useApiInContext((s) => s.api);
const { history } = useRouter(); const { history } = useRouter();
const player = useRef<HTMLVideoElement | null>(null); const player = useRef<HTMLVideoElement | null>(null);
/** Re-apply the transfer/resume position until the HLS player stabilises. */
const startSeekRef = useRef<{ target: number; expiresAt: number } | null>(
null,
);
const getVideoElement = useCallback((): HTMLVideoElement | null => {
const shadowVideo = document
.querySelector("hls-video")
?.shadowRoot?.querySelector("video");
return (shadowVideo as HTMLVideoElement | null) ?? player.current;
}, []);
const user = useCentralStore((s) => s.currentUser); const user = useCentralStore((s) => s.currentUser);
@@ -177,19 +187,16 @@ export function VideoPlayer() {
const handleReady = async () => { const handleReady = async () => {
if (api && !isPlayerReady && player.current) { if (api && !isPlayerReady && player.current) {
const target = ticksToSec(userDataLastPlayedPositionTicks ?? 0); const target = ticksToSec(userDataLastPlayedPositionTicks ?? 0);
const seekToStartPosition = () => { if (target > 1) {
if (!player.current) return; // The HLS wrapper may reset to the manifest start after we
if (usePlaybackStore.getState().metadata.item?.Id !== itemId) return; // seek; keep enforcing the target for a short window.
const drift = Math.abs(player.current.currentTime - target); startSeekRef.current = {
if (target > 1 && drift > 5) { target,
player.current.currentTime = target; expiresAt: Date.now() + 15000,
}
}; };
player.current.currentTime = target; const videoElement = getVideoElement();
// HLS players can reset their position once the manifest loads; if (videoElement) videoElement.currentTime = target;
// re-apply the transfer/resume position after startup. }
window.setTimeout(seekToStartPosition, 700);
window.setTimeout(seekToStartPosition, 1800);
if (navigator.mediaSession && mediaSource) { if (navigator.mediaSession && mediaSource) {
navigator.mediaSession.metadata = new MediaMetadata({ navigator.mediaSession.metadata = new MediaMetadata({
@@ -240,21 +247,18 @@ export function VideoPlayer() {
registerPlayerActions({ registerPlayerActions({
seekTo: (seconds: number) => { seekTo: (seconds: number) => {
const internalPlayer = player.current; // A user seek wins over the pending transfer/resume target.
if (internalPlayer) { startSeekRef.current = null;
internalPlayer.currentTime = seconds; const videoElement = getVideoElement();
console.log("Seeking to", seconds, "seconds"); if (videoElement) {
videoElement.currentTime = seconds;
} else { } else {
console.warn("ReactPlayer is not ready yet"); console.warn("Video element is not ready yet");
} }
}, },
getCurrentTime: () => { getCurrentTime: () => {
const internalPlayer = player.current; const videoElement = getVideoElement();
if (internalPlayer) { return videoElement?.currentTime ?? 0;
return internalPlayer.currentTime ?? 0;
}
console.warn("ReactPlayer is not ready yet");
return 0;
}, },
}); });
setPlayerReady(true); setPlayerReady(true);
@@ -282,9 +286,19 @@ export function VideoPlayer() {
const lastReportTimeRef = useRef<number>(0); const lastReportTimeRef = useRef<number>(0);
const handleTimeUpdate = useCallback(async () => { const handleTimeUpdate = useCallback(async () => {
if (!player.current) return; const videoElement = getVideoElement();
if (!videoElement) return;
const currentTime = player.current.currentTime ?? 0; let currentTime = videoElement.currentTime ?? 0;
const pending = startSeekRef.current;
if (pending) {
if (Date.now() > pending.expiresAt) {
startSeekRef.current = null;
} else if (Math.abs(currentTime - pending.target) > 5) {
videoElement.currentTime = pending.target;
currentTime = pending.target;
}
}
const ticks = secToTicks(currentTime); const ticks = secToTicks(currentTime);
const now = Date.now(); const now = Date.now();
@@ -337,8 +351,9 @@ export function VideoPlayer() {
return; return;
} }
let currentTime = 0; let currentTime = 0;
if (player.current) { const videoElement = getVideoElement();
currentTime = player.current.currentTime ?? 0; if (videoElement) {
currentTime = videoElement.currentTime ?? 0;
} }
// Report Jellyfin server: Playback has been paused // Report Jellyfin server: Playback has been paused
@@ -378,8 +393,9 @@ export function VideoPlayer() {
throw Error("API is not available, cannot report playback stopped."); throw Error("API is not available, cannot report playback stopped.");
} }
let currentTime = 0; let currentTime = 0;
if (player.current) { const videoElement = getVideoElement();
currentTime = player.current.currentTime ?? 0; if (videoElement) {
currentTime = videoElement.currentTime ?? 0;
} }
// Report Jellyfin server: Playback has ended/stopped // Report Jellyfin server: Playback has ended/stopped
@@ -397,14 +413,14 @@ export function VideoPlayer() {
}, []); }, []);
const handleKeyPress = useCallback((event: KeyboardEvent) => { const handleKeyPress = useCallback((event: KeyboardEvent) => {
if (player.current) { const videoElement = getVideoElement();
if (videoElement) {
event.preventDefault(); event.preventDefault();
const currentTime = player.current.currentTime ?? 0; const currentTime = videoElement.currentTime ?? 0;
const seekTo = (time: number) => { const seekTo = (time: number) => {
if (player.current) { startSeekRef.current = null;
player.current.currentTime = time; videoElement.currentTime = time;
}
}; };
switch (event.key) { switch (event.key) {