-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
109 lines (92 loc) · 3.35 KB
/
main.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
// Possible improvements:
// - Change timeline and volume slider into input sliders, reskinned
// - Change into Vue or React component
// - Be able to grab a custom title instead of "Music Song"
// - Hover over sliders to see preview of timestamp/volume change
const audioPlayer = document.querySelector(".audio-player");
let audioList = [
"music/01.mp3",
"music/02.mp3",
"music/03.mp3",
]
// select random audio
const randomAudio = (audio) => {
return audio[Math.floor(Math.random() * audio.length)];
}
const audio = new Audio(
// "https://ia800905.us.archive.org/19/items/FREE_background_music_dhalius/backsound.mp3"
randomAudio(audioList)
);
// audio.src = audioList[Math.floor(Math.random() * audioList.length)];
// console.dir(audio);
audio.addEventListener(
"loadeddata",
() => {
audioPlayer.querySelector(".time .length").textContent = getTimeCodeFromNum(audio.duration);
audio.volume = .75;
},
false
);
//click on timeline to skip around
const timeline = audioPlayer.querySelector(".timeline");
timeline.addEventListener("click", e => {
const timelineWidth = window.getComputedStyle(timeline).width;
const timeToSeek = e.offsetX / parseInt(timelineWidth) * audio.duration;
audio.currentTime = timeToSeek;
}, false);
//click volume slider to change volume
const volumeSlider = audioPlayer.querySelector(".volume-container .volume-slider");
volumeSlider.addEventListener('click', e => {
const sliderWidth = window.getComputedStyle(volumeSlider).width;
const newVolume = e.offsetX / parseInt(sliderWidth);
audio.volume = newVolume;
audioPlayer.querySelector(".volume-container .volume-percentage").style.width = newVolume * 100 + '%';
}, false)
//check audio percentage and update time accordingly
setInterval(() => {
const progressBar = audioPlayer.querySelector(".progress");
progressBar.style.width = audio.currentTime / audio.duration * 100 + "%";
audioPlayer.querySelector(".time .current").textContent = getTimeCodeFromNum(
audio.currentTime
);
}, 500);
//toggle between playing and pausing on button click
const playBtn = audioPlayer.querySelector(".play-audio .toggle-play");
playBtn.addEventListener(
"click",
() => {
if (audio.paused) {
playBtn.classList.remove("play");
playBtn.classList.add("pause");
audio.play();
} else {
playBtn.classList.remove("pause");
playBtn.classList.add("play");
audio.pause();
}
},
false
);
audioPlayer.querySelector(".volume-button").addEventListener("click", () => {
const volumeEl = audioPlayer.querySelector(".volume-container i");
audio.muted = !audio.muted;
if (audio.muted) {
volumeEl.classList.remove("fa-volume-low");
volumeEl.classList.add("fa-volume-xmark");
} else {
volumeEl.classList.add("fa-volume-low");
volumeEl.classList.remove("fa-volume-xmark");
}
});
// audio time calculator
function getTimeCodeFromNum(num) {
let seconds = parseInt(num);
let minutes = parseInt(seconds / 60);
seconds -= minutes * 60;
const hours = parseInt(minutes / 60);
minutes -= hours * 60;
if (hours === 0) return `${minutes}:${String(seconds % 60).padStart(2, 0)}`;
return `${String(hours).padStart(2, 0)}:${minutes}:${String(
seconds % 60
).padStart(2, 0)}`;
}