-
Notifications
You must be signed in to change notification settings - Fork 6
/
music.js
221 lines (196 loc) · 8.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const API_KEY = 'AIzaSyCmw1LikX2ci00qharZd7o0ztYWW3pYcvg';
let playlist = [];
let isPlayerInitialized = false;
document.addEventListener('DOMContentLoaded', () => {
const toggleBtn = document.getElementById('toggle-btn');
const musicPlayer = document.getElementById('music-player');
const closeBtn = document.getElementById('close-btn');
const songImage = document.getElementById('song-image');
const songName = document.getElementById('song-name');
const fetchBtn = document.getElementById('fetch-btn');
const urlInput = document.getElementById('url-input');
const playPauseBtn = document.getElementById('play-pause-btn');
const playIcon = document.getElementById('play-icon');
const pauseIcon = document.getElementById('pause-icon');
const volumeSlider = document.getElementById('volume-slider');
const playlistDiv = document.getElementById('playlist');
const tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
let player;
window.onYouTubeIframeAPIReady = () => {
player = new YT.Player('player', {
height: '0',
width: '0',
events: {
'onReady': onPlayerReady
}
});
};
function onPlayerReady(event) {
loadPlayerState();
isPlayerInitialized = true;
}
function extractVideoId(url) {
const urlObj = new URL(url);
const params = new URLSearchParams(urlObj.search);
return params.get('v') || urlObj.pathname.split('/').pop();
}
function fetchSongData(videoId) {
fetch(`https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoId}&key=${API_KEY}`)
.then(response => response.json())
.then(data => {
const videoData = data.items[0].snippet;
songImage.src = videoData.thumbnails.high.url;
songName.textContent = videoData.title;
playlist.push({ videoId, title: videoData.title, thumbnail: videoData.thumbnails.high.url });
updatePlaylist();
if (player && typeof player.loadVideoById === 'function') {
player.loadVideoById(videoId);
savePlayerState();
} else {
console.error('Player is not properly initialized or does not support loadVideoById.');
}
})
.catch(error => console.error('Error fetching video data:', error));
}
function updatePlaylist() {
playlistDiv.innerHTML = '';
playlist.forEach((item, index) => {
const playlistItem = document.createElement('div');
playlistItem.className = 'playlist-item';
playlistItem.innerHTML = `
<img src="${item.thumbnail}" alt="Thumbnail">
<span>${item.title}</span>
`;
playlistItem.addEventListener('click', () => {
if (player && typeof player.loadVideoById === 'function') {
player.loadVideoById(item.videoId);
songImage.src = item.thumbnail;
songName.textContent = item.title;
playIcon.style.display = 'none';
pauseIcon.style.display = 'block';
savePlayerState();
} else {
console.error('Player is not properly initialized or does not support loadVideoById.');
}
});
playlistDiv.appendChild(playlistItem);
});
}
function savePlayerState() {
if (player && typeof player.getVideoData === 'function') {
const currentVideoId = player.getVideoData().video_id;
const currentTime = player.getCurrentTime();
const volume = player.getVolume();
setCookie('currentVideoId', currentVideoId, 7);
setCookie('currentTime', currentTime, 7);
setCookie('volume', volume, 7);
}
}
function loadPlayerState() {
const savedVideoId = getCookie('currentVideoId');
const savedTime = parseFloat(getCookie('currentTime'));
const savedVolume = parseFloat(getCookie('volume'));
if (savedVideoId) {
player.cueVideoById(savedVideoId); // Use cueVideoById instead of loadVideoById to allow seeking before playing
player.addEventListener('onStateChange', function (event) {
if (event.data === YT.PlayerState.CUED) {
if (savedTime && !isNaN(savedTime)) {
player.seekTo(savedTime, true);
}
if (savedVolume && !isNaN(savedVolume)) {
player.setVolume(savedVolume);
volumeSlider.value = savedVolume;
}
}
});
} else {
songImage.src = '/img/placeholder.png';
songName.textContent = 'No song loaded';
}
}
fetchBtn.addEventListener('click', () => {
const url = urlInput.value.trim();
if (url) {
const videoId = extractVideoId(url);
if (videoId) {
fetchSongData(videoId);
} else {
alert('Invalid YouTube URL');
}
} else {
alert('Please enter a YouTube URL');
}
});
playPauseBtn.addEventListener('click', () => {
if (player && typeof player.getPlayerState === 'function') {
const playerState = player.getPlayerState();
if (playerState === YT.PlayerState.PLAYING) {
player.pauseVideo();
playIcon.style.display = 'block';
pauseIcon.style.display = 'none';
} else {
player.playVideo();
playIcon.style.display = 'none';
pauseIcon.style.display = 'block';
}
savePlayerState();
} else {
console.error('Player is not properly initialized or does not support getPlayerState.');
}
});
volumeSlider.addEventListener('input', () => {
const volume = volumeSlider.value;
if (player && typeof player.setVolume === 'function') {
player.setVolume(volume);
savePlayerState();
} else {
console.error('Player is not properly initialized or does not support setVolume.');
}
});
toggleBtn.addEventListener('click', () => {
if (musicPlayer.classList.contains('show')) {
musicPlayer.classList.remove('show');
musicPlayer.classList.add('hide');
setTimeout(() => {
musicPlayer.style.display = 'none';
}, 300);
} else {
musicPlayer.style.display = 'flex';
setTimeout(() => {
musicPlayer.classList.remove('hide');
musicPlayer.classList.add('show');
}, 10);
}
});
closeBtn.addEventListener('click', () => {
musicPlayer.classList.remove('show');
musicPlayer.classList.add('hide');
setTimeout(() => {
musicPlayer.style.display = 'none';
}, 300);
});
// Collapse the player by default
musicPlayer.style.display = 'none';
});
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function deleteCookie(name) {
document.cookie = name + '=; Max-Age=-99999999;';
}