-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathads_controller.js
executable file
·128 lines (113 loc) · 3.86 KB
/
ads_controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2013 Google Inc. All Rights Reserved.
// You may study, modify, and use this example for any purpose.
// Note that this example is provided "as is", WITHOUT WARRANTY
// of any kind either expressed or implied.
var AdsController = function(controller, player) {
this.controller = controller;
this.player = player;
this.adDisplayContainer =
new google.ima.AdDisplayContainer(this.player.adContainer);
this.adsLoader = new google.ima.AdsLoader(this.adDisplayContainer);
this.adsManager = null;
this.adsLoader.addEventListener(
google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,
this.onAdsManagerLoaded,
false,
this);
this.adsLoader.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
this.onAdError,
false,
this);
};
// On iOS and Android devices, video playback must begin in a user action.
// AdDisplayContainer provides a initialize() API to be called at appropriate
// time.
// This should be called when the user clicks or taps.
AdsController.prototype.initialUserAction = function() {
this.adDisplayContainer.initialize();
};
AdsController.prototype.requestAds = function(adTagUrl) {
var adsRequest = new google.ima.AdsRequest();
adsRequest.adTagUrl = adTagUrl;
adsRequest.linearAdSlotWidth = this.player.width;
adsRequest.linearAdSlotHeight = this.player.height;
adsRequest.nonLinearAdSlotWidth = this.player.width;
adsRequest.nonLinearAdSlotHeight = this.player.height;
this.adsLoader.requestAds(adsRequest);
};
AdsController.prototype.onAdsManagerLoaded = function(adsManagerLoadedEvent) {
this.controller.log('Ads loaded.');
this.adsManager = adsManagerLoadedEvent.getAdsManager(
this.player.contentPlayer);
this.processAdsManager(this.adsManager);
};
AdsController.prototype.processAdsManager = function(adsManager) {
// Attach the pause/resume events.
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,
this.onContentPauseRequested,
false,
this);
adsManager.addEventListener(
google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,
this.onContentResumeRequested,
false,
this);
// Handle errors.
adsManager.addEventListener(
google.ima.AdErrorEvent.Type.AD_ERROR,
this.onAdError,
false,
this);
var events = [google.ima.AdEvent.Type.ALL_ADS_COMPLETED,
google.ima.AdEvent.Type.CLICK,
google.ima.AdEvent.Type.COMPLETE,
google.ima.AdEvent.Type.FIRST_QUARTILE,
google.ima.AdEvent.Type.LOADED,
google.ima.AdEvent.Type.MIDPOINT,
google.ima.AdEvent.Type.PAUSED,
google.ima.AdEvent.Type.STARTED,
google.ima.AdEvent.Type.THIRD_QUARTILE];
for (var index in events) {
adsManager.addEventListener(
events[index],
this.onAdEvent,
false,
this);
}
adsManager.init(
this.player.width,
this.player.height,
google.ima.ViewMode.NORMAL);
adsManager.start();
};
AdsController.prototype.pause = function() {
if (this.adsManager) {
this.adsManager.pause();
}
};
AdsController.prototype.resume = function() {
if (this.adsManager) {
this.adsManager.resume();
}
};
AdsController.prototype.onContentPauseRequested = function(adErrorEvent) {
this.controller.pauseForAd();
};
AdsController.prototype.onContentResumeRequested = function(adErrorEvent) {
this.controller.resumeAfterAd();
};
AdsController.prototype.onAdEvent = function(adEvent) {
this.controller.log('Ad event: ' + adEvent.type);
if (adEvent.type == google.ima.AdEvent.Type.CLICK) {
this.controller.adClicked();
}
};
AdsController.prototype.onAdError = function(adErrorEvent) {
this.controller.log('Ad error: ' + adErrorEvent.getError().toString());
if (this.adsManager) {
this.adsManager.destroy();
}
this.controller.resumeAfterAd();
};