Files
KVideo/components/player/hooks/desktop/usePlaybackControls.ts
T

130 lines
4.4 KiB
TypeScript

import { useCallback, useEffect } from 'react';
interface UsePlaybackControlsProps {
videoRef: React.RefObject<HTMLVideoElement | null>;
isPlaying: boolean;
setIsPlaying: (playing: boolean) => void;
setIsLoading: (loading: boolean) => void;
initialTime: number;
shouldAutoPlay: boolean;
setDuration: (duration: number) => void;
setCurrentTime: (time: number) => void;
onTimeUpdate?: (currentTime: number, duration: number) => void;
onError?: (error: string) => void;
isDraggingProgressRef: React.MutableRefObject<boolean>;
speedMenuTimeoutRef: React.MutableRefObject<NodeJS.Timeout | null>;
setPlaybackRate: (rate: number) => void;
setShowSpeedMenu: (show: boolean) => void;
}
export function usePlaybackControls({
videoRef,
isPlaying,
setIsPlaying,
setIsLoading,
initialTime,
shouldAutoPlay,
setDuration,
setCurrentTime,
onTimeUpdate,
onError,
isDraggingProgressRef,
speedMenuTimeoutRef,
setPlaybackRate,
setShowSpeedMenu
}: UsePlaybackControlsProps) {
const togglePlay = useCallback(() => {
if (!videoRef.current) return;
if (isPlaying) {
videoRef.current.pause();
} else {
videoRef.current.play();
}
}, [isPlaying, videoRef]);
const handlePlay = useCallback(() => setIsPlaying(true), [setIsPlaying]);
const handlePause = useCallback(() => setIsPlaying(false), [setIsPlaying]);
const handleTimeUpdateEvent = useCallback(() => {
if (!videoRef.current || isDraggingProgressRef.current) return;
const current = videoRef.current.currentTime;
const total = videoRef.current.duration;
setCurrentTime(current);
setDuration(total);
if (onTimeUpdate) {
onTimeUpdate(current, total);
}
}, [videoRef, isDraggingProgressRef, setCurrentTime, setDuration, onTimeUpdate]);
const handleLoadedMetadata = useCallback(() => {
if (!videoRef.current) return;
setDuration(videoRef.current.duration);
setIsLoading(false);
if (initialTime > 0) {
videoRef.current.currentTime = initialTime;
}
videoRef.current.play().catch((err: Error) => {
console.warn('Autoplay was prevented:', err);
});
}, [videoRef, setDuration, setIsLoading, initialTime]);
// Handle late initialization of initialTime (e.g. from async storage hydration)
useEffect(() => {
if (initialTime > 0 && videoRef.current) {
// Only seek if we haven't progressed far (e.g. still near start)
// This prevents jumping if the user has already started watching
if (videoRef.current.currentTime < 2) {
videoRef.current.currentTime = initialTime;
}
}
}, [initialTime, videoRef]);
// Force autoplay when shouldAutoPlay is true (for proxy retry)
useEffect(() => {
if (shouldAutoPlay && videoRef.current) {
const playPromise = videoRef.current.play();
if (playPromise !== undefined) {
playPromise.catch((err: Error) => {
console.warn('Force autoplay was prevented:', err);
});
}
}
}, [shouldAutoPlay, videoRef]);
const handleVideoError = useCallback(() => {
setIsLoading(false);
if (onError) {
onError('Video failed to load');
}
}, [setIsLoading, onError]);
const changePlaybackSpeed = useCallback((speed: number) => {
if (!videoRef.current) return;
videoRef.current.playbackRate = speed;
setPlaybackRate(speed);
setShowSpeedMenu(false);
if (speedMenuTimeoutRef.current) {
clearTimeout(speedMenuTimeoutRef.current);
}
}, [videoRef, setPlaybackRate, setShowSpeedMenu, speedMenuTimeoutRef]);
const formatTime = useCallback((seconds: number) => {
if (isNaN(seconds)) return '0:00:00';
const hours = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
return `${hours}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
}, []);
return {
togglePlay,
handlePlay,
handlePause,
handleTimeUpdateEvent,
handleLoadedMetadata,
handleVideoError,
changePlaybackSpeed,
formatTime
};
}