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
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:
1 parent
75b4ddd598
commit
3cfa52ffc9
1 file changed
+51
-35
@@ -84,6 +84,16 @@ export function VideoPlayer() {
|
||||
const api = useApiInContext((s) => s.api);
|
||||
const { history } = useRouter();
|
||||
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);
|
||||
|
||||
@@ -177,19 +187,16 @@ export function VideoPlayer() {
|
||||
const handleReady = async () => {
|
||||
if (api && !isPlayerReady && player.current) {
|
||||
const target = ticksToSec(userDataLastPlayedPositionTicks ?? 0);
|
||||
const seekToStartPosition = () => {
|
||||
if (!player.current) return;
|
||||
if (usePlaybackStore.getState().metadata.item?.Id !== itemId) return;
|
||||
const drift = Math.abs(player.current.currentTime - target);
|
||||
if (target > 1 && drift > 5) {
|
||||
player.current.currentTime = target;
|
||||
}
|
||||
};
|
||||
player.current.currentTime = target;
|
||||
// HLS players can reset their position once the manifest loads;
|
||||
// re-apply the transfer/resume position after startup.
|
||||
window.setTimeout(seekToStartPosition, 700);
|
||||
window.setTimeout(seekToStartPosition, 1800);
|
||||
if (target > 1) {
|
||||
// The HLS wrapper may reset to the manifest start after we
|
||||
// seek; keep enforcing the target for a short window.
|
||||
startSeekRef.current = {
|
||||
target,
|
||||
expiresAt: Date.now() + 15000,
|
||||
};
|
||||
const videoElement = getVideoElement();
|
||||
if (videoElement) videoElement.currentTime = target;
|
||||
}
|
||||
|
||||
if (navigator.mediaSession && mediaSource) {
|
||||
navigator.mediaSession.metadata = new MediaMetadata({
|
||||
@@ -240,21 +247,18 @@ export function VideoPlayer() {
|
||||
|
||||
registerPlayerActions({
|
||||
seekTo: (seconds: number) => {
|
||||
const internalPlayer = player.current;
|
||||
if (internalPlayer) {
|
||||
internalPlayer.currentTime = seconds;
|
||||
console.log("Seeking to", seconds, "seconds");
|
||||
// A user seek wins over the pending transfer/resume target.
|
||||
startSeekRef.current = null;
|
||||
const videoElement = getVideoElement();
|
||||
if (videoElement) {
|
||||
videoElement.currentTime = seconds;
|
||||
} else {
|
||||
console.warn("ReactPlayer is not ready yet");
|
||||
console.warn("Video element is not ready yet");
|
||||
}
|
||||
},
|
||||
getCurrentTime: () => {
|
||||
const internalPlayer = player.current;
|
||||
if (internalPlayer) {
|
||||
return internalPlayer.currentTime ?? 0;
|
||||
}
|
||||
console.warn("ReactPlayer is not ready yet");
|
||||
return 0;
|
||||
const videoElement = getVideoElement();
|
||||
return videoElement?.currentTime ?? 0;
|
||||
},
|
||||
});
|
||||
setPlayerReady(true);
|
||||
@@ -282,9 +286,19 @@ export function VideoPlayer() {
|
||||
const lastReportTimeRef = useRef<number>(0);
|
||||
|
||||
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 now = Date.now();
|
||||
@@ -337,8 +351,9 @@ export function VideoPlayer() {
|
||||
return;
|
||||
}
|
||||
let currentTime = 0;
|
||||
if (player.current) {
|
||||
currentTime = player.current.currentTime ?? 0;
|
||||
const videoElement = getVideoElement();
|
||||
if (videoElement) {
|
||||
currentTime = videoElement.currentTime ?? 0;
|
||||
}
|
||||
// Report Jellyfin server: Playback has been paused
|
||||
|
||||
@@ -378,8 +393,9 @@ export function VideoPlayer() {
|
||||
throw Error("API is not available, cannot report playback stopped.");
|
||||
}
|
||||
let currentTime = 0;
|
||||
if (player.current) {
|
||||
currentTime = player.current.currentTime ?? 0;
|
||||
const videoElement = getVideoElement();
|
||||
if (videoElement) {
|
||||
currentTime = videoElement.currentTime ?? 0;
|
||||
}
|
||||
|
||||
// Report Jellyfin server: Playback has ended/stopped
|
||||
@@ -397,14 +413,14 @@ export function VideoPlayer() {
|
||||
}, []);
|
||||
|
||||
const handleKeyPress = useCallback((event: KeyboardEvent) => {
|
||||
if (player.current) {
|
||||
const videoElement = getVideoElement();
|
||||
if (videoElement) {
|
||||
event.preventDefault();
|
||||
const currentTime = player.current.currentTime ?? 0;
|
||||
const currentTime = videoElement.currentTime ?? 0;
|
||||
|
||||
const seekTo = (time: number) => {
|
||||
if (player.current) {
|
||||
player.current.currentTime = time;
|
||||
}
|
||||
startSeekRef.current = null;
|
||||
videoElement.currentTime = time;
|
||||
};
|
||||
|
||||
switch (event.key) {
|
||||
|
||||
Reference in new issue
Block a user