如何更改暂停的 html5 视频的当前时间并从该位置开始播放
How to change current time of a paused html5 video and start playing from that position?
我在这样的页面上有一个html 5视频:
<video preload="none" id="movie" poster="http://my_poster_url.com/sourcefile.jpeg" controls>
<source id="mp4" type="video/mp4" src="http://my_mp4.com/sourcefile.mp4"></source>
<source id="webm" type="video/webm" src="http://my_webm.com/sourcefile.webm"></source>
</video>
在该视频下方,我有一个导航,您可以在其中跳到视频中的不同时间。播放视频时它可以完美运行,但是当暂停时会出现以下错误:
无效状态错误:尝试使用的对象不是 或不再可用
这里的函数:
$('#my_video_navigation a').click(function () {
var start_in_seconds=...//a variable that gets the time from a json based on the navigation index
$("#movie").get(0).currentTime = start_in_seconds;
$("#movie").get(0).play();
}
我添加了一个 if/else 语句,无论视频是否暂停:
if ($('#movie').get(0).paused) {
$("#movie").get(0).play();
}
然后尝试像上面一样设置当前时间,甚至使用设置的超时功能,以便它等到视频加载并正在播放,但这不起作用。我做错了什么或忘记了什么?
您应该
更改媒体元素的currentTime
字段,如 API 文档中所示。这里还有一个示例代码,在 MDN
作为参考,那里的示例代码:
var mediaElement = document.getElementById('mediaElementID');
mediaElement.seekable.start(); // Returns the starting time (in seconds)
mediaElement.seekable.end(); // Returns the ending time (in seconds)
mediaElement.currentTime = 122; // Seek to 122 seconds
mediaElement.played.end(); // Returns the number of seconds the browser has played
其中mediaElementID
是audio
或video
标签的 ID。
编辑:看来我误读了你的问题。我会进一步看看。
相关文章:
- 将可拖动元素与从父项位置开始的网格对齐
- 如何将开始和结束位置应用于jquery视差动画
- 在开始联系人事件上设置新的身体位置
- 在单击后创建输入时,是否可以准确地在单击发生的位置开始编辑输入
- 如何更改暂停的 html5 视频的当前时间并从该位置开始播放
- 如何让视频在特定位置开始播放
- 加载页面后,如何让视频在特定位置开始播放
- 在不以相同位置开始的setInterval内创建图像
- 如何从定义的位置开始滚动,逐渐调整粘性标题的大小
- 删除从指定位置开始出现的范围
- 使用html5视频标签从特定位置开始播放视频
- 需要修复JavaScript,以便数组从正确的位置开始
- 文本高亮显示.替换位置开始和结束处的文本
- 在确定位置开始固定位置
- 为什么这个CSS动画(transform: scale)在错误的位置开始?
- 返回给定位置的元素(但它不是从该位置开始的!)
- 从滚动位置开始获取文本
- 当从特定位置开始时,将regex匹配到输入的开始
- HTML5 音频从 Firefox 中的错误位置开始
- 如何从特定位置开始HTML 5视频