Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

javascript - HTML5 reset video and play again

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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 smooth is by simply pausing the video before changing the currenttime property.

Like so:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...