fix: Prevent video from getting stuck at 00:00:00 by seeking to 0.1s on start and enable autoplay by default.

This commit is contained in:
kuekhaoyang
2025-11-23 21:59:00 +08:00
parent 3aa966f0a6
commit 95e4a733ee
3 changed files with 13 additions and 7 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ interface VideoPlayerProps {
export function VideoPlayer({ playUrl, videoId, currentEpisode, onBack }: VideoPlayerProps) {
const [videoError, setVideoError] = useState<string>('');
const [useProxy, setUseProxy] = useState(false);
const [shouldAutoPlay, setShouldAutoPlay] = useState(false);
const [shouldAutoPlay, setShouldAutoPlay] = useState(true);
// Use reactive hook to subscribe to history updates
// This ensures the component re-renders when history is hydrated from localStorage
const viewingHistory = useHistoryStore(state => state.viewingHistory);
@@ -60,9 +60,12 @@ export function usePlaybackControls({
if (!videoRef.current) return;
setDuration(videoRef.current.duration);
setIsLoading(false);
if (initialTime > 0) {
videoRef.current.currentTime = initialTime;
}
// 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;
videoRef.current.play().catch((err: Error) => {
console.warn('Autoplay was prevented:', err);
});
@@ -75,9 +75,12 @@ export function useMobilePlaybackControls({
if (!videoRef.current) return;
setDuration(videoRef.current.duration);
setIsLoading(false);
if (initialTime > 0) {
videoRef.current.currentTime = initialTime;
}
// 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;
videoRef.current.play().catch((err: Error) => {
console.warn('Autoplay was prevented:', err);
});