-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
218 lines (177 loc) · 6.01 KB
/
player.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
function createPlayer({
elementId,
src='https://dvmn.org/media/filer_public/78/db/78db3456-3fd3-4504-9ed9-d2d1fd843c0b/highest_peak.mp4'
}){
const player = Playable.create({
fillAllSpace: true,
src: src,
hideOverlay: true,
hideMainUI: true,
});
const playerContainer = document.getElementById(elementId)
if (!playerContainer){
throw Error(`Element with id "${elementId}" not found.`);
}
const videoContainers = playerContainer.getElementsByClassName('js-video-container');
if (!videoContainers.length){
throw Error(`Element with class "js-video-container" not found.`);
}
if (videoContainers.length > 1){
throw Error(`Expects single element with class "js-video-container", but ${videoContainers.length} were found.`);
}
const videoContainer = videoContainers[0];
player.attachToElement(videoContainer);
const $playerContainer = $(playerContainer);
(function activatePlayButtons(){
const $playButton = $playerContainer.find('.js-play-button');
const $pauseButton = $playerContainer.find('.js-pause-button');
$playButton.click(()=>{
player.play();
});
$pauseButton.click(()=>{
player.pause();
});
function activatePlayBtn(){
$playButton.attr("hidden", false);
$pauseButton.attr("hidden", true);
}
function activatePauseBtn(){
$playButton.attr("hidden", true);
$pauseButton.attr("hidden", false);
}
activatePlayBtn();
player.on(Playable.ENGINE_STATES.PLAYING, activatePauseBtn);
player.on(Playable.ENGINE_STATES.PAUSED, activatePlayBtn);
player.on(Playable.ENGINE_STATES.ENDED, ()=>{
player.reset();
activatePlayBtn();
});
})();
(function activateVolumeButtons(){
const $volumeButton = $playerContainer.find('.js-volume-button');
const $muteButton = $playerContainer.find('.js-mute-button');
$volumeButton.click(()=>{
player.setVolume(100);
});
$muteButton.click(()=>{
player.setVolume(0);
});
function activateVolumeButton(){
$volumeButton.attr("hidden", false);
$muteButton.attr("hidden", true);
}
function activateMuteBtn(){
$volumeButton.attr("hidden", true);
$muteButton.attr("hidden", false);
}
function toggleVolumeMuteBtns(){
if (player.getVolume() > 0){
activateMuteBtn();
} else {
activateVolumeButton();
}
}
player.on(Playable.VIDEO_EVENTS.VOLUME_CHANGED, toggleVolumeMuteBtns);
toggleVolumeMuteBtns();
})();
const $fullscreenButton = $playerContainer.find('.js-fullscreen-button');
$fullscreenButton.click(()=>{
player.enterFullScreen();
});
function formatTime(seconds) {
// StackOverflow snippet https://stackoverflow.com/a/52560608
const format = val => `0${Math.floor(val)}`.slice(-2);
const hours = seconds / 3600;
const minutes = (seconds % 3600) / 60;
return [Math.floor(hours), format(minutes), format(seconds % 60)].join(':');
}
(function activateTimeLabels(){
const $currentTime = $playerContainer.find('.js-current-time');
function showCurrentTime(){
const totalSeconds = player.getCurrentTime();
const formattedTime = formatTime(totalSeconds);
$currentTime.text(formattedTime);
}
player.on(Playable.VIDEO_EVENTS.CURRENT_TIME_UPDATED, showCurrentTime);
showCurrentTime();
const $duration = $playerContainer.find('.js-duration');
function showDuration(){
const totalSeconds = player.getDuration();
const formattedTime = formatTime(totalSeconds);
$duration.text(formattedTime);
}
player.on(Playable.VIDEO_EVENTS.DURATION_UPDATED, showDuration);
showDuration();
})();
function throttle(func, ms) {
// Source code snippet https://learn.javascript.ru/task/throttle
let isThrottled = false,
savedArgs,
savedThis;
function wrapper() {
if (isThrottled) { // (2)
savedArgs = arguments;
savedThis = this;
return;
}
func.apply(this, arguments); // (1)
isThrottled = true;
setTimeout(function() {
isThrottled = false; // (3)
if (savedArgs) {
wrapper.apply(savedThis, savedArgs);
savedArgs = savedThis = null;
}
}, ms);
}
return wrapper;
}
(function activateProgressbar(){
const $progress = $playerContainer.find('.js-progress');
const $slider = $progress.find('.js-progress-slider');
function setSliderWidth(percentage){
$slider.css("width", `${percentage}%`);
}
function updateSliderWidth(){
const durationSeconds = player.getDuration();
if (!durationSeconds){
$slider.css("width", "0%");
return;
}
const percentage = player.getCurrentTime() / durationSeconds * 100;
setSliderWidth(percentage);
}
player.on(Playable.VIDEO_EVENTS.CURRENT_TIME_UPDATED, updateSliderWidth);
player.on(Playable.VIDEO_EVENTS.DURATION_UPDATED, updateSliderWidth);
updateSliderWidth();
// Code snippet from https://codepen.io/frytyler/pen/juGfk
function updateVideoProgress(x){
const durationSeconds = player.getDuration();
//calculate drag position
//and update video currenttime
//as well as progress bar
var relPosition = x - $progress.offset().left;
var percentage = 100 * relPosition / $progress.width();
if(percentage > 100) {
percentage = 100;
}
if(percentage < 0) {
percentage = 0;
}
setSliderWidth(percentage); // Redraw before CURRENT_TIME_UPDATED event for better responsiveness
seconds = durationSeconds * percentage / 100;
player.seekTo(seconds);
};
const throttledUpdateVideoProgress = throttle(updateVideoProgress, 100);
$progress.on('mousedown', function(e) {
throttledUpdateVideoProgress(e.pageX);
$(document).on('mousemove', (e) => {
throttledUpdateVideoProgress(e.pageX)
});
});
$progress.on('mouseup', function(e) {
$(document).off('mousemove');
throttledUpdateVideoProgress(e.pageX);
});
})();
}