Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -360,91 +360,95 @@ button.lg.secondary.svelte-1gz44hr span {
|
|
| 360 |
</style>
|
| 361 |
|
| 362 |
<script>
|
| 363 |
-
// Function to initialize controls once elements are found
|
| 364 |
function initializeControls() {
|
| 365 |
const video = document.querySelector('[data-testid="Video-player"]');
|
| 366 |
const waveform = document.getElementById('waveform');
|
| 367 |
|
| 368 |
-
//
|
| 369 |
if (!video || !waveform) {
|
|
|
|
| 370 |
return;
|
| 371 |
}
|
| 372 |
-
|
| 373 |
-
//
|
| 374 |
const audio = waveform.querySelector('div')?.shadowRoot?.querySelector('audio');
|
| 375 |
if (!audio) {
|
|
|
|
| 376 |
return;
|
| 377 |
}
|
| 378 |
|
| 379 |
console.log('Elements found:', { video, audio });
|
| 380 |
-
|
| 381 |
-
// 监听视频播放进度
|
| 382 |
-
video.addEventListener("play", () => {
|
| 383 |
-
if (audio.paused) {
|
| 384 |
-
audio.play(); // 如果音频暂停,开始播放
|
| 385 |
-
}
|
| 386 |
-
});
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
}
|
| 400 |
-
});
|
| 401 |
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
|
|
|
| 407 |
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
}
|
| 413 |
-
});
|
| 414 |
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
}
|
| 421 |
|
| 422 |
-
//
|
| 423 |
const observer = new MutationObserver((mutations) => {
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
//
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
// 可选:如果不需要继续监听,可以断开观察器
|
| 432 |
-
// observer.disconnect();
|
| 433 |
}
|
| 434 |
-
}
|
| 435 |
-
}
|
| 436 |
});
|
| 437 |
|
| 438 |
-
//
|
| 439 |
observer.observe(document.body, {
|
| 440 |
childList: true,
|
| 441 |
subtree: true
|
| 442 |
});
|
| 443 |
|
| 444 |
-
//
|
| 445 |
document.addEventListener('DOMContentLoaded', () => {
|
| 446 |
console.log('DOM Content Loaded');
|
| 447 |
-
initializeControls();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
});
|
| 449 |
|
| 450 |
</script>
|
|
|
|
| 360 |
</style>
|
| 361 |
|
| 362 |
<script>
|
|
|
|
| 363 |
function initializeControls() {
|
| 364 |
const video = document.querySelector('[data-testid="Video-player"]');
|
| 365 |
const waveform = document.getElementById('waveform');
|
| 366 |
|
| 367 |
+
// Ensure the video and waveform elements are available
|
| 368 |
if (!video || !waveform) {
|
| 369 |
+
console.log("Video or waveform element not found.");
|
| 370 |
return;
|
| 371 |
}
|
| 372 |
+
|
| 373 |
+
// Try to get the audio element from the shadow DOM
|
| 374 |
const audio = waveform.querySelector('div')?.shadowRoot?.querySelector('audio');
|
| 375 |
if (!audio) {
|
| 376 |
+
console.log("Audio element not found.");
|
| 377 |
return;
|
| 378 |
}
|
| 379 |
|
| 380 |
console.log('Elements found:', { video, audio });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
+
// Event listener to synchronize the video and audio play states
|
| 383 |
+
video.addEventListener("play", () => {
|
| 384 |
+
if (audio.paused) {
|
| 385 |
+
audio.play(); // If audio is paused, start playing it
|
| 386 |
+
}
|
| 387 |
+
});
|
| 388 |
|
| 389 |
+
audio.addEventListener("play", () => {
|
| 390 |
+
if (video.paused) {
|
| 391 |
+
video.play(); // If video is paused, start playing it
|
| 392 |
+
}
|
| 393 |
+
});
|
|
|
|
| 394 |
|
| 395 |
+
// Sync the play progress between video and audio
|
| 396 |
+
video.addEventListener("timeupdate", () => {
|
| 397 |
+
if (Math.abs(video.currentTime - audio.currentTime) > 0.1) {
|
| 398 |
+
audio.currentTime = video.currentTime; // Sync time if the difference is more than 0.1s
|
| 399 |
+
}
|
| 400 |
+
});
|
| 401 |
|
| 402 |
+
audio.addEventListener("timeupdate", () => {
|
| 403 |
+
if (Math.abs(audio.currentTime - video.currentTime) > 0.1) {
|
| 404 |
+
video.currentTime = audio.currentTime; // Sync time if the difference is more than 0.1s
|
| 405 |
+
}
|
| 406 |
+
});
|
|
|
|
| 407 |
|
| 408 |
+
// Pause both video and audio when one of them is paused
|
| 409 |
+
video.addEventListener("pause", () => {
|
| 410 |
+
if (!audio.paused) {
|
| 411 |
+
audio.pause();
|
| 412 |
+
}
|
| 413 |
+
});
|
| 414 |
+
|
| 415 |
+
audio.addEventListener("pause", () => {
|
| 416 |
+
if (!video.paused) {
|
| 417 |
+
video.pause();
|
| 418 |
+
}
|
| 419 |
+
});
|
| 420 |
}
|
| 421 |
|
| 422 |
+
// Create a MutationObserver to watch for DOM changes
|
| 423 |
const observer = new MutationObserver((mutations) => {
|
| 424 |
+
mutations.forEach((mutation) => {
|
| 425 |
+
mutation.addedNodes.forEach((node) => {
|
| 426 |
+
// Check if the audio element has been added
|
| 427 |
+
if (node.tagName === 'AUDIO') {
|
| 428 |
+
console.log("Audio element detected");
|
| 429 |
+
initializeControls(); // Call initialize when the audio element is found
|
| 430 |
+
observer.disconnect(); // Disconnect the observer once we've found the audio element
|
|
|
|
|
|
|
| 431 |
}
|
| 432 |
+
});
|
| 433 |
+
});
|
| 434 |
});
|
| 435 |
|
| 436 |
+
// Start observing the DOM for new nodes
|
| 437 |
observer.observe(document.body, {
|
| 438 |
childList: true,
|
| 439 |
subtree: true
|
| 440 |
});
|
| 441 |
|
| 442 |
+
// Ensure the initialization happens after DOM is fully loaded
|
| 443 |
document.addEventListener('DOMContentLoaded', () => {
|
| 444 |
console.log('DOM Content Loaded');
|
| 445 |
+
initializeControls(); // Initialize controls once the DOM is loaded
|
| 446 |
+
});
|
| 447 |
+
|
| 448 |
+
// In case the page takes longer to load, use the load event
|
| 449 |
+
window.addEventListener('load', () => {
|
| 450 |
+
console.log('Window loaded');
|
| 451 |
+
initializeControls(); // Ensure everything is loaded before initializing
|
| 452 |
});
|
| 453 |
|
| 454 |
</script>
|