-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
400 lines (359 loc) · 14.3 KB
/
player.js
File metadata and controls
400 lines (359 loc) · 14.3 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
Object.prototype.toTime = function ()
{
var self = +this;
var m = ''+Math.floor(self/60);
while (m.length < 2) m = '0' + m;
var h = ''+Math.floor(self/60/60);
while (h.length < 1) h = '0' + h;
var s = '' + ~~(self - h*3600 - m*60);
while (s.length < 2) s = '0' + s;
return h + ':' + m + ':' + s;
};
Object.prototype.extend = function (obj)
{
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
this[i] = obj[i];
}
}
};
var Player = function (id, movie, options)
{
if (this.players[id]) return this.players[id];
this.movie = movie;
options = options || {};
this.options.extend(options);
this.container = null;
this.video = null;
this.controlPanel = null;
this.buttonsPanel = null;
this.controls = {
play: null,
pause: null,
replay: null,
timer: null,
slider: null,
volume: null,
fs: null
};
this.buttons = {
play: null,
pause: null,
replay: null
};
this.sliderHandler = null;
this.volumeHandler = null;
this.clickHandler = null;
this.init();
var container = document.getElementById(id);
if (container) {
container.appendChild(this.container);
} else {
throw 'No element found';
}
this.players[id] = this;
return this;
};
Player.prototype = {
players: {},
options: {
theme: 'default',
width: 640,
height: 480,
durationTimer: true // show timer from 00:00 (false - show rest time)
},
init: function() {
this.initCss();
this.initContainer();
this.initVideo();
this.initControls();
},
initCss: function()
{
var cssId = 'css__player__' + this.options.theme;
if (!document.getElementById(cssId)) {
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'themes/'+this.options.theme+'/'+this.options.theme+'.css';
link.media = 'all';
head.appendChild(link);
}
cssId = 'css__player__awesome';
if (!document.getElementById(cssId)) {
head = document.getElementsByTagName('head')[0];
link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/font-awesome.min.css';
link.media = 'all';
head.appendChild(link);
}
},
initContainer: function()
{
this.container = document.createElement('div');
this.container.className = 'player__'+this.options.theme+'__video';
},
initVideo: function()
{
this.video = document.createElement('video');
this.video.width = this.options.width;
this.video.height = this.options.height;
this.video.controls = false;
this.video.autoplay = false;
this.container.appendChild(this.video);
var movie = document.createElement('source');
movie.src = this.movie;
this.video.appendChild(movie);
this.video.addEventListener('play', this.showStatus.bind(this, 'play'), false);
this.video.addEventListener('pause', this.showStatus.bind(this, 'pause'), false);
this.video.addEventListener('ended', this.showStatus.bind(this, 'end'), false);
this.video.addEventListener('timeupdate', this.setTimer.bind(this), false);
this.video.addEventListener('loadeddata', this.showStatus.bind(this, 'start'), false);
this.video.load();
var warn = document.createElement('p');
warn.innerHTML = 'To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video';
this.video.appendChild(warn);
},
initControls: function()
{
this.controlPanel = document.createElement('div');
this.controlPanel.className = 'player__'+this.options.theme+'__player';
this.container.appendChild(this.controlPanel);
// play
this.controls.play = document.createElement('div');
this.controls.play.className = 'player__'+this.options.theme+'__player-play';
this.controls.play.addEventListener('click', this.play.bind(this), false);
this.controlPanel.appendChild(this.controls.play);
var icon_play = document.createElement('i');
icon_play.className = 'fa fa-play';
this.controls.play.appendChild(icon_play);
// pause
this.controls.pause = document.createElement('div');
this.controls.pause.className = 'player__'+this.options.theme+'__player-pause';
this.controls.pause.style.display = 'none';
this.controls.pause.addEventListener('click', this.pause.bind(this), false);
this.controlPanel.appendChild(this.controls.pause);
var icon_pause = document.createElement('i');
icon_pause.className = 'fa fa-pause';
this.controls.pause.appendChild(icon_pause);
this.clickHandler = this.pause.bind(this);
// replay
this.controls.replay = document.createElement('div');
this.controls.replay.className = 'player__'+this.options.theme+'__player-replay';
this.controls.replay.style.display = 'none';
this.controls.replay.addEventListener('click', this.play.bind(this), false);
this.controlPanel.appendChild(this.controls.replay);
var icon_replay = document.createElement('i');
icon_replay.className = 'fa fa-refresh';
this.controls.replay.appendChild(icon_replay);
// timer
this.controls.timer = document.createElement('div');
this.controls.timer.className = 'player__'+this.options.theme+'__player-timer';
this.controlPanel.appendChild(this.controls.timer);
this.controls.timer.addEventListener('click', this.changeTimerStatus.bind(this), false);
// slider
var slider_container = document.createElement('div');
slider_container.className = 'player__'+this.options.theme+'__player-slider';
this.controlPanel.appendChild(slider_container);
this.controls.slider = document.createElement('div');
this.controls.slider.className = 'player__'+this.options.theme+'__player-progressbar';
slider_container.appendChild(this.controls.slider);
var progress = document.createElement('div');
progress.className = 'player__'+this.options.theme+'__player-progress';
this.controls.slider.appendChild(progress);
this.controls.slider.addEventListener('mouseup', this.slideEnd.bind(this), false);
this.controls.slider.addEventListener('mousedown', this.slideStart.bind(this), false);
this.controls.slider.addEventListener('mousemove', this.slideMove.bind(this), false);
// volume
this.controls.volume = document.createElement('div');
this.controls.volume.className = 'player__'+this.options.theme+'__player-volume';
this.controlPanel.appendChild(this.controls.volume);
var icon_volume = document.createElement('i');
icon_volume.className = 'fa fa-volume-up fa-fw';
this.controls.volume.appendChild(icon_volume);
icon_volume.addEventListener('click', this.mute.bind(this), false);
var volume = document.createElement('div');
volume.className = 'player__'+this.options.theme+'__player-progressbar';
this.controls.volume.appendChild(volume);
var volumeLevel = document.createElement('div');
volumeLevel.className = 'player__'+this.options.theme+'__player-progress';
volume.appendChild(volumeLevel);
volume.addEventListener('mouseup', this.volumeEnd.bind(this), false);
volume.addEventListener('mousedown', this.volumeStart.bind(this), false);
volume.addEventListener('mousemove', this.volumeMove.bind(this), false);
// social
var social_container = document.createElement('div');
social_container.className = 'player__'+this.options.theme+'__player-social';
this.controlPanel.appendChild(social_container);
var twitter = document.createElement('i');
twitter.className = 'fa fa-twitter';
social_container.appendChild(twitter);
twitter.addEventListener('click', function(){alert ('Nothing to do');});
// full screen
this.controls.fs = document.createElement('div');
this.controls.fs.className = 'player__'+this.options.theme+'__player-fs';
this.controls.fs.addEventListener('click', this.fs.bind(this), false);
this.controlPanel.appendChild(this.controls.fs);
var icon_fs = document.createElement('i');
icon_fs.className = 'fa fa-expand';
this.controls.fs.appendChild(icon_fs);
// big icons
this.buttonsPanel = document.createElement('div');
this.buttonsPanel.className = 'player__'+this.options.theme+'__video-buttons';
this.container.appendChild(this.buttonsPanel);
this.buttons.play = document.createElement('div');
this.buttons.play.className = 'player__'+this.options.theme+'__video-buttons-play';
this.buttons.play.style.display = 'none';
this.buttonsPanel.appendChild(this.buttons.play);
var play_icon = document.createElement('i');
play_icon.className = 'fa fa-play fa-4x';
this.buttons.play.appendChild(play_icon);
this.buttons.play.addEventListener('click', this.play.bind(this), false);
this.buttons.pause = document.createElement('div');
this.buttons.pause.className = 'player__'+this.options.theme+'__video-buttons-pause';
this.buttons.pause.style.display = 'none';
this.buttonsPanel.appendChild(this.buttons.pause);
var pause_icon = document.createElement('i');
pause_icon.className = 'fa fa-pause fa-4x';
this.buttons.pause.appendChild(pause_icon);
this.buttons.pause.addEventListener('click', this.pause.bind(this), false);
this.buttons.replay = document.createElement('div');
this.buttons.replay.className = 'player__'+this.options.theme+'__video-buttons-replay';
this.buttons.replay.style.display = 'none';
this.buttonsPanel.appendChild(this.buttons.replay);
var replay_icon = document.createElement('i');
replay_icon.className = 'fa fa-refresh fa-4x';
this.buttons.replay.appendChild(replay_icon);
this.buttons.replay.addEventListener('click', this.play.bind(this), false);
},
initMovie: function()
{
this.setTimer();
this.setVolume();
},
showStatus: function(status)
{
this.buttons.play.style.display = 'none';
this.buttons.pause.style.display = 'none';
this.buttons.replay.style.display = 'none';
this.controls.play.style.display = 'none';
this.controls.pause.style.display = 'none';
this.controls.replay.style.display = 'none';
this.buttonsPanel.removeEventListener('click', this.clickHandler, false);
if (status == 'pause') {
this.buttons.play.style.display = 'block';
this.controls.play.style.display = 'block';
} else
if (status == 'play' && !this.video.paused) {
this.controls.pause.style.display = 'block';
this.buttonsPanel.addEventListener('click', this.clickHandler, false);
} else
if (status == 'start') {
this.initMovie();
this.controls.play.style.display = 'block';
this.buttons.play.style.display = 'block';
} else
if (status == 'end') {
this.controls.replay.style.display = 'block';
this.buttons.replay.style.display = 'block';
}
},
play: function()
{
this.video.play();
},
pause: function()
{
this.video.pause();
},
changeTimerStatus: function()
{
this.options.durationTimer = !this.options.durationTimer;
this.setTimer();
},
setTimer: function()
{
if (this.options.durationTimer) {
this.controls.timer.innerHTML = this.video.currentTime.toTime();
} else {
this.controls.timer.innerHTML = (this.video.duration-this.video.currentTime).toTime();
}
var dx = 100*this.video.currentTime/this.video.duration;
this.controls.slider.querySelector('div').style.width = ''+dx+'%';
},
slide: function(e)
{
var dx = 100 * e.offsetX / this.controls.slider.offsetWidth;
this.video.currentTime = this.video.duration * dx / 100;
},
slideStart: function(e)
{
this.sliderHandler = true;
this.slide(e);
},
slideEnd: function(e)
{
this.sliderHandler = null;
this.slide(e);
},
slideMove: function(e)
{
if (!this.sliderHandler) return;
this.slide(e);
},
mute: function()
{
this.video.muted = !this.video.muted;
var icon = this.controls.volume.querySelector('i');
if (this.video.muted) {
icon.className = 'fa fa-volume-off fa-fw';
} else {
icon.className = 'fa fa-volume-up fa-fw';
}
},
setVolume: function()
{
var volume = this.controls.volume.querySelector('div');
var dx = 100*this.video.volume;
volume.querySelector('div').style.width = ''+dx+'%';
},
volume: function(e)
{
var volume = this.controls.volume.querySelector('div');
var dx = 100 * e.offsetX /volume.offsetWidth;
this.video.volume = dx / 100;
this.setVolume();
},
volumeStart: function(e)
{
this.volumeHandler = true;
this.volume(e);
},
volumeEnd: function(e)
{
this.volumeHandler = null;
this.volume(e);
},
volumeMove: function(e)
{
if (!this.volumeHandler) return;
this.volume(e);
},
fs: function()
{
if (this.video.requestFullscreen) {
this.video.requestFullscreen();
} else if (this.video.msRequestFullscreen) {
this.video.msRequestFullscreen();
} else if (this.video.mozRequestFullScreen) {
this.video.mozRequestFullScreen();
} else if (this.video.webkitRequestFullscreen) {
this.video.webkitRequestFullscreen();
}
}
};