This commit is contained in:
parent
a9e3cf2ea5
commit
48f62c8c02
6 changed files with 107 additions and 5 deletions
|
|
@ -99,6 +99,18 @@
|
|||
playerReady = true;
|
||||
dispatch('ready');
|
||||
|
||||
// Try to report duration early if available (some videos have it on ready)
|
||||
const storeState = $watchSync;
|
||||
const playlistItemId = storeState.currentVideo?.id;
|
||||
if (playlistItemId && playlistItemId !== durationReportedForItemId) {
|
||||
const playerDuration = player.getDuration();
|
||||
if (playerDuration > 0) {
|
||||
console.log(`Reporting duration on ready for playlist item ${playlistItemId}: ${Math.floor(playerDuration)}s`);
|
||||
watchSync.reportDuration(playlistItemId, playerDuration);
|
||||
durationReportedForItemId = playlistItemId;
|
||||
}
|
||||
}
|
||||
|
||||
// Start sync check interval
|
||||
if (syncCheckInterval) clearInterval(syncCheckInterval);
|
||||
syncCheckInterval = setInterval(checkAndSync, SYNC_CHECK_INTERVAL);
|
||||
|
|
@ -111,6 +123,8 @@
|
|||
// Always handle ENDED state - crucial for playlist advancement
|
||||
if (state === window.YT.PlayerState.ENDED) {
|
||||
dispatch('ended');
|
||||
// Also notify server directly - this is a fallback for when duration-based detection fails
|
||||
watchSync.videoEnded();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -348,6 +348,11 @@ function createWatchSyncStore() {
|
|||
send({ type: 'skip' });
|
||||
}
|
||||
|
||||
// Notify server that video has ended (fallback for when duration is unknown)
|
||||
function videoEnded() {
|
||||
send({ type: 'video_ended' });
|
||||
}
|
||||
|
||||
// Report video duration to server (called when YouTube player loads a video)
|
||||
function reportDuration(playlistItemId, duration) {
|
||||
if (!playlistItemId || !duration || duration <= 0) return;
|
||||
|
|
@ -389,6 +394,7 @@ function createWatchSyncStore() {
|
|||
pause,
|
||||
seek,
|
||||
skip,
|
||||
videoEnded,
|
||||
requestSync,
|
||||
checkSync,
|
||||
reportDuration,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
import { browser } from '$app/environment';
|
||||
import { auth, userColor } from '$lib/stores/auth';
|
||||
import { siteSettings } from '$lib/stores/siteSettings';
|
||||
import { watchSync, viewerCount, canControl, canAddToPlaylist, currentVideo } from '$lib/stores/watchSync';
|
||||
import { watchSync, viewerCount, canControl, canAddToPlaylist, currentVideo, watchConnected } from '$lib/stores/watchSync';
|
||||
import { chatLayout } from '$lib/stores/chatLayout';
|
||||
import ChatPanel from '$lib/components/chat/ChatPanel.svelte';
|
||||
import YouTubePlayer from '$lib/components/watch/YouTubePlayer.svelte';
|
||||
|
|
@ -191,10 +191,22 @@
|
|||
// Prevent duplicate skip calls
|
||||
if (skipInProgress) return;
|
||||
|
||||
console.log('handleVideoEnded called, canControl:', $canControl, 'connected:', $watchConnected);
|
||||
|
||||
// When a video ends, call skip to advance to next (or restart if locked)
|
||||
// The server handles locked videos by restarting them instead of advancing
|
||||
// Note: YouTubePlayer also calls watchSync.videoEnded() directly as a fallback
|
||||
if ($canControl) {
|
||||
// Check if WebSocket is connected
|
||||
if (!$watchConnected) {
|
||||
console.warn('WebSocket disconnected, cannot send skip');
|
||||
// Try to refresh playlist anyway
|
||||
loadPlaylist();
|
||||
return;
|
||||
}
|
||||
|
||||
skipInProgress = true;
|
||||
console.log('Sending skip command to server');
|
||||
watchSync.skip();
|
||||
// Reset flag after a delay and refresh playlist
|
||||
setTimeout(() => {
|
||||
|
|
@ -203,7 +215,7 @@
|
|||
}, 2000);
|
||||
} else {
|
||||
// Non-controllers just request sync to get the updated state
|
||||
// (the server auto-advances after a short delay or owner skips)
|
||||
// (the server auto-advances via the videoEnded fallback)
|
||||
setTimeout(() => {
|
||||
watchSync.requestSync();
|
||||
loadPlaylist();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue