How to pause an html 5 video at a specific time
Is there an elegan开发者_JAVA百科t way to pause html 5 video at a specific time?
I tried two ways:
- Use the timeupdate event and see if the currentTime property is greater than the desired time and make a call to pause(). This performs well in Firefox, other Browsers sometimes lag behind and render additional frames before the video really pauses.
- Use TimedTracks and a Cue with the PauseOnExit Flag set to true. Unfortunatly, no browser implements TimedTracks...
Do you have other ideas?
Cheers, Stefan
You can user the timeupdate event to check the currentTime and once it has gone over your stop time, pause it. The resolution for the timeupdate event may be up to 250ms, which isn't great.
So, alternatively, you can poll the currentTime with setInterval at a higher rate, e.g. every 50ms and pause the video when it gets past your stop time. That's probably more reliable, but creates a higher load on your browser.
A callback interface for the video element is still in development, which will eventually solve this issue.
It sounds like you want the video to play up to a certain point and then pause. This isn't possible with the current API. What you can do, however, is to use the timeupdate event to pause just before the desired time and then set currentTime to the desired time. Seeking should work when paused too, so this should pause you at the same frame every time (module browser bugs).
You could combine the two solutions Silvia suggested. If you use the timeupdate event to find when you're within 1 second of the desired time, you could then use the setInterval solution at that point. This would achieve the increased precision of the SetInterval solution, but you'd only have the higher load on your browser for a second.
精彩评论