Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
201 lines (182 loc) · 5.75 KB
/
Copy pathscript.js
File metadata and controls
201 lines (182 loc) · 5.75 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const musicsData = [
{ title: "Solar", artist: "Betical", id: 1 },
{ title: "Electric-Feel", artist: "TEEMID", id: 2 },
{ title: "Aurora", artist: "SLUMB", id: 3 },
{ title: "Lost-Colours", artist: "Fakear", id: 4 },
];
const musicPlayer = document.querySelector('audio');
const thumbnail = document.querySelector('.thumbnail');
const title = document.querySelector('.music-title');
const artist = document.querySelector('.artist-name');
const prevBtn = document.querySelector('.prev-btn');
const playBtn = document.querySelector('.play-btn');
const nextBtn = document.querySelector('.next-btn');
const currentIndex = document.querySelector('.current-index span');
const shuffle = document.querySelector('.shuffle');
const repeat = document.querySelector('.repeat');
const progressContainer = document.querySelector('.progress-container');
const progressBar = document.querySelector('.progress-bar');
const currentTimeTxt = document.querySelector('.current-time');
const durationTimeTxt = document.querySelector('.duration-time');
const canvas = document.querySelector('canvas');
let index = 0;
let prevIndex = null;
let isRandom = false;
let repeatValue = 0;
prevBtn.addEventListener('click', handlePrevMusic);
nextBtn.addEventListener('click', handleNextMusic);
playBtn.addEventListener('click', handlePlayPause);
shuffle.addEventListener('click', handleRandomChange);
repeat.addEventListener('click', handleRepeatChange);
musicPlayer.addEventListener('timeupdate', progressEvolution);
progressContainer.addEventListener('click', setProgress)
musicPlayer.addEventListener('play', () => {
const contextAudio = new AudioContext();
const src = contextAudio.createMediaElementSource(musicPlayer);
const analyser = contextAudio.createAnalyser();
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext("2d");
src.connect(analyser);
analyser.connect(contextAudio.destination);
analyser.fftSize = 1024;
const audioFrequences = analyser.frequencyBinCount;
const tabFrequences = new Uint8Array(audioFrequences);
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
let widthBar = (WIDTH / tabFrequences.length) + 2;
let heightBar;
let x;
function returnBars() {
requestAnimationFrame(returnBars);
x = 0;
analyser.getByteFrequencyData(tabFrequences);
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(0,0,0,0)"
ctx.fillRect(0, 0, WIDTH, HEIGHT);
for (let i = 0; i < audioFrequences; i++){
heightBar = tabFrequences[i];
let r = 109;
let g = 214;
let b = tabFrequences[i];
ctx.fillStyle = `rgb(${r}, ${g}, ${b})`;
ctx.fillRect(x, HEIGHT, widthBar, - heightBar);
x += widthBar + 1;
}
}
returnBars();
})
update();
function progressEvolution() {
const evolution = (musicPlayer.currentTime / musicPlayer.duration) * 100;
progressBar.style.width = `${evolution}%`;
formatValue(musicPlayer.currentTime, currentTimeTxt);
formatValue(musicPlayer.duration, durationTimeTxt);
if (musicPlayer.currentTime === musicPlayer.duration) {
console.log(index);
if (repeatValue === 0) {
pause();
}else if (repeatValue === 1) {
if (index === 3) {
handleNextMusic();
} else {
handleNextMusic();
}
} else if (repeatValue === 2) {
musicPlayer.currentTime = 0;
play();
}
}
}
function handlePrevMusic() {
if (musicPlayer.currentTime > 3) {
musicPlayer.currentTime = 0;
} else {
if (!isRandom) {
if(index > 0)index--;
else index = 3;
} else {
if (prevIndex) index = prevIndex;
else {
if(index > 0)index--;
else index = 3;
}
}
}
update();
}
function handleNextMusic() {
if (!isRandom) {
if (index < 3) index++;
else index = 0;
}else {
let randomIndex = null;
while (!randomIndex || randomIndex === index || randomIndex === prevIndex) {
randomIndex = Math.trunc(Math.random() * 4);
}
prevIndex = index;
console.log(randomIndex, prevIndex)
index = randomIndex;
}
update()
}
function handlePlayPause() {
if (musicPlayer.paused) play()
else pause()
}
function play(){
playBtn.querySelector('img').src = './ressources/icons/pause-icon.svg';
musicPlayer.play();
}
function pause(){
playBtn.querySelector('img').src = './ressources/icons/play-icon.svg';
musicPlayer.pause();
}
function handleRandomChange() {
isRandom = !isRandom;
console.log(isRandom);
if (isRandom) shuffle.className = 'shuffle active';
else shuffle.className = 'shuffle';
}
function handleRepeatChange() {
if (repeatValue < 2) {
repeatValue++
if (repeatValue === 1) {
repeat.classList.add('active');
} else if(repeatValue === 2) {
repeat.classList.add('one');
}
}
else {
repeatValue = 0;
repeat.classList.remove('one');
repeat.classList.remove('active');
}
console.log(repeatValue, repeat.classList);
}
function formatValue(val, element) {
const currentMin = Math.trunc(val / 60);
let currentSec = Math.trunc(val % 60);
if (currentSec < 10) {
currentSec = `0${currentSec}`;
}
element.textContent = val ? `${currentMin}:${currentSec}`: '0:00';
}
function setProgress(e) {
let rect = progressContainer.getBoundingClientRect();
console.log(rect);
let width = rect.width;
const x = e.clientX - rect.left;
musicPlayer.currentTime = ((x / width) * musicPlayer.duration);
console.log((width/x) / 100);
}
function init() {
}
async function update() {
musicPlayer.src = `ressources/music/${await musicsData[index].title}.mp3`;
thumbnail.src = `ressources/thumbs/${await musicsData[index].title}.png`;
title.textContent = await musicsData[index].title;
artist.textContent = await musicsData[index].artist;
currentIndex.textContent = await musicsData[index].id;
play();
}