-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioControls.js
More file actions
373 lines (341 loc) · 14.1 KB
/
Copy pathaudioControls.js
File metadata and controls
373 lines (341 loc) · 14.1 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
(function registerAudioControls(global) {
if (!global) return;
const root = global.VideoTagger = global.VideoTagger || {};
const player = root.player = root.player || {};
const constants = player.constants = player.constants || {};
const state = player.state = player.state || {};
const MEDIA_MODE = constants.MEDIA_MODE || { AUDIO: 'audio', VIDEO: 'video' };
const CONTROL_LOG_DELTA = constants.CONTROL_LOG_DELTA ?? 0.75;
function formatMediaTime(seconds) {
if (!Number.isFinite(seconds) || seconds < 0) return '00:00';
const minutes = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
}
function getActiveMediaApi(videoElement) {
if (global.ytPlayer && typeof global.ytPlayer.getDuration === 'function') {
return {
type: 'youtube',
getDuration: () => Number(global.ytPlayer.getDuration?.()) || 0,
getCurrentTime: () => Number(global.ytPlayer.getCurrentTime?.()) || 0,
play: () => global.ytPlayer.playVideo?.(),
pause: () => global.ytPlayer.pauseVideo?.(),
seek: (seconds) => global.ytPlayer.seekTo?.(seconds, true),
getVolume: () => {
const vol = global.ytPlayer.getVolume?.();
return Number.isFinite(vol) ? vol : 100;
},
setVolume: (value) => global.ytPlayer.setVolume?.(Math.max(0, Math.min(100, value))),
isPlaying: () => {
if (!global.YT || !global.YT.PlayerState) return false;
return global.ytPlayer.getPlayerState?.() === global.YT.PlayerState.PLAYING;
}
};
}
if (global.plyrInstance) {
return {
type: 'plyr',
getDuration: () => Number(global.plyrInstance.duration) || 0,
getCurrentTime: () => Number(global.plyrInstance.currentTime) || 0,
play: () => global.plyrInstance.play(),
pause: () => global.plyrInstance.pause(),
seek: (seconds) => { global.plyrInstance.currentTime = seconds; },
getVolume: () => Number.isFinite(global.plyrInstance.volume) ? global.plyrInstance.volume * 100 : 100,
setVolume: (value) => { global.plyrInstance.volume = Math.max(0, Math.min(100, value)) / 100; },
isPlaying: () => !global.plyrInstance.paused
};
}
if (videoElement && videoElement.readyState >= 1) {
return {
type: 'html5',
getDuration: () => Number(videoElement.duration) || 0,
getCurrentTime: () => Number(videoElement.currentTime) || 0,
play: () => videoElement.play(),
pause: () => videoElement.pause(),
seek: (seconds) => { videoElement.currentTime = seconds; },
getVolume: () => Number.isFinite(videoElement.volume) ? videoElement.volume * 100 : 100,
setVolume: (value) => { videoElement.volume = Math.max(0, Math.min(100, value)) / 100; },
isPlaying: () => !videoElement.paused
};
}
return null;
}
function updateAudioControls(trigger = 'auto') {
const {
video,
audioToggleBtn,
audioStatus,
audioProgress,
audioControlBar,
audioVolume,
audioBanner
} = player.getMediaElements();
if (!audioToggleBtn || !audioStatus) {
console.warn('[video.js] updateAudioControls missing controls', {
trigger,
hasToggle: !!audioToggleBtn,
hasStatus: !!audioStatus
});
return;
}
if (audioControlBar) {
audioControlBar.hidden = false;
audioControlBar.removeAttribute('hidden');
audioControlBar.style.visibility = 'visible';
audioControlBar.style.opacity = '1';
audioControlBar.style.display = 'flex';
}
if (audioBanner) {
audioBanner.style.display = global.mediaMode === MEDIA_MODE.AUDIO ? 'flex' : 'none';
}
const sourceType = global.ytPlayer
? 'youtube'
: (global.plyrInstance ? 'plyr' : (video ? 'html5' : 'none'));
const ytPlayer = global.ytPlayer || null;
const ytFunctionsAvailable = ytPlayer
&& typeof global.ytPlayer.getPlayerState === 'function'
&& typeof global.ytPlayer.getCurrentTime === 'function';
const ytDurationAvailable = ytPlayer
&& typeof global.ytPlayer.getDuration === 'function';
const isYouTube = !!ytPlayer;
const hasSource = isYouTube
? !!ytPlayer
: !!(video && (video.currentSrc || video.src));
audioToggleBtn.disabled = !hasSource;
if (audioVolume) {
if (!hasSource) {
audioVolume.disabled = true;
audioVolume.value = 100;
}
}
if (!hasSource || (!video && !isYouTube)) {
const awaitingYouTube = (typeof document !== 'undefined' && document.body?.classList?.contains('youtube-mode')) || !!global.pendingYouTubeLoad;
const details = {
trigger,
sourceType,
ytPlayerAvailable: !!global.ytPlayer,
ytReadyState: global.ytPlayer ? player.safeYouTubeCall(global.ytPlayer, 'getPlayerState') : null,
html5HasVideo: !!video,
html5CurrentSrc: video?.currentSrc || null,
plyrActive: !!global.plyrInstance,
note: awaitingYouTube
? 'Audio controls waiting for YouTube player to finish initialising.'
: 'Audio controls disabled because no playable media source is available.'
};
const iconSpan = audioToggleBtn.querySelector('.material-symbols-outlined');
if (iconSpan) {
iconSpan.textContent = 'play_arrow';
} else {
audioToggleBtn.textContent = 'Play';
}
audioToggleBtn.setAttribute('aria-label', 'Play audio');
audioStatus.textContent = awaitingYouTube ? 'Loading…' : '00:00 / 00:00';
if (audioProgress) {
if (!audioProgress.dataset.scrubbing) audioProgress.dataset.scrubbing = 'false';
audioProgress.value = 0;
audioProgress.max = 0;
}
if (audioVolume) {
audioVolume.disabled = true;
audioVolume.value = 100;
}
if (!hasSource) {
if (awaitingYouTube) {
console.info('[video.js] updateAudioControls awaiting YouTube source', details);
} else {
console.debug('[video.js] updateAudioControls skipped: no media source', details);
}
if (trigger !== 'auto') {
player.logPlayerLayout(`updateAudioControls:noSource:${trigger}`);
}
}
state.lastAudioControlSnapshot = null;
return;
}
let isPlaying = false;
let current = 0;
let rawDuration = 0;
if (isYouTube) {
const playerState = ytFunctionsAvailable && global.ytPlayer.getPlayerState
? global.ytPlayer.getPlayerState()
: undefined;
const ytStates = (typeof global.YT !== 'undefined' && global.YT.PlayerState) ? global.YT.PlayerState : null;
const playingValue = ytStates?.PLAYING ?? 1;
if (typeof playerState === 'number') {
isPlaying = playerState === playingValue;
} else if (playerState && typeof playerState === 'object' && 'label' in playerState) {
isPlaying = playerState.label === 'playing';
}
if (ytFunctionsAvailable && typeof global.ytPlayer.getCurrentTime === 'function') {
current = global.ytPlayer.getCurrentTime();
}
if (ytDurationAvailable) {
rawDuration = global.ytPlayer.getDuration();
}
} else if (global.plyrInstance) {
isPlaying = !global.plyrInstance.paused;
current = Number.isFinite(global.plyrInstance.currentTime)
? global.plyrInstance.currentTime
: (video?.currentTime || 0);
rawDuration = Number.isFinite(global.plyrInstance.duration)
? global.plyrInstance.duration
: (Number.isFinite(video?.duration) ? video.duration : 0);
} else if (video) {
isPlaying = !video.paused && !video.ended;
current = Number.isFinite(video.currentTime) ? video.currentTime : 0;
rawDuration = Number.isFinite(video.duration) && video.duration > 0 ? video.duration : 0;
}
const duration = Number.isFinite(rawDuration) && rawDuration > 0 ? rawDuration : 0;
// Update button text and icon
const iconSpan = audioToggleBtn.querySelector('.material-symbols-outlined');
if (iconSpan) {
iconSpan.textContent = isPlaying ? 'pause' : 'play_arrow';
} else {
audioToggleBtn.textContent = isPlaying ? 'Pause' : 'Play';
}
audioToggleBtn.setAttribute('aria-label', isPlaying ? 'Pause audio' : 'Play audio');
audioStatus.textContent = `${formatMediaTime(current)} / ${formatMediaTime(duration || 0)}`;
if (audioProgress) {
if (!audioProgress.dataset.scrubbing) {
audioProgress.dataset.scrubbing = 'false';
}
const scrubbing = audioProgress.dataset.scrubbing === 'true';
const safeDuration = duration > 0 ? duration : 0;
if (audioProgress.max != safeDuration) audioProgress.max = safeDuration;
if (!scrubbing) {
const clamped = safeDuration > 0
? Math.min(Math.max(current, 0), safeDuration)
: Math.max(current, 0);
audioProgress.value = clamped;
}
}
if (audioVolume) {
const api = getActiveMediaApi(video);
if (api && typeof api.getVolume === 'function') {
audioVolume.disabled = false;
audioVolume.value = Math.round(api.getVolume());
} else {
audioVolume.disabled = true;
audioVolume.value = 100;
}
}
const snapshot = {
trigger,
sourceType,
isPlaying,
current,
duration,
disabled: audioToggleBtn.disabled
};
if (
!state.lastAudioControlSnapshot
|| state.lastAudioControlSnapshot.sourceType !== snapshot.sourceType
|| state.lastAudioControlSnapshot.isPlaying !== snapshot.isPlaying
|| state.lastAudioControlSnapshot.disabled !== snapshot.disabled
|| Math.abs((state.lastAudioControlSnapshot.duration || 0) - snapshot.duration) > CONTROL_LOG_DELTA
|| Math.abs((state.lastAudioControlSnapshot.current || 0) - snapshot.current) > (isPlaying ? CONTROL_LOG_DELTA * 4 : CONTROL_LOG_DELTA)
) {
console.debug('[video.js] controls:update', snapshot);
state.lastAudioControlSnapshot = { ...snapshot };
} else {
state.lastAudioControlSnapshot = { ...state.lastAudioControlSnapshot, ...snapshot };
}
}
function applyMediaMode() {
const { player: playerRoot, placeholder, youtubeContainer, audioControlBar, audioBanner, audioVolume } = player.getMediaElements();
if (!playerRoot) return;
const mode = global.mediaMode === MEDIA_MODE.VIDEO ? MEDIA_MODE.VIDEO : MEDIA_MODE.AUDIO;
playerRoot.classList.remove('audio-mode', 'video-mode');
playerRoot.classList.add(`${mode}-mode`);
if (global.document && global.document.body) {
global.document.body.classList.toggle('audio-mode-active', mode === MEDIA_MODE.AUDIO);
global.document.body.classList.toggle('video-mode-active', mode === MEDIA_MODE.VIDEO);
}
if (placeholder) {
placeholder.setAttribute('aria-hidden', mode === MEDIA_MODE.VIDEO ? 'true' : 'false');
const hasSource = global.ytPlayer
|| (global.plyrInstance && global.plyrInstance.media?.currentSrc)
|| document.getElementById('video')?.currentSrc;
const audioActive = mode === MEDIA_MODE.AUDIO && hasSource;
placeholder.textContent = audioActive ? 'Audio playback active' : 'No Video';
if (audioActive) {
placeholder.dataset.placeholderNote = 'Audio mode active';
} else {
placeholder.removeAttribute('data-placeholder-note');
}
}
if (audioControlBar) {
audioControlBar.hidden = mode !== MEDIA_MODE.AUDIO;
if (mode === MEDIA_MODE.AUDIO) {
audioControlBar.removeAttribute('hidden');
audioControlBar.style.display = 'flex';
audioControlBar.style.visibility = 'visible';
audioControlBar.style.opacity = '1';
}
}
if (audioBanner) {
audioBanner.style.display = mode === MEDIA_MODE.AUDIO ? 'flex' : 'none';
}
if (audioVolume && mode !== MEDIA_MODE.AUDIO) {
audioVolume.disabled = true;
}
if (global.plyrInstance && global.plyrInstance.elements?.container) {
global.plyrInstance.elements.container.classList.toggle('plyr--audio-mode', mode === MEDIA_MODE.AUDIO);
}
if (youtubeContainer && global.ytPlayer) {
if (mode === MEDIA_MODE.AUDIO) {
youtubeContainer.style.opacity = '0.001';
youtubeContainer.style.pointerEvents = 'none';
youtubeContainer.style.height = '';
youtubeContainer.style.display = '';
} else {
youtubeContainer.style.opacity = '';
youtubeContainer.style.pointerEvents = '';
youtubeContainer.style.height = '';
youtubeContainer.style.display = '';
}
} else if (youtubeContainer) {
youtubeContainer.style.opacity = '';
youtubeContainer.style.pointerEvents = '';
youtubeContainer.style.height = '';
youtubeContainer.style.display = '';
}
// Handle HTML5 video element visibility in audio mode
const html5Video = document.getElementById('video');
const html5Wrapper = document.getElementById('html5-wrapper');
if (html5Video && html5Video.src) {
if (mode === MEDIA_MODE.AUDIO) {
html5Video.style.opacity = '0.001';
html5Video.style.pointerEvents = 'none';
if (html5Wrapper) {
html5Wrapper.style.opacity = '0.001';
html5Wrapper.style.pointerEvents = 'none';
}
} else {
html5Video.style.opacity = '';
html5Video.style.pointerEvents = '';
if (html5Wrapper) {
html5Wrapper.style.opacity = '';
html5Wrapper.style.pointerEvents = '';
}
}
}
if (mode === MEDIA_MODE.AUDIO) {
updateAudioControls();
}
if (typeof global.updateAudioModeToggle === 'function') {
try {
global.updateAudioModeToggle();
} catch (err) {
console.debug('[video.js] updateAudioModeToggle failed', err);
}
}
player.logPlayerLayout(`applyMediaMode:${mode}`);
}
player.formatMediaTime = formatMediaTime;
player.getActiveMediaApi = getActiveMediaApi;
player.updateAudioControls = updateAudioControls;
player.applyMediaMode = applyMediaMode;
global.updateAudioControls = updateAudioControls;
global.applyMediaMode = applyMediaMode;
})(typeof window !== 'undefined' ? window : undefined);