-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideofullscreen.js
238 lines (200 loc) · 7.18 KB
/
videofullscreen.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// From https://zzzcode.ai/javascript/code-generator?id=9886521f-5ae3-4040-8bd6-d9b90f6b0265
document.addEventListener('click', function(event) {
const video = event.target.closest('video');
if (video) {
if (video.requestFullscreen) {
video.requestFullscreen();
} else if (video.webkitRequestFullscreen) { // Safari
video.webkitRequestFullscreen();
}
video.play();
video.onended = function() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) { // Safari
document.webkitExitFullscreen();
}
};
}
});
// Code from AI generators.
// The query was:
/*
Write html and javascript that work correctly on WKWebView on iPad that will:
1. Display 4 embedded videos in a 2 by 2 grid.
2. Play a separate preview clip from one of four full videos on the page
3. Rotate which video has a preview playing,
4. When the user clicks on any video, the full video will open in fullscreen and automatically play
5. When the fullscreen video is done, will exit fullscreen
6. ALL of these MUST WORK correctly on iPad with WKWebView.
*/
/*
// From https://zzzcode.ai/javascript/code-generator?id=9886521f-5ae3-4040-8bd6-d9b90f6b0265
// Result: FAIL
// Play Preview Clip: F
// Rotate Clips playing correctly: F
// Fullscreen on click: F
// Auto Play: F
// Close fullscreen on exit: F
const videos = document.querySelectorAll('video');
let currentPreviewIndex = 0;
function playPreview() {
videos[currentPreviewIndex].currentTime = 0;
videos[currentPreviewIndex].play();
currentPreviewIndex = (currentPreviewIndex + 1) % videos.length;
setTimeout(() => {
videos[currentPreviewIndex].pause();
playPreview();
}, 5000); // Change preview every 5 seconds
}
videos.forEach((video) => {
video.addEventListener('click', () => {
video.requestFullscreen();
video.play();
video.onended = () => {
document.exitFullscreen();
};
});
});
playPreview();
// End zzzcode
*/
/*
// From https://www.codeconvert.ai/javascript-code-generator
// Result: FAIL
// Fullscreen on click: Pass
// Auto Play: Pass
// Close fullscreen on exit: Fail
// Note: Video elements need ID of "preview1" etc.
// Also, PREVIEW clips on main page. Full loaded in js
const previews = [
{ preview: document.getElementById('preview1'), full: 'BTS1.mp4' },
{ preview: document.getElementById('preview2'), full: 'BTS2.mp4' },
{ preview: document.getElementById('preview3'), full: 'BTS3.mp4' },
{ preview: document.getElementById('preview4'), full: 'BTS4.mp4' }
];
let currentIndex = 0;
function playNextPreview() {
previews[currentIndex].preview.play();
previews[currentIndex].preview.onended = () => {
currentIndex = (currentIndex + 1) % previews.length;
playNextPreview();
};
}
previews.forEach((video, index) => {
video.preview.addEventListener('click', () => {
const fullVideo = document.createElement('video');
fullVideo.src = video.full;
fullVideo.controls = true;
fullVideo.autoplay = true;
fullVideo.style.position = 'fixed';
fullVideo.style.top = '0';
fullVideo.style.left = '0';
fullVideo.style.width = '100%';
fullVideo.style.height = '100%';
fullVideo.style.zIndex = '1000';
document.body.appendChild(fullVideo);
fullVideo.onended = () => {
document.body.removeChild(fullVideo);
};
});
});
playNextPreview();
//End CodeConvert
*/
/*
// from https://workik.com/javascript-code-generator
// Result: FAIL
// Fullscreen on click: Pass
// Auto Play: Pass
// Close fullscreen on exit: Fail
// Requires ElementID, but best written
// Note: Video elements need ID of "video1" etc.
// Also, PREVIEW clips on main page. Full loaded in js
// script.js
document.addEventListener('DOMContentLoaded', function () {
const videos = [
{ id: 'video1', fullVideo: 'video1-full.mp4' },
{ id: 'video2', fullVideo: 'video2-full.mp4' },
{ id: 'video3', fullVideo: 'video3-full.mp4' },
{ id: 'video4', fullVideo: 'video4-full.mp4' },
];
let currentPreviewIndex = 0;
// Play a preview video
function playPreview(index) {
videos.forEach((video, i) => {
const videoElement = document.getElementById(video.id);
if (i === index) {
videoElement.play();
} else {
videoElement.pause();
videoElement.currentTime = 0; // Reset other videos
}
});
}
// Function to toggle fullscreen
function openFullscreen(elem) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
}
}
// Function to exit fullscreen
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
}
// Play full video on click
videos.forEach(video => {
const videoElement = document.getElementById(video.id);
videoElement.addEventListener('click', function() {
const fullVideo = document.createElement('video');
fullVideo.src = video.fullVideo;
fullVideo.controls = true;
fullVideo.style.position = 'fixed';
fullVideo.style.top = 0;
fullVideo.style.left = 0;
fullVideo.style.width = '100%';
fullVideo.style.height = '100%';
fullVideo.autoplay = true;
fullVideo.addEventListener('ended', function () {
closeFullscreen();
document.body.removeChild(fullVideo);
});
openFullscreen(fullVideo);
document.body.appendChild(fullVideo);
});
});
// Automatically switch preview every 5 seconds
setInterval(() => {
currentPreviewIndex = (currentPreviewIndex + 1) % videos.length;
playPreview(currentPreviewIndex);
}, 5000);
// Start playing the first preview
playPreview(currentPreviewIndex);
});
// End Workik
*/
// from https://www.mymap.ai/playground?mid=vHSswtlge5zqG
// Result: FAIL
// Fullscreen on click: Pass
// Auto Play: Pass
// Close fullscreen on exit: Fail
// UGLY code.
// From https://typli.ai/ai-javascript-code-generator
// Result: FAIL
// Fullscreen on click: Fail
// Auto Play: Fail
// Close fullscreen on exit: Fail