【发布时间】:2020-11-21 21:08:05
【问题描述】:
代码
这是视频元素:
<video
ref={videoRef}
onDurationChange={onSetVideoDuration}
onTimeUpdate={onSetVideoTimestamp}
width="100%"
controls
>
函数onSetVideoTimestamp是用来设置视频时间戳状态变化时的:
const onSetVideoTimestamp = (event: SyntheticEvent<HTMLVideoElement, Event>) => {
const timestamp = event.currentTarget.currentTime
setVideoTimestamp(Math.floor(timestamp))
}
这就是 videoTimestamp 和 setVideoTimestamp 的声明方式:
const [videoTimestamp, setVideoTimestamp] = useState(0)
什么是有效的
基本上,上面的代码工作,这意味着时间戳状态设置正确。
问题
-
问题在于,由于视频正在播放,时间戳状态会不断更新,这会导致视频始终暂停和取消暂停。总之,视频再现并不流畅。
-
另一个问题是,如果我设置为每 60 秒更新一次状态,使用以下代码:
const onSetVideoTimestamp = (event: SyntheticEvent<HTMLVideoElement, Event>) => {
if(event.currentTarget.currentTime > videoTimestamp + 60) {
const timestamp = event.currentTarget.currentTime
setVideoTimestamp(Math.floor(timestamp))
}
}
STILL视频在播放 60 秒后暂停和取消暂停 - 在那一刻。
【问题讨论】:
标签: reactjs react-hooks react-state