-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
audio.js
146 lines (127 loc) · 5.68 KB
/
audio.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
(function() {
const handleAudioPlayer = function (audioElementContainer) {
const audioPlayerContainer = audioElementContainer.querySelector('.kg-audio-player-container');
const playIconContainer = audioElementContainer.querySelector('.kg-audio-play-icon');
const pauseIconContainer = audioElementContainer.querySelector('.kg-audio-pause-icon');
const seekSlider = audioElementContainer.querySelector('.kg-audio-seek-slider');
const playbackRateContainer = audioElementContainer.querySelector('.kg-audio-playback-rate');
const muteIconContainer = audioElementContainer.querySelector('.kg-audio-mute-icon');
const unmuteIconContainer = audioElementContainer.querySelector('.kg-audio-unmute-icon');
const volumeSlider = audioElementContainer.querySelector('.kg-audio-volume-slider');
const audio = audioElementContainer.querySelector('audio');
const durationContainer = audioElementContainer.querySelector('.kg-audio-duration');
const currentTimeContainer = audioElementContainer.querySelector('.kg-audio-current-time');
let playbackRates = [{
rate: 0.75,
label: '0.7×'
}, {
rate: 1.0,
label: '1×'
}, {
rate: 1.25,
label: '1.2×'
}, {
rate: 1.75,
label: '1.7×'
}, {
rate: 2.0,
label: '2×'
}];
let raf = null;
let currentPlaybackRateIdx = 1;
const whilePlaying = () => {
seekSlider.value = Math.floor(audio.currentTime);
currentTimeContainer.textContent = calculateTime(seekSlider.value);
audioPlayerContainer.style.setProperty('--seek-before-width', `${seekSlider.value / seekSlider.max * 100}%`);
raf = requestAnimationFrame(whilePlaying);
}
const showRangeProgress = (rangeInput) => {
if (rangeInput === seekSlider) {
audioPlayerContainer.style.setProperty('--seek-before-width', rangeInput.value / rangeInput.max * 100 + '%');
}
else {
audioPlayerContainer.style.setProperty('--volume-before-width', rangeInput.value / rangeInput.max * 100 + '%');
}
}
const calculateTime = (secs) => {
const minutes = Math.floor(secs / 60);
const seconds = Math.floor(secs % 60);
const returnedSeconds = seconds < 10 ? `0${seconds}` : `${seconds}`;
return `${minutes}:${returnedSeconds}`;
}
const displayDuration = () => {
durationContainer.textContent = calculateTime(audio.duration);
}
const setSliderMax = () => {
seekSlider.max = Math.floor(audio.duration);
}
const displayBufferedAmount = () => {
if (audio.buffered.length > 0) {
const bufferedAmount = Math.floor(audio.buffered.end(audio.buffered.length - 1));
audioPlayerContainer.style.setProperty('--buffered-width', `${(bufferedAmount / seekSlider.max) * 100}%`);
}
}
if (audio.readyState > 0) {
displayDuration();
setSliderMax();
displayBufferedAmount();
} else {
audio.addEventListener('loadedmetadata', () => {
displayDuration();
setSliderMax();
displayBufferedAmount();
});
}
playIconContainer.addEventListener('click', () => {
playIconContainer.classList.add("kg-audio-hide");
pauseIconContainer.classList.remove("kg-audio-hide");
audio.play();
requestAnimationFrame(whilePlaying);
});
pauseIconContainer.addEventListener('click', () => {
pauseIconContainer.classList.add("kg-audio-hide");
playIconContainer.classList.remove("kg-audio-hide");
audio.pause();
cancelAnimationFrame(raf);
});
muteIconContainer.addEventListener('click', () => {
muteIconContainer.classList.add("kg-audio-hide");
unmuteIconContainer.classList.remove("kg-audio-hide");
audio.muted = false;
});
unmuteIconContainer.addEventListener('click', () => {
unmuteIconContainer.classList.add("kg-audio-hide");
muteIconContainer.classList.remove("kg-audio-hide");
audio.muted = true;
});
playbackRateContainer.addEventListener('click', () => {
let nextPlaybackRate = playbackRates[(currentPlaybackRateIdx + 1) % 5];
currentPlaybackRateIdx = currentPlaybackRateIdx + 1;
audio.playbackRate = nextPlaybackRate.rate;
playbackRateContainer.textContent = nextPlaybackRate.label;
});
audio.addEventListener('progress', displayBufferedAmount);
seekSlider.addEventListener('input', (e) => {
showRangeProgress(e.target);
currentTimeContainer.textContent = calculateTime(seekSlider.value);
if (!audio.paused) {
cancelAnimationFrame(raf);
}
});
seekSlider.addEventListener('change', () => {
audio.currentTime = seekSlider.value;
if (!audio.paused) {
requestAnimationFrame(whilePlaying);
}
});
volumeSlider.addEventListener('input', (e) => {
const value = e.target.value;
showRangeProgress(e.target);
audio.volume = value / 100;
});
}
const audioCardElements = document.querySelectorAll('.kg-audio-card');
for (let i = 0; i < audioCardElements.length; i++) {
handleAudioPlayer(audioCardElements[i]);
}
})();