-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
136 lines (114 loc) · 3.88 KB
/
app.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
//const apiKey = 'AIzaSyByBbKXWZZgt03HqHWApMGzb3F8DOSckGQ';
let player;
const playlist = ['J2i0cZWCdq4', 'eOu74uBG7qc', 'ijpdruHDJHY', '28KRPhVzCus', '4xDzrJKXOOY', '5yx6BWlEVcY', '7NOSDKb0HlU']; // Add more video IDs
let currentVideoIndex = 0;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
videoId: playlist[currentVideoIndex],
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange,
},
playerVars: {
autoplay: 1,
controls: 0,
mute: 0, // Start muted to comply with autoplay policies
loop: 1,
playlist: playlist.join(','), // Loop the playlist
},
});
}
function onPlayerReady(event) {
event.target.playVideo();
fetchVideoDetails(playlist[currentVideoIndex]);
// Set initial volume
event.target.setVolume(50);
}
function onPlayerStateChange(event) {
// Auto-update title when video changes
if (event.data === YT.PlayerState.PLAYING) {
fetchVideoDetails(player.getVideoData().video_id);
}
}
// Play button event listener
document.getElementById('play').addEventListener('click', () => {
player.unMute();
player.playVideo();
});
// Pause button event listener
document.getElementById('pause').addEventListener('click', () => {
player.pauseVideo();
});
// Next button event listener
document.getElementById('next').addEventListener('click', () => {
currentVideoIndex = (currentVideoIndex + 1) % playlist.length;
loadVideo(currentVideoIndex);
});
document.getElementById('stop').addEventListener('click', () => {
player.stopVideo();
});
// Previous button event listener
document.getElementById('prev').addEventListener('click', () => {
currentVideoIndex = (currentVideoIndex - 1 + playlist.length) % playlist.length;
loadVideo(currentVideoIndex);
});
// Mute button event listener
document.getElementById('mute').addEventListener('click', () => {
player.mute();
});
// Unmute button event listener
document.getElementById('unmute').addEventListener('click', () => {
player.unMute();
});
// Volume control event listener
document.getElementById('volume').addEventListener('input', (e) => {
const volume = e.target.value;
player.setVolume(volume);
});
// Load a new video by index
function loadVideo(index) {
const videoId = playlist[index];
player.loadVideoById(videoId);
fetchVideoDetails(videoId);
}
async function fetchVideoDetails(videoId) {
try {
//const response = await fetch(
// `https://www.googleapis.com/youtube/v3/videos?id=${videoId}&part=snippet&key=${apiKey}`
//);
const response = await fetch(`http://75.181.32.107:3535/api/videos/${videoId}`)
const data = await response.json();
if (data.items.length > 0) {
const title = data.items[0].snippet.title;
// Update both spans with the title text
const scrollingTexts = document.querySelectorAll('.scrolling-text');
scrollingTexts.forEach(span => {
span.innerText = title;
});
// Update scrolling title animation duration
updateScrollingTitle();
} else {
document.querySelectorAll('.scrolling-text').forEach(span => {
span.innerText = 'Unknown Title';
});
}
} catch (error) {
console.error('Error fetching video details:', error);
}
}
function updateScrollingTitle() {
setTimeout(() => {
const titleElement = document.getElementById('video-title');
const containerWidth = document.getElementById('video-titleContainer').offsetWidth;
const textWidth = titleElement.querySelector('.scrolling-text').scrollWidth;
const totalTextWidth = textWidth * 2; // Duplicate the text to create a scrolling effect
const scrollSpeed = 50; // Adjust this value to change the speed (pixels per second)
const duration = totalTextWidth / scrollSpeed;
// Apply the animation duration
titleElement.style.animationDuration = `${duration}s`;
// Restart the animation
titleElement.style.animation = 'none';
titleElement.offsetHeight; // Trigger reflow
titleElement.style.animation = `marquee ${duration}s linear infinite`;
}, 100);
}