Html HTML5 重置视频并再次播放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16646648/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:34:44  来源:igfitidea点击:

HTML5 reset video and play again

javascripthtmlvideo

提问by Stichy

I'm using timesheets.js and want to play a HTML5 video when the slider is active. I'm using this code:

我正在使用 timesheets.js 并希望在滑块处于活动状态时播放 HTML5 视频。我正在使用此代码:

if (document.getElementById("v_"+element.id))
{
video = document.getElementById('v_'+element.id)
video.currentTime = 0;
video.play();
}

If I'm not using "video.currentTime = 0; it works most of the time (except when videos are too long). I have to reset the video so it plays from the beginning. Now it resets the video, but it doesn't play. Any ideas?

如果我不使用“video.currentTime = 0;它大部分时间都在工作(除非视频太长)。我必须重置视频以便从头开始播放。现在它重置视频,但它没有不玩。有什么想法吗?

回答by Dylan Maxey

While Stichy posted a sufficient answer to his own question, the method video.load()causes unnecessary bandwidth to be used as it reloads the video from the server.

虽然 Stichy 对他自己的问题发布了足够的答案,但该方法video.load()会导致使用不必要的带宽,因为它会从服务器重新加载视频。

Loading videos in particular can pose a heavy load on the server. Consider you could possibly be loading many megabytes each time the video is replayed.

特别是加载视频会给服务器带来沉重的负担。考虑到每次重播视频时您可能会加载许多兆字节。

There is also a short period where the user needs to wait for the video to re-download as well.

还有一小段时间用户也需要等待视频重新下载。

The solution I have found that works most efficient and seamlessly is by simply pausing the video before changing the currenttimeproperty.

我发现最有效和最无缝的解决方案是在更改currenttime属性之前简单地暂停视频。

Like so:

像这样:

video.pause();
video.currentTime = 0;
video.play();

回答by Stichy

This solved it! I changed the video.currentTime = 0;to:

这就解决了!我将其更改video.currentTime = 0;为:

video.load();