-
Notifications
You must be signed in to change notification settings - Fork 5
/
youTubePlayer.js
367 lines (296 loc) · 8.34 KB
/
youTubePlayer.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* Author: Abraham Itule */
(function() {
/*
* Exposes the functionality of the video player to be used at one's discretion
*
* @param {String} name - The ID of the HTML element to be replaced with the iframe & video
* @return {Void}
*/
function VideoPlayer(name) {
// instance variable declarations
this._name = name
this._index = 0
this._videoList = null
this.player = null
// Load the YouTube Iframe API code from youtube.com
var tag = document.createElement('script')
tag.src = '//www.youtube.com/iframe_api'
var firstSTag = document.getElementsByTagName('script')[0]
firstSTag.parentNode.insertBefore(tag, firstSTag)
}
/*
* Initiliazes the YouTube API
* Note: This should be invoked inside the onYouTubePlayerAPIReady() function
*
* @param {Object} config - configuration settings for the video player
* @return {Void}
*/
VideoPlayer.prototype.init = function(config) {
console.log('Video-player State: "running initilization"')
this.player = new YT.Player(this._name, {
height: config.height || '390',
width: config.width || '680',
videoId: config.videoId,
events: {
'onReady': this._callbacks._playerReady,
'onStateChange': this._callbacks._playerStateChange,
'onError': this._callbacks._playerError,
'onPlaybackQualityChange': this._callbacks._playerPlaybackQualityChange,
'onPlaybackRateChange': this._callbacks._playerPlaybackRateChange
}
})
return this
}
VideoPlayer.prototype._callbacks = {
/*
* Default event listener when the player is ready!
* It's idealy a fallback when a custom listener is NOT added
*
* @param {Object} event - Native YouTube API event object
* @return {Void}
*/
'_playerReady' : function(event) {
event.target.setVolume(100)
event.target.playVideo()
window.APP.pubSub.on('readyOverride', event)
},
/*
* Default event listener when the player state changes!
* It's idealy a fallback when a custom listener is NOT added
*
* @param {Object} event - Native YouTube API event object
* @return {Void}
*/
'_playerStateChange' : function(event) {
switch(event.data){
case YT.PlayerState.PLAYING : {
console.log('Video-player State: "video playing"')
}
break;
case YT.PlayerState.ENDED: {
console.log('Video-player State: "video ended"')
// raise the custom ended event
window.APP.pubSub.on('ended')
}
break;
case YT.PlayerState.PAUSED: {
console.log('Video-player State: "video paused"')
}
break;
case YT.PlayerState.BUFFERING: {
console.log('Video-player State: "video buffering"')
}
break;
case YT.PlayerState.CUED: {
console.log('Video-player State: "video cued"')
}
break;
}
window.APP.pubSub.on('stateChangeOverride', event)
},
/*
* Default event listener when an ERROR in the player occurs!
* It's idealy a fallback when a custom listener is NOT added
*
* @param {Object} event - Native YouTube API event object
* @return {Void}
*/
'_playerError' : function(event) {
switch(event.data) {
case 2:
case '2': {
console.log('Error: invalid parameter value.')
}
break;
case '5':
case '5':{
console.log('Error: requested content cannot be played on HTML5 Player.')
}
break;
case '100':
case '100':{
console.log('Error: Video request was not found.')
}
break;
case '101':
case '101':
case '150':
case '150':{
console.log('Error: Owner of video does not allow to be played in embedde players')
}
break;
}
window.APP.pubSub.on('errorOverride', event)
},
'_playerPlaybackQualityChange' : function() {
window.APP.pubSub.on('playbackQualityChangeOverride', event)
},
'_playerPlaybackRateChange' : function() {
window.APP.pubSub.on('playbackRateChangeOverride', event)
}
}
/*
* Adds a function to the VideoPlayer prototype
*
* @param {String} name - Name of the function
* @param {Function} func - The function code
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.addMethod = function(name, func, place) {
console.log('State: Adding a new Method')
if (place) {
place[name] = func
return
}
VideoPlayer.prototype[name] = func
return this
}
/*
* Attaches a Listener to the YouTube Event names
*
* @param {String} name - Name of the event
* @param {Function} func - The function code/callback
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.addListener = function(eventName, func) {
switch(eventName) {
case 'onReady': {
this.addMethod('_playerReady', func, this._callbacks );
console.log('"onReady" Default action overriden')
}
break;
case 'onStateChange': {
this.addMethod('_playerStateChange', func, this._callbacks );
console.log('"onStateChange" Default action overriden')
}
break;
case 'onError': {
this.addMethod('_playerError', func, this._callbacks );
console.log('"onError" Default action overriden')
}
break;
case 'onPlaybackQualityChange': {
this.addMethod('_playerPlaybackQualityChange', func, this._callbacks ); console.log('"onPlaybackQualityChange" Default action overriden')
}
break;
case 'onPlaybackRateChange': {
this.addMethod('_playerPlaybackRateChange', func, this._callbacks );
console.log('"onPlaybackRateChange" Default action overriden')
}
break;
}
return this
}
/*
* Sets a playlist
*
* @param {Array} videoList - Array of strings with video ID's inside
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.setVideoList = function(videoList) {
this._videoList = videoList
return this
}
/*
* Plays the video playlist
*
* @return {Object} this - constructor's instance
*/
VideoPlayer.prototype.playVideoList = function() {
this.player.loadVideoById(this._videoList[this._index])
return this
}
/*
* Plays a video in the playlist with a specific ID
*
* @param {Number} index - Position of the video in the array
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.playVideoByIndex = function(index) {
this._index = index
this.playVideoList()
return this
}
/*
* Plays the video with the specified ID
*
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.playVideoById = function(id) {
this.player.loadVideoById(id)
return this
}
/*
* Plays current video
*
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.playVideo = function() {
this.player.playVideo()
return this
}
/*
* Pauses current video
*
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.pauseVideo = function() {
this.player.pauseVideo()
return this
}
/*
* Seeks to skips to as specified second on the video and automatically plays
*
* @param {Number} - Time in seconds to skip to
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.seekToAndPlay = function(secs) {
this.player.seekTo(secs)
this.player.playVideo()
return this
}
/*
* Shows the current time of which the video is playing
*
* @return {Number} - current playing time
*/
VideoPlayer.prototype.getCurrentTime = function() {
return this.player.getCurrentTime()
}
/*
* Plays the next video in the playlist
*
* @return {Boolean} - TRUE if possible, FALSE if there is NO next video
*/
VideoPlayer.prototype.nextVideo = function() {
if(this._index + 1 >= this.videoList.length){
//$(this).trigger('endOfList')
return false
}
this._index++;
this.playVideoList();
//$(this).trigger('next')
return true
}
/*
* Stops playing the current video
*
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.stopVideo = function() {
this.player.stopVideo()
return this
}
/*
* Sets the volume of the current video
*
* @return {Object} this - 'this' points to the constructor's instance
*/
VideoPlayer.prototype.setVolume = function(vol) {
this.player.setVolume(vol)
return this
}
// Expose it to the Global scope (window) as a 'APP' property
window.APP = window.APP || {}
window.APP.VideoPlayer = VideoPlayer
})()