Reset Vast ads programatically - Videojs

Updated VAST/VPAID plugin (version 2.0) now features few options to reset and reuse VAST/VPAID ads when video changed and loaded programmatically or for videos from the playlist.

Nuevo plugin has been updated not to allow video source changed while VAST/VPAID ad playing. Same concerns video from playlist. You can't switch to the new video item when ad is playing.

Here's an example of video with VAST preroll and the option to change video source. Once video source changed, VAST is reseted and reused!

Change Source

To change video source you must wait at least until VAST preroll finished.

This is the standard way to initialize a player with VAST ad.

Code snippet
<script>
  var player = videojs('player_1");
  player.nuevo({ title="video title" });
  player.vastAds ({ tagURL: "path-to-vast.xml",id:'1' });
</script>

We use simple button and a javascript function to change video source and reset VAST ad, for example:

Code snippet
  function buttonclick() {
   var new_source = {
      sources: [
        {src:"//path-to-video_720p.mp4",type:"video/mp4",res:"720",label:"720p", default:true}, 
        {src:"//path-to-ideo_360p.mp4",type:"video/mp4",res:"480",label:"480p"}, 
        {src:"/path-to-video_240p.mp4",type:"video/mp4",res:"360",label:"360p"}
     ],
     title: 'new video title'
    }
    player.changeSource( new_source);
    player.vastReset();
  }

Changing video source was described in another post, including examples with single video source, multiple video sources and with other video attributes. Once new source variable ready you can use it via the simple function player.changeSource(new_source). Resetting VAST ads to reuse it on neew video source is even more simple via the function player.vastReset().

The VAST reset function works with multiple ads defined for one video as described our demo website page . This means that if you have preroll, midroll,postroll multiple ads defined, all will be reseted to play again for new video.

If you would like to reset and reuse VAST ads for every new playlist media item, you need to listen to the playlist's new item event.
Code snippet
<script> 
player.on('playlist_newitem', function(event,data) {
	player.vastReset();
 });
</script>

The Nuevo and VAST/VPAID plugin features also more advanced VAST usage with media files loaded programmatically. Instead of simple resetting VAST it allows to load and play other VAST ad for a new video or playlist item. Instead of reset function player.vastReset(); load new VAST, for example:

Code snippet
   player.playVast ({ tagURL: "//path-to-vast-tag.xml",id:'vast_1' });
You can also load a set of multiple VAST ads, for example:
Code snippet
player.playVast ([
   {tagURL: "preroll-1-ad-tag-url", id: 'vast_1'},
   {tagURL: "midroll-1-ad-tag-url", id: 'vast_2', timeOffset:"00:15"},
   {tagURL: "midroll-2-ad-tag-url", id: 'vast_3', timeOffset:'00:45'}
]);

Please remind that possible postroll VAST ad will be skipped if this is a playlist of videos.

And finally last advanced option. Let's assume that you set 3 VAST ads for one video, a preroll video and 2 x midroll video: But you do not want to play preroll or midroll for next video from playlist, or on video loaded programmatically. This is how you can achieve it:

Code snippet
player.vastAds ([
   {tagURL: "preroll-1-ad-tag-url", id: 'vast_1', playAlways:false},
   {tagURL: "midroll-1-ad-tag-url", id: 'vast_2', timeOffset:"00:15"},
   {tagURL: "midroll-2-ad-tag-url", id: 'vast_3', timeOffset:'00:45'}
]);

Did you notice it? Option playAlways: false. This means that certain VAST ad will be skipped for the next video on playlist or for video source loaded later progrmmatically.

Nuevodevel Blog
Nuevodevel Tweeter