This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.js
101 lines (80 loc) · 3.52 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
(function() {
let audioIsPlaying = false;
document.addEventListener('DOMContentLoaded', function() {
let player = document.querySelector('audio');
// play/pause
document.querySelector('.audio-player').addEventListener('click', function() {
if (player.paused) {
player.play();
audioIsPlaying = true;
checkPlayerStatus();
document.querySelector('.audio-player-button').src = "images/svg/pause-icon.svg";
} else {
player.pause();
audioIsPlaying = false;
document.querySelector('.audio-player-button').src = "images/svg/play-icon.svg";
}
});
// restarts the whole audio from the beginning
/*document.querySelector('.audio-player-text').addEventListener('click', function() {
if (player.paused) {
document.querySelectorAll('.audio-player-progress-segment').forEach(function(segment) {
segment.style.display = "none";
});
player.currentTime = 0.0;
player.play();
audioIsPlaying = true;
checkPlayerStatus();
document.querySelector('.audio-player-button').src = "images/svg/Pause-Icon.svg";
}
});*/
// resets the player, if the audio is over
player.ontimeupdate = function () {
const progress = player.currentTime/player.duration;
if (progress == 1.0) {
audioIsPlaying = false;
updateProgressUI();
document.querySelector('.audio-player-button').src = "images/svg/play-icon.svg";
document.querySelectorAll('.audio-player-progress-segment').forEach(function(segment) {
segment.style.display = "none";
});
}
}
});
// Checks, if player is playing and updates the UI accordingly, if so
function checkPlayerStatus() {
if (audioIsPlaying) {
let player = document.querySelector('audio');
let progress = player.currentTime/player.duration;
updateProgressUI(progress);
setTimeout(function () {
checkPlayerStatus();
}, 33);
}
}
// updates the progress bar
function updateProgressUI(progress) {
let segments = document.querySelectorAll('.audio-player-progress-segment');
let segmentProgress;
if (progress < 0.25) {
segments[0].style.display = "block";
segmentProgress = -(90-progress*360.0);
segments[0].style.transform = "skew(0deg,"+segmentProgress+"deg)";
} else if (progress < 0.5) {
segments[0].style.transform = "skew(0deg, 0deg)";
segmentProgress = (180-progress*360.0);
segments[1].style.transform = "skew("+segmentProgress+"deg, 0deg)";
segments[1].style.display = "block";
} else if (progress < 0.75) {
segments[1].style.transform = "skew(0deg, 0deg)";
segmentProgress = -(270-progress*360.0);
segments[2].style.transform = "skew(0deg,"+segmentProgress+"deg)";
segments[2].style.display = "block";
} else {
segments[2].style.transform = "skew(0deg, 0deg)";
segmentProgress = (360-progress*360.0);
segments[3].style.transform = "skew("+segmentProgress+"deg, 0deg)";
segments[3].style.display = "block";
}
}
})();