在HTML5视频中设置currentTime的问题 [英] Issue setting currentTime in HTML5 video

查看:1505
本文介绍了在HTML5视频中设置currentTime的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的html中间有一段视频。正如你刚才所见,我没有任何来源

I have a video in the middle of my html. As you can see at first I haven't any source

<video id="videoPrincipal" src="" width="640" height="360" controls preload></video>

当我点击一个按钮时,我触发一个函数:

When I click a button I trigger a function that makes:

myVid = document.getElementById('videoPrincipal');
myVid.src = 'video.mp4';
myVid.play();
myVid.currentTime ='5';

视频开始播放正确,但我无法设置当前时间(而且我们可以做到请参阅 http://www.w3schools.com/tags/av_prop_currenttime.asp

The video starts playing correctly but I cant set the current time (and I made the same as we can see in http://www.w3schools.com/tags/av_prop_currenttime.asp)

如果我在调用play函数之前设置currentTime,它会给我同样的错误。

It gives me the same error if I set the currentTime before calling the play function.

显示的错误我在控制台中的内容如下:Uncaught InvalidStateError:尝试使用不可用或不再可用的对象。 (在currentTime行),但当我搜索这个问题,我不能与视频关联,只能与画布。

The error that shows me in the console is the following: "Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable." (in the currentTime line) but when i search this problem I cant associate with video, only with canvas.

预先感谢

推荐答案

您不必等待它开始播放,但它必须准备好播放。有这样的 canplay 活动,所以这样的事情应该有效:

You don't have to wait for it to start playing, but it does have to be ready to play. There's the canplay event to do that, so something like this should work:

myVid.play();
myVid.addEventListener('canplay', function() {
    this.currentTime = 5;
});

这篇关于在HTML5视频中设置currentTime的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆