-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.js
78 lines (66 loc) ยท 2.31 KB
/
player.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
const audio = document.getElementById("audio");
const btnStart = document.querySelector(".player .bottom button");
const svgCircle = document.querySelector('.player .cover svg');
const img = document.querySelector('.player .cover img');
const timeLabel = document.querySelector('.player .time');
const circleBack = svgCircle.children[0];
const circleProgress = svgCircle.children[1];
const strokeLength = circleProgress.getTotalLength();
let isPlaying = false;
let audioDuration = 0;
circleProgress.style['stroke-dasharray'] = strokeLength + 'px';
function setProgress(percent){
circleProgress.style['stroke-dashoffset'] =
(strokeLength - (( strokeLength * percent) / 100 )) + 'px';
}
setProgress(0);
circleBack.addEventListener('click', (event) => {
const x = event.offsetX - (circleProgress.getBBox().width/2);
const y = -(event.offsetY - (circleProgress.getBBox().height/2));
let angle = (180 / Math.PI) * Math.atan2(x, y);
if(angle<0) { angle+=360 }
const anglePercent = (angle*100)/360;
setProgress(anglePercent);
audio.currentTime = anglePercent*audioDuration/100;
});
function pausePlaying() {
isPlaying = false;
btnStart.innerHTML = 'play_arrow';
img.classList.add('animate-pause');
}
function stopPlaying() {
pausePlaying();
img.classList.remove('animate-pause');
img.classList.remove('animate-rotate');
}
btnStart.addEventListener('click', () => {
if(isPlaying===false){
audio.play();
isPlaying = true;
btnStart.innerHTML = 'pause';
img.classList.add('animate-rotate');
img.classList.remove('animate-pause');
} else {
audio.pause();
pausePlaying();
}
});
function secToString(sec) {
const twoDigit = function(num) {
return ("0" + num).slice(-2);
};
const minutes = Math.floor(sec / 60);
const seconds = Math.floor(sec - minutes * 60);
return twoDigit(minutes) + ":" + twoDigit(seconds);
}
function timeUpdate(currentTime, duration){
setProgress(Math.floor((currentTime*100)/duration));
timeLabel.innerHTML = " - " + secToString(currentTime);
if((duration-1)<currentTime){ stopPlaying(); }
}
function timeLoad(duration){
audioDuration = duration;
}
// Create a bound on sound pollution xD
var audx = document.getElementById("audio");
audx.volume = 0.3;