fix: Refine video seeking logic in playback controls to prevent overwrites and offset HLS preloader start index to avoid browser competition.

This commit is contained in:
kuekhaoyang
2025-11-23 23:18:22 +08:00
parent f0b19c1144
commit 38bad7546f
3 changed files with 16 additions and 6 deletions
@@ -64,9 +64,12 @@ export function usePlaybackControls({
setIsLoading(false);
// Fix for stuck at 00:00:00:
// If initialTime is 0, we seek to a tiny offset to help the browser/HLS buffer start.
const startPosition = initialTime > 0 ? initialTime : 0.1;
videoRef.current.currentTime = startPosition;
// Only seek if we are at the very start (to avoid overwriting a previous seek)
if (videoRef.current.currentTime < 0.5) {
// If initialTime is 0, we seek to a tiny offset to help the browser/HLS buffer start.
const startPosition = initialTime > 0 ? initialTime : 0.1;
videoRef.current.currentTime = startPosition;
}
videoRef.current.play().catch((err: Error) => {
console.warn('Autoplay was prevented:', err);
@@ -79,9 +79,12 @@ export function useMobilePlaybackControls({
setIsLoading(false);
// Fix for stuck at 00:00:00:
// If initialTime is 0, we seek to a tiny offset to help the browser/HLS buffer start.
const startPosition = initialTime > 0 ? initialTime : 0.1;
videoRef.current.currentTime = startPosition;
// Only seek if we are at the very start (to avoid overwriting a previous seek)
if (videoRef.current.currentTime < 0.5) {
// If initialTime is 0, we seek to a tiny offset to help the browser/HLS buffer start.
const startPosition = initialTime > 0 ? initialTime : 0.1;
videoRef.current.currentTime = startPosition;
}
videoRef.current.play().catch((err: Error) => {
console.warn('Autoplay was prevented:', err);
@@ -61,6 +61,10 @@ export function useHLSPreloader({ src, currentTime }: UseHLSPreloaderProps) {
}
}
// Offset start index by 3 segments to avoid competing with browser playback
// The browser needs the immediate segments NOW; we preload the future.
startIndex = Math.min(startIndex + 3, segmentsRef.current.length - 1);
if (startIndex >= segmentsRef.current.length) return;
// Check if this is sequential playback or a seek