Create js/player.js
Browse files- static/js/player.js +100 -0
static/js/player.js
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
let player;
|
2 |
+
let currentPlaylist = [];
|
3 |
+
let currentTrackIndex = 0;
|
4 |
+
|
5 |
+
function onYouTubeIframeAPIReady() {
|
6 |
+
player = new YT.Player('player', {
|
7 |
+
height: '0',
|
8 |
+
width: '0',
|
9 |
+
playerVars: {
|
10 |
+
'playsinline': 1,
|
11 |
+
'controls': 0
|
12 |
+
},
|
13 |
+
events: {
|
14 |
+
'onStateChange': onPlayerStateChange
|
15 |
+
}
|
16 |
+
});
|
17 |
+
}
|
18 |
+
|
19 |
+
function onPlayerStateChange(event) {
|
20 |
+
if (event.data === YT.PlayerState.ENDED) {
|
21 |
+
nextTrack();
|
22 |
+
}
|
23 |
+
updatePlayPauseButton();
|
24 |
+
}
|
25 |
+
|
26 |
+
function updatePlayPauseButton() {
|
27 |
+
const btn = document.getElementById('playPauseBtn');
|
28 |
+
if (player && player.getPlayerState() === YT.PlayerState.PLAYING) {
|
29 |
+
btn.textContent = 'Pause';
|
30 |
+
} else {
|
31 |
+
btn.textContent = 'Play';
|
32 |
+
}
|
33 |
+
}
|
34 |
+
|
35 |
+
function search() {
|
36 |
+
const query = document.getElementById('searchInput').value;
|
37 |
+
fetch('/search', {
|
38 |
+
method: 'POST',
|
39 |
+
headers: {
|
40 |
+
'Content-Type': 'application/json',
|
41 |
+
},
|
42 |
+
body: JSON.stringify({ query: query })
|
43 |
+
})
|
44 |
+
.then(response => response.json())
|
45 |
+
.then(data => {
|
46 |
+
displaySearchResults(data);
|
47 |
+
})
|
48 |
+
.catch(error => console.error('Error:', error));
|
49 |
+
}
|
50 |
+
|
51 |
+
function displaySearchResults(results) {
|
52 |
+
const container = document.getElementById('searchResults');
|
53 |
+
container.innerHTML = '';
|
54 |
+
|
55 |
+
results.forEach((song, index) => {
|
56 |
+
const div = document.createElement('div');
|
57 |
+
div.className = 'song-item';
|
58 |
+
div.innerHTML = `
|
59 |
+
<strong>${song.title}</strong> - ${song.artists[0].name}
|
60 |
+
`;
|
61 |
+
div.onclick = () => playSong(song, index, results);
|
62 |
+
container.appendChild(div);
|
63 |
+
});
|
64 |
+
}
|
65 |
+
|
66 |
+
function playSong(song, index, playlist) {
|
67 |
+
currentPlaylist = playlist;
|
68 |
+
currentTrackIndex = index;
|
69 |
+
|
70 |
+
const videoId = song.videoId;
|
71 |
+
document.getElementById('currentSongTitle').textContent = `${song.title} - ${song.artists[0].name}`;
|
72 |
+
document.getElementById('player-container').classList.remove('d-none');
|
73 |
+
|
74 |
+
player.loadVideoById(videoId);
|
75 |
+
player.playVideo();
|
76 |
+
}
|
77 |
+
|
78 |
+
function togglePlayPause() {
|
79 |
+
if (player) {
|
80 |
+
if (player.getPlayerState() === YT.PlayerState.PLAYING) {
|
81 |
+
player.pauseVideo();
|
82 |
+
} else {
|
83 |
+
player.playVideo();
|
84 |
+
}
|
85 |
+
}
|
86 |
+
}
|
87 |
+
|
88 |
+
function nextTrack() {
|
89 |
+
if (currentPlaylist.length > 0) {
|
90 |
+
currentTrackIndex = (currentTrackIndex + 1) % currentPlaylist.length;
|
91 |
+
playSong(currentPlaylist[currentTrackIndex], currentTrackIndex, currentPlaylist);
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
function previousTrack() {
|
96 |
+
if (currentPlaylist.length > 0) {
|
97 |
+
currentTrackIndex = (currentTrackIndex - 1 + currentPlaylist.length) % currentPlaylist.length;
|
98 |
+
playSong(currentPlaylist[currentTrackIndex], currentTrackIndex, currentPlaylist);
|
99 |
+
}
|
100 |
+
}
|