Embed HTML5 Video Ads

How can I insert ads into the html5 tag before the main video is played? Are there any open source tools to make this easier? Is there any link that can help me?

It works with this code:

<script type="text/javascript"> // listener function changes src function myNewSrc() { var myVideo = document.getElementsByTagName('video')[0]; myVideo.src="../main.webm"; myVideo.load(); myVideo.play(); } // function adds listener function to ended event --> function myAddListener(){ var myVideo = document.getElementsByTagName('video')[0]; myVideo.addEventListener('ended',myNewSrc,false); } </script> 

but I can not when he plays the second. He is showing a poster. How to get rid of a poster?

+4
source share
5 answers

This is a quick start cuff for a solution that should at least point you in the right direction. This gives you a singleton with an init method that, when called, sets up a preset for a specific video element.

 var adManager = function () { var vid = document.getElementById("myVid"), adSrc = "videos/epic_rap_battles_of_history_16_adolf_hitler_vs_darth_vader_2_1280x720.mp4", src; var adEnded = function () { vid.removeEventListener("ended", adEnded, false); vid.src = src; vid.load(); vid.play(); }; return { init: function () { src = vid.src; vid.src = adSrc; vid.load(); vid.addEventListener("ended", adEnded, false); } }; }(); 

However, there are a number of things that are not considered here. For example, if you set the init method, which will be called when the video starts playing, you will need to specify a flag indicating whether the game will be so that the playback handler does nothing when you switch from an ad for content (which requires "play" playback after calling load () to get continuous playback).

We use something similar in our video game project, and most video ads out there do something similar to play HTML-based videos (as opposed to playing Flash videos).

It's relatively simple, but you just need to keep track of when to activate event callbacks and when to add and remove these callbacks.

Another thing to consider is the unreliability of a “completed” event. I still do not understand when and on which platforms it constantly works, but this is a pretty well-known problem. A possible solution is to use "timeupdate" instead and check if the currentTime property is somewhere about a second less than the "duration" property, so you know you're right at the end of the video.

+4
source

Sorry, I can’t check this code right now, but theoretically this should work.

 <script> // you will want to do checking here to see if the browser supports the video element document.getElementById('video').addEventListener('ended', function() { // the ad finished playing so update the src attribute to the real video document.getElementById('video').src = 'mainvideo.webm'; }); </script> <video id="video" src="ad.webm"> </video> 
+2
source

You can see what Popcorn.js can do. This allows you to interact with Html5 video and text overlay and many other interesting things:

http://popcornjs.org/documentation

+1
source

@ natlee75 For me this did not work I changed this to the following:

 $( document ).ready(function() { var adManager = function () { var vid = document.getElementById("vid1564730217"), adSrc = "http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_1mb.mp4", src; var adEnded = function () { vid.removeEventListener("ended", adEnded, false); vid.src = src; vid.load(); vid.play(); }; return { init: function () { src = vid.src; vid.src = adSrc; vid.load(); vid.addEventListener("ended", adEnded, false); } }; }().init(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <video id="vid1564730217" src="http://techslides.com/demos/sample-videos/small.mp4" width="100%" style="max-height:600px;" poster="http://orperry.com/sample/wp-content/uploads/2015/12/sample-logo.png" controls> <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4"> Your browser does not support the video tag. </video> 
+1
source

Have you ever watched a video online and saw a banner ad displayed at the beginning of the video? Or watched a video and saw an advertisement halfway through? What about rich media ads that capture the full screen when using a mobile app?

This question was asked 8 years ago. Over the years, everything has changed, and now we have protocols such as VAST , VPAID , VMAP and MRAID .

Read about them here .

0
source

Source: https://habr.com/ru/post/1343209/


All Articles