Search code examples
javascripthtmlvideopopcornjs

How to know whether a video is played completly?


I want to know whether the video has compltely played or not for this I have an idea that after end of video if the currenttime and duration of the video (using javascript) are same we can confirm it as video has played completely. But I cant frame it. Another thing is that are there any direct methods to known whether a video is played till end or not?

thanks in advance

awaiting replies.


Solution

  • If you're using HTML5 < video > tag (let's suppose it has "myVideo" as ID) you can get/set its time with the command

    var vid = document.getElementById("myVideo");
    vid.currentTime = 5;
    alert(vid.currentTime);
    

    and video duration with

    alert(vid.duration)
    

    There's also a direct way to check if a video, by adding a Javascript listener.

    document.getElementById('myVideo').addEventListener('ended',myHandler,false);
    function myHandler(e) {
        if(!e){
            e = window.event;
        }
        alert("Ended."); //Replace this with the code you need to execute when video ends
    }
    

    Sources and examples: currentTime, duration.