admin管理员组文章数量:1335618
I'm trying to run some test code using video.js
player with google IMA plugin
I used a simple example provided by the plugin's authors:
And now I try to subscribe for some of the ads-player events.
I tried to subscribe for the events in following way (changes in lines 48+ of the original sample code):
player.one(startEvent, function() {
player.ima.onAdStarted_ = function(){
console.log("Ad started");
}
player.ima.onAdPlayPauseClick_ = function(){
console.log("Ad clicked");
}
player.ima.onAdComplete_ = function(){
console.log("Ad pleted");
}
player.ima.initializeAdDisplayContainer();
player.ima.requestAds();
player.play();
});
And it captures the events correctly, but the main player is broke: after the ad is finished the IMA controls are not being disabled (they overlay the controls of the main player) and we have no control over the video.
I assume I accidently overrided some of the IMA's events and it's not working properly.
I also tried to add event listeners like that:
player.one(startEvent, function() {
player.ima.initializeAdDisplayContainer();
player.ima.addEventListener("click",function(){
console.log("Ad clicked");
});
player.ima.addEventListener(google.ima.AdEvent.Type.STARTED,function(){
console.log("Ad started");
});
player.ima.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, function(){
console.log("Ad pleted");
});
player.ima.requestAds();
player.play();
});
But it's not working.
Is there a proper way to subscribe for the IMA's events, mainly for "ad started", "ad clicked" and "ad ended" events?
I'm trying to run some test code using video.js
player with google IMA plugin
I used a simple example provided by the plugin's authors: https://github./googleads/videojs-ima/tree/master/examples/simple
And now I try to subscribe for some of the ads-player events.
I tried to subscribe for the events in following way (changes in lines 48+ of the original sample code):
player.one(startEvent, function() {
player.ima.onAdStarted_ = function(){
console.log("Ad started");
}
player.ima.onAdPlayPauseClick_ = function(){
console.log("Ad clicked");
}
player.ima.onAdComplete_ = function(){
console.log("Ad pleted");
}
player.ima.initializeAdDisplayContainer();
player.ima.requestAds();
player.play();
});
And it captures the events correctly, but the main player is broke: after the ad is finished the IMA controls are not being disabled (they overlay the controls of the main player) and we have no control over the video.
I assume I accidently overrided some of the IMA's events and it's not working properly.
I also tried to add event listeners like that:
player.one(startEvent, function() {
player.ima.initializeAdDisplayContainer();
player.ima.addEventListener("click",function(){
console.log("Ad clicked");
});
player.ima.addEventListener(google.ima.AdEvent.Type.STARTED,function(){
console.log("Ad started");
});
player.ima.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, function(){
console.log("Ad pleted");
});
player.ima.requestAds();
player.play();
});
But it's not working.
Is there a proper way to subscribe for the IMA's events, mainly for "ad started", "ad clicked" and "ad ended" events?
Share Improve this question edited May 8, 2016 at 15:25 Gacek asked May 8, 2016 at 15:11 GacekGacek 10.3k10 gold badges58 silver badges89 bronze badges2 Answers
Reset to default 6TLDR;
player.on("adsready", function(){
player.ima.addEventListener(google.ima.AdEvent.Type.CLICK, function(){
console.log(">>> ad clicked");
});
});
This solution is undocumented, your code doesn't work because when you add the event listener, adsManager
hasn't been created. Take a look at the following snippet from googleads/videojs-ima repository.
// https://github./googleads/videojs-ima/blob/master/src/videojs.ima.js#L758-L769
player.ima.addEventListener = function(event, callback) {
if (adsManager) {
adsManager.addEventListener(event, callback);
}
};
To be able to add event listener on ads manager, one have to listen to adsready
event, because it is emitted at the end of ads manager creation, look at the following snippet.
// https://github./googleads/videojs-ima/blob/master/src/videojs.ima.js#L219-L278
player.ima.onAdsManagerLoaded_ = function(adsManagerLoadedEvent) {
adsManager = adsManagerLoadedEvent.getAdsManager(
contentPlayheadTracker, adsRenderingSettings);
// other code
// ...
player.trigger('adsready');
};
Therefore to listen to google ima ad events, you must add the event listener after adsready
event emitted as follows.
player.on("adsready", function(){
player.ima.addEventListener(google.ima.AdEvent.Type.CLICK, function(){
console.log(">>> ad clicked");
});
});
OK, I managed to solve my problem.
The trick is to rewrite the plugin located in videojs-ima.js
file and there one has access to all needed events of adsManager
and adsLoader
objects.
I.e. (code added at line 208):
adsManager.addEventListener(
google.ima.AdEvent.Type.STARTED,
function(){
console.log("Ad started");
});
本文标签: javascriptvideojs playergoogle IMA adshow to subscribe for an eventStack Overflow
版权声明:本文标题:javascript - videojs player + google IMA ads - how to subscribe for an event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392139a2466199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论