有什么方法可以在寻求HTMLMediaElement之前获得当前时间值?
假设我们的应用程序正在Safari上使用默认视频播放器。
当用户播放视频,然后尝试使用Seek Bar移至视频的不同位置时,似乎暂停
事件首先触发,然后我们将获得seeking
和搜索
已发射的事件。
我想知道我们是否可以在搜索之前获得currentime
值。代码> t = 42 使用seek bar,我想以某种方式获得7
作为currenttime
值。
我希望我们可以通过访问currenttime
属性 pause 事件处理程序来获得此值,该属性如下:
const video = document.querySelector('#myvideo');
video.addEventListener('pause', () => {
// I expected that the `video.currentTime` here has the "previous" position,
// but it already points to the new position
console.log(video.currentTime);
});
但是不幸的是,不幸的是currentValue < /code>当时已经更新为新值。
有什么好方法可以实现吗?
(编辑) 缓存currenttime
手动无济于事,因为显然是timeupdate
事件在暂停
事件之前发射。更具体地说,以以下代码为例,当用户尝试跳到另一个位置时,cache
和currentime
暂停> pape 处理程序似乎总是相同的。
<!DOCTYPE html>
<html>
<body>
<video
id="myvideo"
width="640"
height="360"
controls
src="video.mp4"
></video>
</body>
<script>
const video = document.querySelector("#myvideo");
let cache = 0;
video.addEventListener("timeupdate", () => {
cache = video.currentTime;
});
video.addEventListener("pause", () => {
console.log({ cache, currentTime: video.currentTime });
});
</script>
</html>
Let's say our app is using the default video player on Safari.
When a user is playing a video and then attempts to move to a different position of the video using the seek bar, it seems like pause
event is fired first, and then we'll get seeking
and seeked
events fired.
I am wondering if we can get the currentTime
value prior to the seek. For instance, assuming that a user jumps from t = 7
to t = 42
using the seek bar, I want to get 7
as the currentTime
value somehow.
I expected that we could get this value by accessing currentTime
property inside the pause
event handler that is invoked right after the seek like the following:
const video = document.querySelector('#myvideo');
video.addEventListener('pause', () => {
// I expected that the `video.currentTime` here has the "previous" position,
// but it already points to the new position
console.log(video.currentTime);
});
but unfortunately the currentValue
was already updated to the new value at that point.
Is there any good way to achieve it?
(EDIT)
Caching currentTime
manually doesn't help, because apparently a timeupdate
event fires before a pause
event. More specifically, taking the following code as an example, when a user attempts to jump to another position, cache
and currentTime
printed within the pause
handler seem always identical.
<!DOCTYPE html>
<html>
<body>
<video
id="myvideo"
width="640"
height="360"
controls
src="video.mp4"
></video>
</body>
<script>
const video = document.querySelector("#myvideo");
let cache = 0;
video.addEventListener("timeupdate", () => {
cache = video.currentTime;
});
video.addEventListener("pause", () => {
console.log({ cache, currentTime: video.currentTime });
});
</script>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为@kaiido在说“高速缓存两个值” 。
时的意思是
代码未经测试(但看起来比在评论部分中保存更好)
I think @Kaiido means this when saying "Cache two values".
Code is untested (but looks better than being kept in comments section)