Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 video wait for readystate == 4 after setting currentTime

I'm trying to grap some images of a video that is not played. Therefore I use the HTML5 .

Because I want to grab images that haven't been played, I set

video.currentTime = y;

If I now call the grap function it won't work. Because the video.readyState is 1 (and not 4). If I add at the beginning of the grab function an alert(); it works.

I tried to loop until readyState == 4 with

while(true){
   if(video.readyState == 4){
       grapImage();
   }
}

but this ends up in an endless loop.

So how can I wait until readyState == 4?

Thanks

like image 464
fsulser Avatar asked Feb 11 '14 11:02

fsulser


Video Answer


2 Answers

Ok I solved it with the event listener seeked.

If you change the current time it changes to seeking once it's finished it goes to seeked. The loadedmetadata event is also an option, but seeked is the fastest one.

Thanks for your help.

like image 164
fsulser Avatar answered Oct 18 '22 23:10

fsulser


Check out http://www.w3.org/2010/05/video/mediaevents.html for relevent events to listen for. Try listening to the 'loadedmetadata' event, rather than setting currentTime immediately after load(), as that indicates that all duration/timing information is loaded for a video ('canplay' also works).

video.addEventListener('loadedmetadata', function () { console.log('video duration information available'); });
like image 30
Luke Avatar answered Oct 19 '22 00:10

Luke