有没有办法使用 JavaScript 在视频中“寻找”特定时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8663962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to "seek" to a certain time in a video using JavaScript?
提问by Sheehan Alam
I have an iframe with a video, for example:
我有一个带有视频的 iframe,例如:
<iframe src="http://player.vimeo.com/video/18150336" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
The source won't always be vimeo, it can be YouTube, BrightCove, Hulu etc etc.
来源并不总是 vimeo,可以是 YouTube、BrightCove、Hulu 等。
Is there a way to use jQuery/JavaScript to "seek" to a certain time in the video? For example, if I wanted to get to 3:41 inside of a video could I write code to automatically seek to that time w/o API access to the sites providing the video?
有没有办法使用 jQuery/JavaScript 来“寻找”视频中的某个时间?例如,如果我想到达视频中的 3:41,我可以编写代码来自动寻找那个时间,而无需 API 访问提供视频的网站吗?
回答by edwardsharp
Sure. You can use VideoJS. You may have a hard time getting control of the iFrame as you put it. You can embed various video hosting sites' video with VideoJS, supposedly.
当然。您可以使用 VideoJS。正如您所说的那样,您可能很难控制 iFrame。据说,您可以使用 VideoJS 嵌入各种视频托管网站的视频。
<script>
VideoJS.DOMReady(function() {
var player = VideoJS.setup("current_video");
player.play();
player.currentTime(666);
});
</script>
ref:
参考:
https://github.com/zencoder/video-js/blob/master/docs/api.md
https://github.com/zencoder/video-js/blob/master/docs/api.md
回答by alex
Unless your website also has the host, protocol and port of http://player.vimeo.com
, you can not run any code on the iframe
's document
.
除非你的网站也有主机,协议和端口http://player.vimeo.com
,您不能在运行任何代码iframe
的document
。
回答by Benjamin Deming
Unfortunately, not always. There is no single solution to seek a time in a Flashvideo if you don't know what the source of the video is.
不幸的是,并非总是如此。如果您不知道视频的来源是什么,则没有单一的解决方案可以在Flash视频中寻找时间。
If you ever encounter HTML5 video, you can try this: Start HTML5 video at a particular position when loading?
如果你遇到过 HTML5 视频,你可以试试这个:加载时在特定位置启动 HTML5 视频?
回答by Jeff Hykin
That iframe will give you trouble, however in general you can do this without a library.
该 iframe 会给您带来麻烦,但通常您可以在没有库的情况下执行此操作。
// get the video element
let video = document.getElementsByTagName('video')[0]
// jump it to a specific time
video.currentTime = 10 // the number of seconds you want it to be at