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

查看:23
本文介绍了在 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)

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

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

在控制台中显示的错误如下:未捕获的 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天全站免登陆