-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.html
157 lines (142 loc) · 4.63 KB
/
spotify.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Music Player</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
background-color: #181818;
color: #fff;
}
.player {
margin-top: 50px;
text-align: center;
}
button {
background-color: #1DB954;
color: white;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 25px;
margin: 5px;
}
button:hover {
background-color: #1AA34A;
}
input[type="text"] {
padding: 10px;
border-radius: 25px;
border: none;
font-size: 16px;
}
progress {
width: 80%;
height: 10px;
margin-top: 10px;
background-color: #444;
border-radius: 5px;
}
progress::-webkit-progress-bar {
background-color: #444;
border-radius: 5px;
}
progress::-webkit-progress-value {
background-color: #1DB954;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>My Music Player</h1>
<!-- Search Bar -->
<input type="text" id="searchInput" placeholder="Search for a song..." oninput="searchSong()">
<ul id="songList"></ul>
<div class="player">
<h3 id="currentSong">Now Playing: Select a Song</h3>
<audio id="audioPlayer" onended="skipSong()" controls hidden></audio>
<br>
<button onclick="togglePlay()" id="playPauseBtn">Play</button>
<button onclick="skipSong()">Skip</button>
<button onclick="toggleLoop()" id="loopBtn">Enable Loop</button>
<!-- Green Progress Bar -->
<progress id="progressBar" value="0" max="100"></progress>
</div>
<script>
// List of songs (Update manually or load dynamically from server if needed)
const songs = ["bite me.mp3", "We Will Rock You.mp3", "World's Smallest Violin.mp3"];
let currentSongIndex = -1;
const audioPlayer = document.getElementById("audioPlayer");
const currentSongDisplay = document.getElementById("currentSong");
const playPauseBtn = document.getElementById("playPauseBtn");
const songList = document.getElementById("songList");
const progressBar = document.getElementById("progressBar");
// Display all songs initially
function displaySongs(songsToDisplay) {
songList.innerHTML = "";
songsToDisplay.forEach((song, index) => {
const li = document.createElement("li");
li.textContent = song.replace(".mp3", "");
li.style.cursor = "pointer";
li.onclick = () => selectSong(index);
songList.appendChild(li);
});
}
// Initialize with all songs listed
displaySongs(songs);
function searchSong() {
const searchTerm = document.getElementById("searchInput").value.toLowerCase();
const filteredSongs = songs.filter(song => song.toLowerCase().includes(searchTerm));
displaySongs(filteredSongs);
}
function selectSong(index) {
currentSongIndex = index;
playCurrentSong();
}
function toggleLoop() {
audioPlayer.loop = !audioPlayer.loop;
const loopBtn = document.getElementById("loopBtn");
loopBtn.textContent = audioPlayer.loop ? "Disable Loop" : "Enable Loop";
}
function playCurrentSong() {
if (currentSongIndex === -1) return;
audioPlayer.src = "music/" + songs[currentSongIndex];
console.log(audioPlayer.src);
currentSongDisplay.textContent = "Now Playing: " + songs[currentSongIndex].replace(".mp3", "");
audioPlayer.play();
playPauseBtn.textContent = "Pause";
}
function togglePlay() {
if (audioPlayer.paused) {
audioPlayer.play();
playPauseBtn.textContent = "Pause";
} else {
audioPlayer.pause();
playPauseBtn.textContent = "Play";
}
}
function skipSong() {
currentSongIndex = Math.floor(Math.random() * songs.length);
playCurrentSong();
}
// Update progress bar as the song plays
audioPlayer.addEventListener("timeupdate", function() {
const progress = (audioPlayer.currentTime / audioPlayer.duration) * 100;
progressBar.value = progress;
});
// Make progress bar clickable to skip to a specific time in the song
progressBar.addEventListener("click", function(event) {
const progressBarWidth = progressBar.offsetWidth;
const clickPosition = event.offsetX;
const clickedPercentage = (clickPosition / progressBarWidth) * 100;
audioPlayer.currentTime = (clickedPercentage / 100) * audioPlayer.duration;
});
</script>
</body>
</html>