forked from lopezs/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.js
More file actions
1141 lines (1037 loc) · 37 KB
/
control.js
File metadata and controls
1141 lines (1037 loc) · 37 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function ($) {
AblePlayer.prototype.seekTo = function (newTime) {
if (this.player === 'html5') {
var seekable;
this.startTime = newTime;
// Check HTML5 media "seekable" property to be sure media is seekable to startTime
seekable = this.media.seekable;
if (seekable.length > 0 && this.startTime >= seekable.start(0) && this.startTime <= seekable.end(0)) {
this.media.currentTime = this.startTime;
if (this.hasSignLanguage && this.signVideo) {
// keep sign languge video in sync
this.signVideo.currentTime = this.startTime;
}
}
}
else if (this.player === 'jw' && this.jwPlayer) {
// pause JW Player temporarily.
// When seek has successfully reached newTime,
// onSeek event will be called, and playback will be resumed
this.jwSeekPause = true;
this.jwPlayer.seek(newTime);
}
else if (this.player === 'youtube') {
this.youTubePlayer.seekTo(newTime,true);
}
this.liveUpdatePending = true;
this.refreshControls();
};
AblePlayer.prototype.getDuration = function () {
var duration;
if (this.player === 'html5') {
duration = this.media.duration;
}
else if (this.player === 'jw' && this.jwPlayer) {
duration = this.jwPlayer.getDuration();
}
else if (this.player === 'youtube') {
duration = this.youTubePlayer.getDuration();
}
if (duration === undefined || isNaN(duration) || duration === -1) {
return 0;
}
return duration;
};
AblePlayer.prototype.getElapsed = function () {
var position;
if (this.player === 'html5') {
position = this.media.currentTime;
}
else if (this.player === 'jw' && this.jwPlayer) {
if (this.jwPlayer.getState() === 'IDLE') {
return 0;
}
position = this.jwPlayer.getPosition();
}
else if (this.player === 'youtube') {
if (this.youTubePlayer) {
position = this.youTubePlayer.getCurrentTime();
}
}
if (position === undefined || isNaN(position) || position === -1) {
return 0;
}
return position;
};
// Returns one of the following states:
// 'stopped' - Not yet played for the first time, or otherwise reset to unplayed.
// 'ended' - Finished playing.
// 'paused' - Not playing, but not stopped or ended.
// 'buffering' - Momentarily paused to load, but will resume once data is loaded.
// 'playing' - Currently playing.
AblePlayer.prototype.getPlayerState = function () {
if (this.player === 'html5') {
if (this.media.paused) {
if (this.getElapsed() === 0) {
return 'stopped';
}
else if (this.media.ended) {
return 'ended';
}
else {
return 'paused';
}
}
else if (this.media.readyState !== 4) {
return 'buffering';
}
else {
return 'playing';
}
}
else if (this.player === 'jw' && this.jwPlayer) {
if (this.jwPlayer.getState() === 'PAUSED' || this.jwPlayer.getState() === 'IDLE' || this.jwPlayer.getState() === undefined) {
if (this.getElapsed() === 0) {
return 'stopped';
}
else if (this.getElapsed() === this.getDuration()) {
return 'ended';
}
else {
return 'paused';
}
}
else if (this.jwPlayer.getState() === 'BUFFERING') {
return 'buffering';
}
else if (this.jwPlayer.getState() === 'PLAYING') {
return 'playing';
}
}
else if (this.player === 'youtube') {
var state = this.youTubePlayer.getPlayerState();
if (state === -1 || state === 5) {
return 'stopped';
}
else if (state === 0) {
return 'ended';
}
else if (state === 1) {
return 'playing';
}
else if (state === 2) {
return 'paused';
}
else if (state === 3) {
return 'buffering';
}
}
};
AblePlayer.prototype.isMuted = function () {
if (!this.browserSupportsVolume()) {
return false;
}
if (this.player === 'html5') {
return this.media.muted;
}
else if (this.player === 'jw' && this.jwPlayer) {
return this.jwPlayer.getMute();
}
else if (this.player === 'youtube') {
return this.youTubePlayer.isMuted();
}
};
AblePlayer.prototype.setMute = function(mute) {
if (!this.browserSupportsVolume()) {
return;
}
if (!mute) {
this.$muteButton.attr('aria-label',this.tt.mute);
this.$muteButton.find('span').first().removeClass('icon-volume-mute').addClass('icon-volume-loud');
this.$muteButton.find('span.able-clipped').text(this.tt.mute);
}
else {
this.$muteButton.attr('aria-label',this.tt.unmute);
this.$muteButton.find('span').first().removeClass('icon-volume-loud').addClass('icon-volume-mute');
this.$muteButton.find('span.able-clipped').text(this.tt.unmute);
}
if (this.player === 'html5') {
this.media.muted = mute;
}
else if (this.player === 'jw' && this.jwPlayer) {
this.jwPlayer.setMute(mute);
}
else if (this.player === 'youtube') {
if (mute) {
this.youTubePlayer.mute();
}
else {
this.youTubePlayer.unMute();
}
}
if (!mute) {
// TODO: Is this necessary?
// Restore volume to last value.
if (this.lastVolume) {
this.setVolume(this.lastVolume);
}
}
};
AblePlayer.prototype.setVolume = function (volume) {
if (!this.browserSupportsVolume()) {
return;
}
if (this.player === 'html5') {
this.media.volume = volume;
if (this.hasSignLanguage && this.signVideo) {
this.signVideo.volume = 0; // always mute
}
}
else if (this.player === 'jw' && this.jwPlayer) {
this.jwPlayer.setVolume(volume * 100);
}
else if (this.player === 'youtube') {
this.youTubePlayer.setVolume(volume * 100);
}
this.lastVolume = volume;
};
AblePlayer.prototype.getVolume = function (volume) {
if (!this.browserSupportsVolume()) {
return 1;
}
if (this.player === 'html5') {
return this.media.volume;
}
else if (this.player === 'jw' && this.jwPlayer) {
return this.jwPlayer.getVolume() / 100;
}
else if (this.player === 'youtube') {
return this.youTubePlayer.getVolume() / 100;
}
};
AblePlayer.prototype.isPlaybackRateSupported = function () {
if (this.player === 'html5') {
return this.media.playbackRate ? true : false;
}
else if (this.player === 'jw' && this.jwPlayer) {
// Not directly supported by JW player; can hack for HTML5 version by finding the dynamically generated video tag, but decided not to do that.
return false;
}
else if (this.player === 'youtube') {
// Youtube always supports a finite list of playback rates. Only expose controls if more than one is available.
return (this.youTubePlayer.getAvailablePlaybackRates().length > 1);
}
};
AblePlayer.prototype.setPlaybackRate = function (rate) {
rate = Math.max(0.5, rate);
if (this.player === 'html5') {
this.media.playbackRate = rate;
}
else if (this.player === 'youtube') {
this.youTubePlayer.setPlaybackRate(rate);
}
if (this.hasSignLanguage && this.signVideo) {
this.signVideo.playbackRate = rate;
}
this.$speed.text(this.tt.speed + ': ' + rate.toFixed(2).toString() + 'x');
};
AblePlayer.prototype.getPlaybackRate = function () {
if (this.player === 'html5') {
return this.media.playbackRate;
}
else if (this.player === 'jw' && this.jwPlayer) {
// Unsupported, always the normal rate.
return 1;
}
else if (this.player === 'youtube') {
return this.youTubePlayer.getPlaybackRate();
}
};
// Note there are three player states that count as paused in this sense,
// and one of them is named 'paused'.
// A better name would be 'isCurrentlyNotPlayingOrBuffering'
AblePlayer.prototype.isPaused = function () {
var state = this.getPlayerState();
return state === 'paused' || state === 'stopped' || state === 'ended';
};
AblePlayer.prototype.pauseMedia = function () {
if (this.player === 'html5') {
this.media.pause(true);
if (this.hasSignLanguage && this.signVideo) {
this.signVideo.pause(true);
}
}
else if (this.player === 'jw' && this.jwPlayer) {
this.jwPlayer.pause(true);
}
else if (this.player === 'youtube') {
this.youTubePlayer.pauseVideo();
}
};
AblePlayer.prototype.playMedia = function () {
if (this.player === 'html5') {
this.media.play(true);
if (this.hasSignLanguage && this.signVideo) {
this.signVideo.play(true);
}
}
else if (this.player === 'jw' && this.jwPlayer) {
this.jwPlayer.play(true);
}
else if (this.player === 'youtube') {
this.youTubePlayer.playVideo();
this.stoppingYouTube = false;
}
this.startedPlaying = true;
};
// Right now, update the seekBar values based on current duration and time.
// Later, move all non-destructive control updates based on state into this function?
AblePlayer.prototype.refreshControls = function() {
var thisObj = this;
var duration = this.getDuration();
var elapsed = this.getElapsed();
if (this.useFixedSeekInterval === false && this.seekIntervalCalculated === false && duration > 0) {
// couldn't calculate seekInterval previously; try again.
if (duration > 0) {
this.seekInterval = Math.max(this.seekInterval, duration / 10);
this.seekIntervalCalculated = true;
}
}
if (this.seekBar) {
this.seekBar.setDuration(duration);
if (!this.seekBar.tracking) {
// Only update the aria live region if we have an update pending (from a
// seek button control) or if the seekBar has focus.
// We use document.activeElement instead of $(':focus') due to a strange bug:
// When the seekHead element is focused, .is(':focus') is failing and $(':focus') is returning an undefined element.
var updateLive = this.liveUpdatePending || this.seekBar.seekHead.is($(document.activeElement));
this.liveUpdatePending = false;
this.seekBar.setPosition(elapsed, updateLive);
}
}
var displayElapsed;
// When seeking, display the seek bar time instead of the actual elapsed time.
if (this.seekBar.tracking) {
displayElapsed = this.seekBar.lastTrackPosition;
}
else {
displayElapsed = elapsed;
}
this.$durationContainer.text(' / ' + this.formatSecondsAsColonTime(duration));
this.$elapsedTimeContainer.text(this.formatSecondsAsColonTime(displayElapsed));
var textByState = {
'stopped': this.tt.statusStopped,
'paused': this.tt.statusPaused,
'playing': this.tt.statusPlaying,
'buffering': this.tt.statusBuffering,
'ended': this.tt.statusEnd
};
if (this.stoppingYouTube) {
// YouTube reports 'paused' but we're trying to emulate 'stopped'
// See notes in handleStop()
// this.stoppingYouTube will be reset when playback resumes in play()
if (this.$status.text() !== this.tt.statusStopped) {
this.$status.text(this.tt.statusStopped);
}
if (this.$playpauseButton.find('span').first().hasClass('icon-pause')) {
if (this.iconType === 'font') {
this.$playpauseButton.find('span').first().removeClass('icon-pause').addClass('icon-play');
this.$playpauseButton.find('span.able-clipped').text(this.tt.play);
}
else {
this.$playpauseButton.find('img').attr('src',this.playButtonImg);
}
}
}
else {
// Update the text only if it's changed since it has role="alert";
// also don't update while tracking, since this may Pause/Play the player but we don't want to send a Pause/Play update.
if (this.$status.text() !== textByState[this.getPlayerState()] && !this.seekBar.tracking) {
// Debounce updates; only update after status has stayed steadily different for 250ms.
var timestamp = (new Date()).getTime();
if (!this.statusDebounceStart) {
this.statusDebounceStart = timestamp;
// Make sure refreshControls gets called again at the appropriate time to check.
this.statusTimeout = setTimeout(function () {
thisObj.refreshControls();
}, 300);
}
else if ((timestamp - this.statusDebounceStart) > 250) {
this.$status.text(textByState[this.getPlayerState()]);
this.statusDebounceStart = null;
clearTimeout(this.statusTimeout);
this.statusTimeout = null;
}
}
else {
this.statusDebounceStart = null;
clearTimeout(this.statusTimeout);
this.statusTimeout = null;
}
// Don't change play/pause button display while using the seek bar (or if YouTube stopped)
if (!this.seekBar.tracking && !this.stoppingYouTube) {
if (this.isPaused()) {
this.$playpauseButton.attr('aria-label',this.tt.play);
if (this.iconType === 'font') {
this.$playpauseButton.find('span').first().removeClass('icon-pause').addClass('icon-play');
this.$playpauseButton.find('span.able-clipped').text(this.tt.play);
}
else {
this.$playpauseButton.find('img').attr('src',this.playButtonImg);
}
}
else {
this.$playpauseButton.attr('aria-label',this.tt.pause);
if (this.iconType === 'font') {
this.$playpauseButton.find('span').first().removeClass('icon-play').addClass('icon-pause');
this.$playpauseButton.find('span.able-clipped').text(this.tt.pause);
}
else {
this.$playpauseButton.find('img').attr('src',this.pauseButtonImg);
}
}
}
}
// Update seekbar width.
// To do this, we need to calculate the width of all elements surrounding it.
if (this.seekBar) {
var widthUsed = 0;
// Elements on the left side of the control panel.
var leftControls = this.seekBar.wrapperDiv.parent().prev();
leftControls.children().each(function () {
if ($(this).is(':hidden')) {
// jQuery width() returns 0 for hidden elements
// thisObj.getHiddenWidth() is a workaround
widthUsed += thisObj.getHiddenWidth($(this));
}
else {
widthUsed += $(this).width();
}
});
// Elements to the left and right of the seekbar on the right side.
var prev = this.seekBar.wrapperDiv.prev();
while (prev.length > 0) {
if (prev.is(':hidden')) {
widthUsed += thisObj.getHiddenWidth(prev);
}
else {
widthUsed += prev.width();
}
prev = prev.prev();
}
var next = this.seekBar.wrapperDiv.next();
while (next.length > 0) {
if (next.is(':hidden')) {
widthUsed += thisObj.getHiddenWidth(next);
}
else {
widthUsed += next.width();
}
next = next.next();
}
var seekbarWidth = this.playerWidth - widthUsed - 20;
// Sometimes some minor fluctuations based on browser weirdness, so set a threshold.
if (Math.abs(seekbarWidth - this.seekBar.getWidth()) > 5) {
this.seekBar.setWidth(seekbarWidth);
}
}
// Update buttons on/off display.
if (this.$descButton) {
if (this.descOn) {
this.$descButton.removeClass('buttonOff').attr('aria-label',this.tt.turnOffDescriptions);
this.$descButton.find('span.able-clipped').text(this.tt.turnOffDescriptions);
}
else {
this.$descButton.addClass('buttonOff').attr('aria-label',this.tt.turnOnDescriptions);
this.$descButton.find('span.able-clipped').text(this.tt.turnOnDescriptions);
}
}
if (this.$ccButton) {
if (this.usingYouTubeCaptions) {
var captionsCount = this.ytCaptions.length;
}
else {
var captionsCount = this.captions.length;
}
// Button has a different title depending on the number of captions.
// If only one caption track, this is "Show captions" and "Hide captions"
// Otherwise, it is just always "Captions"
if (!this.captionsOn) {
this.$ccButton.addClass('buttonOff');
if (captionsCount === 1) {
this.$ccButton.attr('aria-label',this.tt.showCaptions);
this.$ccButton.find('span.able-clipped').text(this.tt.showCaptions);
}
}
else {
this.$ccButton.removeClass('buttonOff');
if (captionsCount === 1) {
this.$ccButton.attr('aria-label',this.tt.hideCaptions);
this.$ccButton.find('span.able-clipped').text(this.tt.hideCaptions);
}
}
if (captionsCount > 1) {
this.$ccButton.attr({
'aria-label': this.tt.captions,
'aria-haspopup': 'true',
'aria-controls': this.mediaId + '-captions-menu'
});
this.$ccButton.find('span.able-clipped').text(this.tt.captions);
}
}
if (this.$chaptersButton) {
this.$chaptersButton.attr({
'aria-label': this.tt.chapters,
'aria-haspopup': 'true',
'aria-controls': this.mediaId + '-chapters-menu'
});
}
if (this.$muteButton) {
if (!this.isMuted()) {
if (this.iconType === 'font') {
this.$muteButton.find('span').first().removeClass('icon-volume-mute').addClass('icon-volume-loud');
this.$muteButton.find('span.able-clipped').text(this.tt.mute);
}
else {
this.$muteButton.find('img').attr('src',this.volumeLoudButtonImg);
}
}
else {
if (this.iconType === 'font') {
this.$muteButton.find('span').first().removeClass('icon-volume-loud').addClass('icon-volume-mute');
this.$muteButton.find('span.able-clipped').text(this.tt.unmute);
}
else {
this.$muteButton.find('img').attr('src',this.volumeMuteButtonImg);
}
}
}
if (this.$fullscreenButton) {
if (!this.isFullscreen()) {
this.$fullscreenButton.attr('aria-label', this.tt.enterFullScreen);
if (this.iconType === 'font') {
this.$fullscreenButton.find('span').first().removeClass('icon-fullscreen-collapse').addClass('icon-fullscreen-expand');
this.$fullscreenButton.find('span.able-clipped').text(this.tt.enterFullScreen);
}
else {
this.$fullscreenButton.find('img').attr('src',this.fullscreenExpandButtonImg);
}
}
else {
this.$fullscreenButton.attr('aria-label',this.tt.exitFullScreen);
if (this.iconType === 'font') {
this.$fullscreenButton.find('span').first().removeClass('icon-fullscreen-expand').addClass('icon-fullscreen-collapse');
this.$fullscreenButton.find('span.able-clipped').text(this.tt.exitFullScreen);
}
else {
this.$fullscreenButton.find('img').attr('src',this.fullscreenCollapseButtonImg);
}
}
}
// TODO: Move all button updates here.
if (typeof this.$bigPlayButton !== 'undefined') {
// Choose show/hide for big play button and adjust position.
if (this.isPaused() && !this.seekBar.tracking) {
this.$bigPlayButton.show();
if (this.isFullscreen()) {
this.$bigPlayButton.width($(window).width());
this.$bigPlayButton.height($(window).height());
}
else {
this.$bigPlayButton.width(this.$mediaContainer.width());
this.$bigPlayButton.height(this.$mediaContainer.height());
}
}
else {
this.$bigPlayButton.hide();
}
}
if (this.includeTranscript) {
// Sync checkbox with autoScrollTranscript variable.
if (this.autoScrollTranscript !== this.$autoScrollTranscriptCheckbox.prop('checked')) {
this.$autoScrollTranscriptCheckbox.prop('checked', this.autoScrollTranscript);
}
// If transcript locked, scroll transcript to current highlight location.
if (this.autoScrollTranscript && this.currentHighlight) {
var newTop = Math.floor($('.able-transcript').scrollTop() +
$(this.currentHighlight).position().top -
($('.able-transcript').height() / 2) +
($(this.currentHighlight).height() / 2));
if (newTop !== Math.floor($('.able-transcript').scrollTop())) {
// Set a flag to ignore the coming scroll event.
// there's no other way I know of to differentiate programmatic and user-initiated scroll events.
this.scrollingTranscript = true;
$('.able-transcript').scrollTop(newTop);
}
}
}
// Update buffering progress.
// TODO: Currently only using the first HTML5 buffered interval, but this fails sometimes when buffering is split into two or more intervals.
if (this.player === 'html5') {
if (this.media.buffered.length > 0) {
this.seekBar.setBuffered(this.media.buffered.end(0) / this.getDuration())
}
}
else if (this.player === 'jw' && this.jwPlayer) {
this.seekBar.setBuffered(this.jwPlayer.getBuffer() / 100);
}
else if (this.player === 'youtube') {
this.seekBar.setBuffered(this.youTubePlayer.getVideoLoadedFraction());
}
};
AblePlayer.prototype.getHiddenWidth = function($el) {
// jQuery returns for width() if element is hidden
// this function is a workaround
// save a reference to a cloned element that can be measured
var $hiddenElement = $el.clone().appendTo('body');
// calculate the width of the clone
var width = $hiddenElement.outerWidth();
// remove the clone from the DOM
$hiddenElement.remove();
return width;
};
AblePlayer.prototype.handlePlay = function(e) {
if (this.isPaused()) {
this.playMedia();
}
else {
this.pauseMedia();
}
this.refreshControls();
};
AblePlayer.prototype.handleStop = function() {
if (this.player == 'html5') {
this.pauseMedia();
this.seekTo(0);
}
else if (this.player === 'jw' && this.jwPlayer) {
this.jwPlayer.stop();
}
else if (this.player === 'youtube') {
// YouTube API function stopVideo() does not reset video to 0
// However, the stopped video is not seekable
// so we can't call seekTo(0) after calling stopVideo()
// Workaround is to use pauseVideo() instead, then seek to 0
this.youTubePlayer.pauseVideo();
this.seekTo(0);
// Unfortunately, pausing the video doesn't change playerState to 'Stopped'
// which has an effect on the player UI.
// the following Boolean is used in refreshControls() to emulate a 'stopped' state
this.stoppingYouTube = true;
}
this.refreshControls();
};
AblePlayer.prototype.handleRewind = function() {
var targetTime = this.getElapsed() - this.seekInterval;
if (targetTime < 0) {
this.seekTo(0);
}
else {
this.seekTo(targetTime);
}
};
AblePlayer.prototype.handleFastForward = function() {
var targetTime = this.getElapsed() + this.seekInterval;
if (targetTime > this.getDuration()) {
this.seekTo(this.getDuration());
}
else {
this.seekTo(targetTime);
}
};
AblePlayer.prototype.handleMute = function() {
if (this.isMuted()) {
this.setMute(false);
}
else {
this.setMute(true);
}
};
AblePlayer.prototype.handleVolume = function(direction) {
var volume;
if (this.isMuted()) {
this.setMute(false);
}
volume = this.getVolume();
if (direction === 'up') {
if (volume < 0.9) {
volume = Math.round((volume + 0.1) * 10) / 10;
}
else {
volume = 1;
}
}
else if (direction === 'down') {
if (volume > 0.1) {
volume = Math.round((volume - 0.1) * 10) / 10;
}
else {
volume = 0;
}
}
else if (direction >= 49 || direction <= 53) {
// TODO: What is this for?
volume = (direction-48) * 0.2;
}
this.setVolume(volume);
if (volume === 0) {
this.setMute(true);
}
};
AblePlayer.prototype.handleRateIncrease = function() {
this.changeRate(1);
};
AblePlayer.prototype.handleRateDecrease = function() {
this.changeRate(-1);
};
// Increases or decreases playback rate, where dir is 1 or -1 indication direction.
AblePlayer.prototype.changeRate = function (dir) {
if (this.player === 'html5') {
this.setPlaybackRate(this.getPlaybackRate() + (0.25 * dir));
}
else if (this.player === 'youtube') {
var rates = this.youTubePlayer.getAvailablePlaybackRates();
var currentRate = this.getPlaybackRate();
var index = rates.indexOf(currentRate);
if (index === -1) {
console.log('ERROR: Youtube returning unknown playback rate ' + currentRate.toString());
}
else {
index += dir;
// Can only increase or decrease rate if there's another rate available.
if (index < rates.length && index >= 0) {
this.setPlaybackRate(rates[index]);
}
}
}
};
AblePlayer.prototype.handleCaptionToggle = function() {
var captions;
if (this.hidingPopup) {
// stopgap to prevent spacebar in Firefox from reopening popup
// immediately after closing it
this.hidingPopup = false;
return false;
}
if (this.usingYouTubeCaptions) {
}
if (this.captions.length) {
captions = this.captions;
}
else if (this.ytCaptions.length) {
captions = this.ytCaptions;
}
else {
captions = [];
}
if (captions.length === 1) {
// When there's only one set of captions, just do an on/off toggle.
if (this.captionsOn === true) {
// turn them off
this.captionsOn = false;
if (this.usingYouTubeCaptions) {
this.youTubePlayer.unloadModule(this.ytCaptionModule);
}
else {
this.$captionDiv.hide();
}
}
else {
// captions are off. Turn them on.
this.captionsOn = true;
if (this.usingYouTubeCaptions) {
this.youTubePlayer.loadModule(this.ytCaptionModule);
}
else {
this.$captionDiv.show();
}
for (var i=0; i<captions.length; i++) {
if (captions[i].def === true) { // this is the default language
this.selectedCaptions = captions[i];
}
}
this.selectedCaptions = this.captions[0];
if (this.descriptions.length >= 0) {
this.selectedDescriptions = this.descriptions[0];
}
}
this.refreshControls();
}
else {
if (this.captionsPopup.is(':visible')) {
this.captionsPopup.hide();
this.hidingPopup = false;
this.$ccButton.focus();
}
else {
this.closePopups();
this.captionsPopup.show();
this.captionsPopup.css('top', this.$ccButton.position().top - this.captionsPopup.outerHeight());
this.captionsPopup.css('left', this.$ccButton.position().left)
// Focus on the checked button, if any buttons are checked
// Otherwise, focus on the first button
this.captionsPopup.find('li').removeClass('able-focus');
if (this.captionsPopup.find('input:checked')) {
this.captionsPopup.find('input:checked').focus().parent().addClass('able-focus');
}
else {
this.captionsPopup.find('input').first().focus().parent().addClass('able-focus');
}
}
}
};
AblePlayer.prototype.handleChapters = function () {
if (this.hidingPopup) {
// stopgap to prevent spacebar in Firefox from reopening popup
// immediately after closing it
this.hidingPopup = false;
return false;
}
if (this.chaptersPopup.is(':visible')) {
this.chaptersPopup.hide();
this.hidingPopup = false;
this.$chaptersButton.focus();
}
else {
this.closePopups();
this.chaptersPopup.show();
this.chaptersPopup.css('top', this.$chaptersButton.position().top - this.chaptersPopup.outerHeight());
this.chaptersPopup.css('left', this.$chaptersButton.position().left)
// Focus on the checked button, if any buttons are checked
// Otherwise, focus on the first button
this.chaptersPopup.find('li').removeClass('able-focus');
if (this.chaptersPopup.find('input:checked')) {
this.chaptersPopup.find('input:checked').focus().parent().addClass('able-focus');
}
else {
this.chaptersPopup.find('input').first().focus().parent().addClass('able-focus');
}
}
};
AblePlayer.prototype.handleDescriptionToggle = function() {
this.descOn = !this.descOn;
this.updateDescription();
this.refreshControls();
};
AblePlayer.prototype.handlePrefsClick = function() {
this.setFullscreen(false);
this.prefsDialog.show();
};
AblePlayer.prototype.handleHelpClick = function() {
this.setFullscreen(false);
this.helpDialog.show();
};
AblePlayer.prototype.handleTranscriptToggle = function () {
if (this.$transcriptDiv.is(':visible')) {
this.$transcriptArea.hide();
this.$transcriptButton.addClass('buttonOff').attr('aria-label',this.tt.showTranscript);
this.$transcriptButton.find('span.able-clipped').text(this.tt.showTranscript);
}
else {
this.$transcriptArea.show();
this.$transcriptButton.removeClass('buttonOff').attr('aria-label',this.tt.hideTranscript);
this.$transcriptButton.find('span.able-clipped').text(this.tt.hideTranscript);
}
};
AblePlayer.prototype.handleSignToggle = function () {
if (this.$signWindow.is(':visible')) {
this.$signWindow.hide();
this.$signButton.addClass('buttonOff').attr('aria-label',this.tt.showSign);
this.$signButton.find('span.able-clipped').text(this.tt.showSign);
}
else {
this.$signWindow.show();
// get starting position of element; used for drag & drop
var signWinPos = this.$signWindow.offset();
this.dragStartX = signWinPos.left;
this.dragStartY = signWinPos.top;
this.$signButton.removeClass('buttonOff').attr('aria-label',this.tt.hideSign);
this.$signButton.find('span.able-clipped').text(this.tt.hideSign);
}
};
AblePlayer.prototype.isFullscreen = function () {
if (this.nativeFullscreenSupported()) {
return (document.fullscreenElement ||
document.webkitFullscreenElement ||
document.webkitCurrentFullScreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement) ? true : false;
}
else {
return this.modalFullscreenActive ? true : false;
}
}
AblePlayer.prototype.setFullscreen = function (fullscreen) {
if (this.isFullscreen() == fullscreen) {
return;
}
var thisObj = this;
var $el = this.$ableDiv;
var el = $el[0];
if (this.nativeFullscreenSupported()) {
// Note: many varying names for options for browser compatibility.
if (fullscreen) {
// If not in full screen, initialize it.
if (el.requestFullscreen) {
el.requestFullscreen();
}
else if (el.webkitRequestFullscreen) {
el.webkitRequestFullscreen();
}
else if (el.mozRequestFullScreen) {
el.mozRequestFullScreen();
}
else if (el.msRequestFullscreen) {
el.msRequestFullscreen();
}
}
else {
// If in fullscreen, exit it.
if (document.exitFullscreen) {
document.exitFullscreen();
}
else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
else if (document.webkitCancelFullScreen) {
document.webkitCancelFullScreen();
}
else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
}
else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
}
else {
// Non-native fullscreen support through modal dialog.
// Create dialog on first run through.
if (!this.fullscreenDialog) {
var dialogDiv = $('<div>');
this.fullscreenDialog = new AccessibleDialog(dialogDiv, 'Fullscreen dialog', 'Fullscreen video player', '100%', true, function () { thisObj.handleFullscreenToggle() });
$('body').append(dialogDiv);
}