mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +00:00
refactor(webui): use requestAnimationFrame for smooth video progress bar updates
Updates the progress bar currentTime and progress state on requestAnimationFrame loops when playing, while keeping timeupdate as a fallback when paused.
This commit is contained in:
@@ -505,15 +505,22 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
|||||||
setBuffered((bufferedEnd / duration) * 100)
|
setBuffered((bufferedEnd / duration) * 100)
|
||||||
}
|
}
|
||||||
|
|
||||||
setState((prev) => ({
|
// Only update state here if the player is paused.
|
||||||
...prev,
|
// When playing, requestAnimationFrame handles smooth updates.
|
||||||
currentTime: current,
|
if (player.paused()) {
|
||||||
duration: duration || prev.duration,
|
setState((prev) => {
|
||||||
progress: duration > 0 ? (current / duration) * 100 : 0,
|
if (prev.currentTime === current) return prev
|
||||||
}))
|
return {
|
||||||
|
...prev,
|
||||||
|
currentTime: current,
|
||||||
|
duration: duration || prev.duration,
|
||||||
|
progress: duration > 0 ? (current / duration) * 100 : 0,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if (onTimeUpdate) {
|
if (onTimeUpdate) {
|
||||||
onTimeUpdate(current)
|
onTimeUpdate(current)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -550,6 +557,45 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
|||||||
}
|
}
|
||||||
}, [data])
|
}, [data])
|
||||||
|
|
||||||
|
// Smooth progress updates using requestAnimationFrame when playing
|
||||||
|
useEffect(() => {
|
||||||
|
let animationFrameId: number
|
||||||
|
|
||||||
|
const updateProgress = () => {
|
||||||
|
const player = playerRef.current
|
||||||
|
if (player && !player.paused()) {
|
||||||
|
const current = player.currentTime() || 0
|
||||||
|
const duration = player.duration() || data.media?.metadata?.duration || 0
|
||||||
|
|
||||||
|
setState((prev) => {
|
||||||
|
if (prev.currentTime === current) return prev
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
currentTime: current,
|
||||||
|
duration: duration || prev.duration,
|
||||||
|
progress: duration > 0 ? (current / duration) * 100 : 0,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (onTimeUpdate) {
|
||||||
|
onTimeUpdate(current)
|
||||||
|
}
|
||||||
|
|
||||||
|
animationFrameId = requestAnimationFrame(updateProgress)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.isPlaying) {
|
||||||
|
animationFrameId = requestAnimationFrame(updateProgress)
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (animationFrameId) {
|
||||||
|
cancelAnimationFrame(animationFrameId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [state.isPlaying, playerRef, data.media?.metadata?.duration, onTimeUpdate])
|
||||||
|
|
||||||
const handleMouseMove = useCallback(() => {
|
const handleMouseMove = useCallback(() => {
|
||||||
// Always show controls on movement
|
// Always show controls on movement
|
||||||
setIsControlsVisible(true)
|
setIsControlsVisible(true)
|
||||||
|
|||||||
Reference in New Issue
Block a user