加载时在特定位置启动HTML5视频? [英] Start HTML5 video at a particular position when loading?
问题描述
我正在尝试播放HTML5视频(VP8编码)。我想要的是在某个位置玩它。让我们说50秒后的时间。
I am trying to play HTML5 video (VP8 encoded). What I want is to play it at a certain position. Let's say at time 50 seconds onward.
我试过但似乎有些问题。你能否说一下我做错了什么?
I have tried but there seem to be some problem. Can you suggest if there is something I'm doing wrong?
以下是代码:
<video id="vid1" width="640" height="360">
<source src="file.webm" type="video/webm" />
Your browser does not support the video tag.
</video>
<script>
document.getElementById('vid1').currentTime = 50;
</script>
我试过但它不起作用。视频加载时,它从刚开始播放。但是,如果我在回放期间调用此功能,就像播放视频一段时间后它可以正常工作。有什么我想念的吗?
I have tried but it doesn’t work. When video loads, it just starts playing from start. However if I call this during playback like after some time video is playing it works fine. Is there something I am missing?
推荐答案
你必须等到浏览器知道视频的持续时间才能寻求特定的时间。所以,我想你想等待'loadedmetadata'事件是这样的:
You have to wait until the browser knows the duration of the video before you can seek to a particular time. So, I think you want to wait for the 'loadedmetadata' event something like this:
document.getElementById('vid1').addEventListener('loadedmetadata', function() {
this.currentTime = 50;
}, false);
这篇关于加载时在特定位置启动HTML5视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!