googleads / videojs-ima

IMA SDK Plugin for Video.js
Apache License 2.0
450 stars 284 forks source link

Preroll add won't pause if there are less than 3 seconds remaining #825

Closed konmavrakis closed 5 years ago

konmavrakis commented 5 years ago

We are facing an issue where we want to pause the preroll ad when play time is equal or higher than the duration time minus 2 seconds. What we want to achieve is pause the ad at the last frame. If we pause the video at minus 4 seconds, or more, the ad will pause. I've also tried to utilize the "ended" event without any success.

var player = videojs("content_video");
player.ima({ adTagUrl: adTag });

player.on("adsready", function () {
    player.ima.addEventListener(google.ima.AdEvent.Type.STARTED, function () {
        interval = setInterval(function () {
            if (player.currentTime() >= player.duration() - 2) { // here is the issue: -2 or less wont stop, -3 or more will stop
                clearInterval(interval);
                player.pause();
            }
        }, 500);
    });
});

This is a log with timestamps and the duration at (10 seconds) - 3.

currentTime: 0.796381 - duration: 7
currentTime: 1.294863 - duration: 7
currentTime: 1.79544 - duration: 7
currentTime: 2.295131 - duration: 7
currentTime: 2.793447 - duration: 7
currentTime: 3.292127 - duration: 7
currentTime: 3.790952 - duration: 7
currentTime: 4.291875 - duration: 7
currentTime: 4.797779 - duration: 7
currentTime: 5.290144 - duration: 7
currentTime: 5.80124 - duration: 7
currentTime: 6.300455 - duration: 7
currentTime: 6.798617 - duration: 7
currentTime: 7.298289 - duration: 7 // PAUSED.
currentTime: 0.750283 - duration: 8
currentTime: 1.262018 - duration: 8
currentTime: 1.760286 - duration: 8
currentTime: 2.259611 - duration: 8
currentTime: 2.758926 - duration: 8
currentTime: 3.261323 - duration: 8
currentTime: 3.756804 - duration: 8
currentTime: 4.256839 - duration: 8
currentTime: 4.755219 - duration: 8
currentTime: 5.254516 - duration: 8
currentTime: 5.753955 - duration: 8
currentTime: 6.254145 - duration: 8
currentTime: 6.753022 - duration: 8
currentTime: 7.253058 - duration: 8
currentTime: 7.75127 - duration: 8
currentTime: 8.250514 - duration: 8 // stepped into the if statement, but never paused the video

Is any event listener or action prohibiting this to work? Any guidance will be useful. Also, i've tried with interval of 1ms but still nothing.

arnaudcasame commented 5 years ago

Hi,

Your implementation makes it look like you're trying to pause the video content instead of the ad (pre-roll). I modified your code and used the player.ima instance so that the pre-roll can be paused at the specified cue point. I hope this solves your issue.

player.on("adsready", () => {
    player.ima.addEventListener(google.ima.AdEvent.Type.STARTED, () => {
      var adsManager = player.ima.getAdsManager();

        interval = setInterval(() => {
            if (adsManager.getRemainingTime() <= 2) { // here, I am using the IMA getRemainingTime method to get the ad's video remaining time
                clearInterval(interval);
                player.ima.pauseAd();
            }
        }, 500);

    });
});
konmavrakis commented 5 years ago

Hey @arnaudcasame your implementation worked great! Thanks a lot! :+1: