-
Notifications
You must be signed in to change notification settings - Fork 123
/
api.js
99 lines (88 loc) · 2.77 KB
/
api.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
/**
* These are custom helper methods that are not native
* to the HTMLMediaElement API. Pass in the native
* Video element, state and optional desired value to
* set. To be primarily used in `mapVideoElToProps`.
*/
export const togglePause = (videoEl, { paused }) => {
if (paused) {
videoEl.play();
} else {
videoEl.pause();
}
};
export const setCurrentTime = (videoEl, state, value) => {
videoEl.currentTime = value;
};
export const setVolume = (videoEl, state, value) => {
videoEl.muted = false;
videoEl.volume = value;
};
export const mute = (videoEl) => {
videoEl.muted = true;
};
export const unmute = (videoEl) => {
videoEl.muted = false;
};
export const toggleMute = (videoEl, { volume, muted }) => {
if (muted || volume <= 0) {
if (volume <= 0) {
videoEl.volume = 1;
}
videoEl.muted = false;
} else {
videoEl.muted = true;
}
};
export const toggleFullscreen = (videoEl) => {
videoEl.requestFullScreen =
videoEl.requestFullscreen
|| videoEl.msRequestFullscreen
|| videoEl.mozRequestFullScreen
|| videoEl.webkitRequestFullscreen;
document.exitFullscreen =
document.exitFullscreen
|| document.msExitFullscreen
|| document.mozCancelFullScreen
|| document.webkitExitFullscreen;
const fullscreenElement =
document.fullscreenElement
|| document.msFullscreenElement
|| document.mozFullScreenElement
|| document.webkitFullscreenElement;
if (fullscreenElement === videoEl) {
document.exitFullscreen();
} else {
videoEl.requestFullScreen();
}
};
export const showTrack = ({ textTracks }, track) => {
hideTracks({ textTracks });
track.mode = track.SHOWING || 'showing';
};
export const hideTracks = ({ textTracks }) => {
for (var i = 0; i < textTracks.length; i++) {
textTracks[i].mode = textTracks[i].DISABLED || 'disabled';
}
};
export const toggleTracks = (() => {
let previousTrack;
return ({ textTracks }) => {
let currentTrack = [...textTracks]
.filter((track) => track.mode === track.SHOWING || track.mode === 'showing')[0];
if (currentTrack) {
hideTracks({ textTracks });
previousTrack = currentTrack;
} else {
showTrack({ textTracks }, previousTrack || textTracks[0]);
}
}})();
/**
* Custom getter methods that are commonly used
* across video layouts. To be primarily used in
* `mapStateToProps`
*/
export const getPercentageBuffered = ({ buffered, duration }) =>
buffered && buffered.length && buffered.end(buffered.length - 1) / duration * 100 || 0;
export const getPercentagePlayed = ({ currentTime, duration }) =>
currentTime / duration * 100;