-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.js
112 lines (95 loc) · 4.01 KB
/
music.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
document.addEventListener('DOMContentLoaded', function () {
var musicBlocks = document.querySelectorAll('div[music]');
musicBlocks.forEach(function (block) {
var title = block.getAttribute('Title');
var imgSrc = block.getAttribute('Img');
var audioSrc = block.getAttribute('File');
var playerHTML = `
<div class="player">
<div class="cover" style="background-image: url('${imgSrc}');" >
<button class="playBtn">►</button>
</div>
<div class="song-info">
<div class="song-title">${title}</div>
<div class="progress-container">
<div class="preview-progress"></div>
<div class="progress"></div>
</div>
<div class="time">0:00 / 0:00</div>
</div>
<audio class="audio-player" src="${audioSrc}"></audio>
</div>
`;
block.innerHTML = playerHTML;
var audioPlayer = block.querySelector('.audio-player');
var playBtn = block.querySelector('.playBtn');
var progressContainer = block.querySelector('.progress-container');
var progress = block.querySelector('.progress');
var previewProgress = block.querySelector('.preview-progress');
var time = block.querySelector('.time');
var isDragging = false;
playBtn.addEventListener('click', function () {
togglePlayPause();
});
progressContainer.addEventListener('mousedown', function (e) {
isDragging = true;
updatePreviewProgress(e);
});
progressContainer.addEventListener('mousemove', function (e) {
if (isDragging) {
updatePreviewProgress(e);
}
});
window.addEventListener('mouseup', function (e) {
if (isDragging) {
isDragging = false;
setProgress(e);
}
});
window.addEventListener('keydown', function (e) {
if (e.code === 'Space') {
e.preventDefault();
togglePlayPause();
}
});
function togglePlayPause() {
if (audioPlayer.paused) {
audioPlayer.play();
playBtn.innerHTML = '❚❚';
} else {
audioPlayer.pause();
playBtn.innerHTML = '►';
}
}
function updatePreviewProgress(e) {
var width = progressContainer.clientWidth;
var clickX = e.offsetX;
var previewPercent = (clickX / width) * 100;
previewProgress.style.width = previewPercent + '%';
}
function setProgress(e) {
var width = progressContainer.clientWidth;
var clickX = e.offsetX;
var duration = audioPlayer.duration;
audioPlayer.currentTime = (clickX / width) * duration;
updateProgress();
}
function updateProgress() {
var currentTime = audioPlayer.currentTime;
var duration = audioPlayer.duration;
var progressPercent = (currentTime / duration) * 100;
progress.style.width = progressPercent + '%';
updateTimeDisplay(currentTime, duration);
}
function updateTimeDisplay(currentTime, duration) {
var currentMinutes = Math.floor(currentTime / 60);
var currentSeconds = Math.floor(currentTime % 60);
if (currentSeconds < 10) currentSeconds = '0' + currentSeconds;
var durationMinutes = Math.floor(duration / 60);
var durationSeconds = Math.floor(duration % 60);
if (durationSeconds < 10) durationSeconds = '0' + durationSeconds;
time.textContent = currentMinutes + ':' + currentSeconds + ' / ' + durationMinutes + ':' + durationSeconds;
}
audioPlayer.addEventListener('timeupdate', updateProgress);
});
});