-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailymotion-pa-connector.js
238 lines (238 loc) · 8.53 KB
/
dailymotion-pa-connector.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
class paDailymotionPlayer {
// settings it true as default for first video
isVideoEnded = true;
constructor() {
this.paCustomParams = {}
}
set params(params) {
this.paCustomParams = params
}
set media(media) {
this.videoMedia = media
}
avataginit = (state) => {
this.paCustomParams.av_content_id = state.videoId;
this.paCustomParams.av_content = state.videoTitle;
this.paCustomParams.av_content_duration = state.videoDuration * 1000;
this.videoMedia.setProps(this.paCustomParams);
// to check pause state
this.playbackPaused = true;
// to control first play event
this.firstPlayEvent = true;
// to control first playbackStart event
this.firstPlaybackStartEvent = true;
// to save old Cursor Position at seek start
this.oldCursorPosition = 0;
// to check video end event when video change
this.isVideoEnded = false;
// to check previous fullscreen on
this.fullscreenOn = false;
// to check volume change
this.playerVolume = 0;
// to check playback speed change
this.playerPlaybackSpeed = 0;
// to check video quality change
this.videoQuality = "";
// to set the value of ad end
this.adEndTime = 0;
// to track video time
this.videoTime = 0;
}
onPlayerReady = (player) => {
this.instanciatedPlayer = player;
/**
* Events related to player
*/
this.instanciatedPlayer.on(dailymotion.events.PLAYER_START,
(state) => {
this.avataginit(state);
/*Testing :*/
window.eventStart();
}
);
this.instanciatedPlayer.on(dailymotion.events.PLAYER_VIDEOCHANGE,
(state) => {
// if video_end in case of user interaction
if (!this.isVideoEnded) {
this.videoMedia.playbackStopped(this.videoTime * 1000);
}
this.avataginit(state);
}
);
/**
* Events related to content video
*/
this.instanciatedPlayer.on(dailymotion.events.VIDEO_PLAY,
(state) => {
// if player is starting
if (this.firstPlayEvent) {
this.firstPlayEvent = false;
this.videoMedia.play(state.videoTime * 1000);
}
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_BUFFERING,
(state) => {
// if ad end time and this event has same videoTime then ignore
if (this.adEndTime && this.adEndTime === parseInt(state.videoTime)) return;
/**
* always send playbackPaused event before buffering start
* before "playbackStart" no need to send playbackPaused event
* if player is already paused state, no need to send pause event
*/
if (!this.firstPlaybackStartEvent && !this.playbackPaused) {
this.playbackPaused = true;
this.videoMedia.playbackPaused(state.videoTime * 1000);
}
this.oldCursorPosition = state.videoTime * 1000;
this.videoMedia.bufferStart(state.videoTime * 1000);
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_PLAYING,
(state) => {
// if already playing ignore it
if (!this.playbackPaused) return;
this.playbackPaused = false;
if (this.firstPlaybackStartEvent) {
// set player volume at start time
this.playerVolume = state.playerIsMuted ? 0 : state.playerVolume.toFixed(1);
// set playback speed at start time
this.playerPlaybackSpeed = state.playerPlaybackSpeed;
// set video quality at start time
this.videoQuality = state.videoQuality;
this.videoMedia.playbackStart(state.videoTime * 1000);
this.firstPlaybackStartEvent = false;
} else {
this.videoMedia.playbackResumed(state.videoTime * 1000);
}
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_PAUSE,
(state) => {
this.playbackPaused = true;
this.videoMedia.playbackPaused(state.videoTime * 1000);
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_END,
(state) => {
this.isVideoEnded = true;
this.videoMedia.playbackStopped(state.videoTime * 1000);
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_SEEKSTART,
(state) => {
// if ad end time and this event has same videoTime then ignore
if (this.adEndTime && this.adEndTime === parseInt(state.videoTime)) return;
// if player already paused dont send pause event
if (!this.playbackPaused) {
this.playbackPaused = true;
this.videoMedia.playbackPaused(state.videoTime * 1000);
}
this.oldCursorPosition = state.videoTime * 1000;
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_SEEKEND,
(state) => {
// if ad end time and this event has same videoTime then ignore
if (this.adEndTime && this.adEndTime === parseInt(state.videoTime)) return;
// do not send this evens before playback start event
if (!this.firstPlaybackStartEvent) {
this.videoMedia.seek(this.oldCursorPosition, state.videoTime * 1000);
}
}
);
// To track video time
this.instanciatedPlayer.on(dailymotion.events.VIDEO_TIMECHANGE,
(state) => {
this.videoTime = state.videoTime;
}
);
/**
* Contextual events : The following list of events is not taken
* into account in the calculation of media consumption times.
* They are one-time contextual events
*/
this.instanciatedPlayer.on(dailymotion.events.PLAYER_PLAYBACKSPEEDCHANGE,
(state) => {
// do not send this evens before playback start event
// do not send this event if playerPlaybackSpeed did not change
if (!this.firstPlaybackStartEvent && (this.playerPlaybackSpeed !== state.playerPlaybackSpeed)) {
this.playerPlaybackSpeed = state.playerPlaybackSpeed;
this.videoMedia.setPlaybackSpeed(state.playerPlaybackSpeed);
}
}
);
this.instanciatedPlayer.on(dailymotion.events.AD_CLICK,
(state) => {
this.videoMedia.adClick();
}
);
this.instanciatedPlayer.on(dailymotion.events.AD_END,
(state) => {
this.adEndTime = parseInt(state.videoTime);
if (state.adEndedReason === "skipped") {
this.videoMedia.adSkip();
}
}
);
this.instanciatedPlayer.on(dailymotion.events.PLAYER_ERROR,
(state) => {
this.videoMedia.error(state.playerError && state.playerError.title);
}
);
this.instanciatedPlayer.on(dailymotion.events.PLAYER_PRESENTATIONMODECHANGE,
(state) => {
if (state.playerPresentationMode === "fullscreen") {
this.fullscreenOn = true;
this.videoMedia.fullscreenOn();
} else if (this.fullscreenOn) {
this.fullscreenOn = false;
this.videoMedia.fullscreenOff();
}
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_QUALITYCHANGE,
(state) => {
// do not send this evens before playback start event
// do not send this event if videoQuality did not change
if (!this.firstPlaybackStartEvent && (this.videoQuality !== state.videoQuality)) {
this.videoQuality = state.videoQuality;
console.log(state.videoQuality);
this.videoMedia.quality();
}
}
);
this.instanciatedPlayer.on(dailymotion.events.VIDEO_SUBTITLESCHANGE,
(state) => {
if (state.videoSubtitles) {
this.videoMedia.subtitleOff();
} else {
this.videoMedia.subtitleOn();
}
}
);
this.instanciatedPlayer.on(dailymotion.events.PLAYER_VOLUMECHANGE,
(state) => {
let volume = state.playerIsMuted ? 0 : state.playerVolume.toFixed(1);
// do not send this evens before playback start event
// do not send this event if volume did not change
if (!this.firstPlaybackStartEvent && (this.playerVolume !== volume)) {
this.playerVolume = volume;
this.videoMedia.volume();
}
}
);
/**
* adding puase event when ad start
*/
this.instanciatedPlayer.on(dailymotion.events.AD_START,
(state) => {
if (!this.playbackPaused) {
this.playbackPaused = true;
this.videoMedia.playbackPaused(state.videoTime * 1000);
};
}
);
}
}
const paDailymotionConnector = new paDailymotionPlayer();