Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell whether video is loaded or not in Javascript

So, I've been using a listener on

document.getElementById("video").buffered.length 

to see if it's greater than 0 for when a video's loaded or not. This works for a very small video, and only in Google Chrome. It doesn't work in Firefox at all. Any ideas for how to get this to work?

I essentially want to wait till 3 seperate videos are loaded to take a specific action, how do I go about this?

like image 416
Riveascore Avatar asked Dec 30 '11 23:12

Riveascore


People also ask

How do I know if my video is playing JavaScript?

You can use the . paused() method in javascript to check whether a video is playing or not. It will return true if the video is playing and return false if the video is not playing. You can get a video DOM element using its tag name or id.

How check data is loaded or not in JavaScript?

To verify if a JavaScript variable has been loaded or not, we can check if it is undefined or has a null value by doing comparison for both the values. We can also use typeof() since it returns string always to know if variable has been loaded or not.

How do I make sure JavaScript is loaded?

What is the best way to make sure javascript is running when page is fully loaded? If you mean "fully loaded" literally, i.e., all images and other resources downloaded, then you have to use an onload handler, e.g.: window. onload = function() { // Everything has loaded, so put your code here };

How do I know if my video is playing jquery?

click(function() { var video = $("#myvideo"). get(0); video. play(); $(".


2 Answers

Try this:

var video = document.getElementById("video-id-name");  if ( video.readyState === 4 ) {     // it's loaded } 

Read here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/readyState

like image 106
Šime Vidas Avatar answered Oct 14 '22 03:10

Šime Vidas


UPDATE:

As others have mentioned, my original solution below does work but it can lead to performance issues and some unpredictability in its behaviour.

Therefore I recommend listening to the loadeddata event. Read more here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/loadeddata_event

const videoElement = document.getElementById("my_video");  videoElement.addEventListener('loadeddata', (e) => {    //Video should now be loaded but we can add a second check     if(videoElement.readyState >= 3){        //your code goes here    }  }); 

==================================

INFERIOR SOLUTION:

I find using setInterval works for actively listening to when the readyState of the video changes by checking every half-second until it loads in.

checkforVideo();  function checkforVideo() {     //Every 500ms, check if the video element has loaded     var b = setInterval(()=>{         if(VideoElement.readyState >= 3){             //This block of code is triggered when the video is loaded              //your code goes here              //stop checking every half second             clearInterval(b);          }                        },500); } 

If you're not using ES6 just replace () => with function()

like image 30
Badrush Avatar answered Oct 14 '22 02:10

Badrush