Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
by using time function . we can view ads on videos.
Posted

1 solution

I would think to solve the problem in the following way (as a first attempt):

Create a video tag, then make reference to the video tag using some javascript code.
Create a canvas and draw the video content in it, then create another canvas which will hold the advertisement image. On the desired time we will draw the second canvas content on top of the first one.
The code should look like below:

var startTime = Date.now();
$(function () {
    var canvas = document.getElementById('canvas');
    var canvas2 = document.createElement('canvas');    
    canvas2.setAttribute('id', 'canvas2');
    var ctx = canvas.getContext('2d');
    var ctx2 = canvas2.getContext('2d');
    var img = new Image();
    img.src = "...";
    img.onload = function() {
        ctx2.drawImage(img);
    }

    var video = document.getElementById('video');

    video.addEventListener('play', function () {
        var $this = this; //cache
        (function loop() {
            var currentTime = Date.now() - startTime;
            if (!$this.paused && !$this.ended) {
                if (currentTime > 3600)  {
                    ctx.drawImage(canvas2, 0, canvas.height - 100);
                } else {
                    document.removeElementById('canvas2');
                }                    

                ctx.drawImage($this, 0, 0);
                setTimeout(loop, 1000 / 30); // drawing at 30fps
            }
        })();
    }, 0);
});
​

I found a nice an thoroughly explained article at html5doctor on the process. However i didn't check their solution. The offered solution is not complete, i sketch only the basic idea how i would approach the problem.
 
Share this answer
 
v2
Comments
Abhishek Pant 30-Jul-14 15:55pm    
if you explain/refer about any article please post their links too..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900