Question

I want the user to have the option to skip the preroll ad after a specified time (say 5 secs into the ad). Then the normal video would play. How can I achieve this? Currently I have something inline of this:

var adManager = function () {
    var adSrc = url,
        src = "http://media.w3.org/2010/05/sintel/trailer.mp4";
    var adEnded = function () {
        videoPlayer.removeEventListener("ended", adEnded, false);
        videoPlayer.removeEventListener("playing", adPlaying, false);
        $("#companionad").hide();
        videoPlayer.src = src;
        videoPlayer.load();
        videoPlayer.play();
        console.log(videoPlayer.currentTime);
    };
    var adPlaying = function () {
        var companionad = $(responseXML).find("HTMLResource").text();
        $("#companionad").html(companionad);
        console.log(videoPlayer.currentTime);
    }
    return {
        init: function () {
            videoPlayer.src = adSrc;
            videoPlayer.load();
            videoPlayer.addEventListener("ended", adEnded, false);
            videoPlayer.addEventListener("playing", adPlaying, false);
            if (videoPlayer.currentTime > 5) {
                $("#skip").show();
            }
            console.log(videoPlayer.currentTime);
        }
    };
}();
adManager.init();

What I am trying to do is:

if (videoPlayer.currentTime > 5) {
    $("#skip").show();
}

show the skip button and continue to normal video. Is there any event that is continually fired as the video play progresses?

Was it helpful?

Solution

Do your check against the media.currentTime property in a handler for the timeupdate event. See documentation:

The timeupdate event is fired when the time indicated by the currentTime attribute has been updated.

Just as an aside, this HTML5 video demo page is a really handy reference for playing around with the various properties and events available to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top