-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
124 lines (104 loc) · 3.58 KB
/
script.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
const palyLsitContainerTag =
document.getElementsByClassName("playListContainer")[0];
const audioTag = document.getElementsByClassName("audioTag")[0];
const currentAndTotalTimeTag = document.getElementsByClassName(
"currentAndTotalTime"
)[0];
const currentProgressTag = document.getElementById("currentProgress");
const playButtonTag = document.getElementsByClassName("playButton")[0];
const pauseButtonTag = document.getElementsByClassName("pauseButton")[0];
const previousButtonTag = document.getElementsByClassName("previousButton")[0];
const nextButtonTag = document.getElementsByClassName("nextButton")[0];
const tracks = [
{ trackId: "music/track1.mp3", title: "December Nya - Lin Nit" },
{ trackId: "music/track2.mp3", title: "Tsaw Ra Ai Tsaw Hkrup Sai - Ann Naw" },
{
trackId: "music/track3.mp3",
title: "Lann Mha Gyee Yey Bey - Wine Su Khine Thein",
},
{ trackId: "music/track4.mp3", title: "Yee Zarr Sar - Sai Sai Kham Hlaing" },
];
for (let i = 0; i < tracks.length; i++) {
const trackTag = document.createElement("div");
trackTag.addEventListener("click", () => {
currentPlayingIndex = i;
playSong();
});
trackTag.classList.add("trackItem");
const title = (i + 1).toString() + ". " + tracks[i].title;
trackTag.textContent = title;
palyLsitContainerTag.append(trackTag);
}
let duration = 0;
let durationText = "00:00";
audioTag.addEventListener("loadeddata", () => {
duration = Math.floor(audioTag.duration);
durationText = creatMinuteAndSecondText(duration);
});
audioTag.addEventListener("timeupdate", () => {
const currentTime = Math.floor(audioTag.currentTime);
const currentTimeText = creatMinuteAndSecondText(currentTime);
const currentTimeTextAndDurationText = currentTimeText + " / " + durationText;
currentAndTotalTimeTag.textContent = currentTimeTextAndDurationText;
updateCurrentProgress(currentTime);
});
const updateCurrentProgress = (currentTime) => {
const currentProgressWidth = (500 / duration) * currentTime;
currentProgressTag.style.width = currentProgressWidth.toString() + "px";
};
const creatMinuteAndSecondText = (totalSecond) => {
const minutes = Math.floor(totalSecond / 60);
const seconds = totalSecond % 60;
const minutesText = minutes < 10 ? "0" + minutes.toString() : minutes;
const secondsText = seconds < 10 ? "0" + seconds.toString() : seconds;
return minutesText + ":" + secondsText;
};
let currentPlayingIndex = 0;
let isPlaying = false;
playButtonTag.addEventListener("click", () => {
isPlaying = true;
const currentTime = Math.floor(audioTag.currentTime);
if (currentTime === 0) {
playSong();
} else {
audioTag.play();
updatePlayAndPauseButton();
}
});
pauseButtonTag.addEventListener("click", () => {
isPlaying = false;
audioTag.pause();
updatePlayAndPauseButton();
});
previousButtonTag.addEventListener("click", () => {
if (currentPlayingIndex === 0) {
currentPlayingIndex = tracks.length - 1;
} else {
currentPlayingIndex -= 1;
}
playSong();
});
nextButtonTag.addEventListener("click", () => {
if (currentPlayingIndex === tracks.length - 1) {
currentPlayingIndex = 0;
} else {
currentPlayingIndex += 1;
}
playSong();
});
const playSong = () => {
const songIdToPlay = tracks[currentPlayingIndex].trackId;
audioTag.src = songIdToPlay;
audioTag.play();
isPlaying = true;
updatePlayAndPauseButton();
};
const updatePlayAndPauseButton = () => {
if (isPlaying) {
playButtonTag.style.display = "none";
pauseButtonTag.style.display = "inline";
} else {
playButtonTag.style.display = "inline";
pauseButtonTag.style.display = "none";
}
};