Skip to content

Commit e414aaa

Browse files
committed
feat: video update
1 parent baeb1e4 commit e414aaa

File tree

5 files changed

+1292
-581
lines changed

5 files changed

+1292
-581
lines changed

src/components/hooks/useVideo/index.tsx

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,21 @@ const useVideo = (ref: RefObject<HTMLVideoElement | null>) => {
111111
)
112112

113113
const handleTimeControl = useCallback((e: Event) => {
114+
const target = e.target as HTMLVideoElement
115+
const nextTime = target.currentTime
116+
// Throttle frequent timeupdate to reduce re-render pressure
117+
// (especially noticeable in fullscreen with heavy UI around the video)
118+
const now = typeof performance !== 'undefined' ? performance.now() : Date.now()
119+
;(handleTimeControl as any)._lastTs = (handleTimeControl as any)._lastTs || 0
120+
const lastTs = (handleTimeControl as any)._lastTs as number
121+
if (now - lastTs < 120) return
122+
;(handleTimeControl as any)._lastTs = now
123+
114124
setVideoState((prev) => {
125+
if (Math.abs(prev.currentTime - nextTime) < 0.05) return prev
115126
return {
116127
...prev,
117-
currentTime: (e.target as HTMLVideoElement).currentTime,
128+
currentTime: nextTime,
118129
}
119130
})
120131
}, [])
@@ -190,18 +201,58 @@ const useVideo = (ref: RefObject<HTMLVideoElement | null>) => {
190201
}
191202
}, [handlePlayPauseControl, handleTimeControl, handleVolumeControl, ref])
192203

204+
const increaseVolume = useCallback(
205+
(increase = 5) => {
206+
handleVolume(increase)
207+
},
208+
[handleVolume]
209+
)
210+
211+
const decreaseVolume = useCallback(
212+
(decrease = 5) => {
213+
handleVolume(decrease * -1)
214+
},
215+
[handleVolume]
216+
)
217+
218+
const mute = useCallback(() => {
219+
handleMute(true)
220+
}, [handleMute])
221+
222+
const unmute = useCallback(() => {
223+
handleMute(false)
224+
}, [handleMute])
225+
226+
const toggleMute = useCallback(() => {
227+
handleMute(!ref.current?.muted)
228+
}, [handleMute, ref])
229+
230+
const forward = useCallback(
231+
(increase = 5) => {
232+
handleTime(increase)
233+
},
234+
[handleTime]
235+
)
236+
237+
const back = useCallback(
238+
(decrease = 5) => {
239+
handleTime(decrease * -1)
240+
},
241+
[handleTime]
242+
)
243+
193244
return {
194245
...videoState,
195246
play,
196247
pause,
197248
togglePause,
198-
increaseVolume: (increase = 5) => handleVolume(increase),
199-
decreaseVolume: (decrease = 5) => handleVolume(decrease * -1),
200-
mute: () => handleMute(true),
201-
unmute: () => handleMute(false),
202-
toggleMute: () => handleMute(!ref.current?.muted),
203-
forward: (increase = 5) => handleTime(increase),
204-
back: (decrease = 5) => handleTime(decrease * -1),
249+
increaseVolume,
250+
decreaseVolume,
251+
mute,
252+
unmute,
253+
toggleMute,
254+
forward,
255+
back,
205256
toggleFullscreen,
206257
}
207258
}

0 commit comments

Comments
 (0)