mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 06:24:30 +00:00
Merge pull request #107 from shumaiOne/feat/smooth-progress-bar
refactor(webui): use requestAnimationFrame for smooth video progress bar updates
This commit is contained in:
@@ -505,12 +505,19 @@ 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,41 @@ 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,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
animationFrameId = requestAnimationFrame(updateProgress)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.isPlaying) {
|
||||||
|
animationFrameId = requestAnimationFrame(updateProgress)
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (animationFrameId) {
|
||||||
|
cancelAnimationFrame(animationFrameId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [state.isPlaying, playerRef, data.media?.metadata?.duration])
|
||||||
|
|
||||||
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