Enhance video progress handling with improved logging and initial time management

This commit is contained in:
Salman Qureshi
2025-08-10 01:59:53 +05:30
parent 01f92ef3fc
commit 1980c45fce
2 changed files with 45 additions and 18 deletions

View File

@@ -78,12 +78,20 @@ const TorrentPageNetflix = () => {
// Load progress only once on mount
const allProgress = progressService.getAllProgress();
console.log('📊 All progress data:', allProgress);
const torrentProgress = {};
Object.values(allProgress).forEach(progress => {
if (progress.torrentHash === torrentHash) {
torrentProgress[progress.fileIndex] = progress;
}
});
console.log('📊 Filtered progress for torrent:', torrentHash, torrentProgress);
// Also test direct progress retrieval for file 0
const directProgress = progressService.getProgress(torrentHash, 0);
console.log('📊 Direct progress for file 0:', directProgress);
setRecentProgress(torrentProgress);
// Only run progress fetching if no video is selected
@@ -147,8 +155,16 @@ const TorrentPageNetflix = () => {
if (selectedVideo) {
const videoKey = `${torrentHash}-${selectedVideo.index}-${selectedVideo.name}`;
// Capture initial time when video is first selected and don't change it
const initialProgress = recentProgress[selectedVideo.index]?.currentTime || 0;
// Try multiple methods to get progress
const progressFromState = recentProgress[selectedVideo.index]?.currentTime || 0;
const progressFromService = progressService.getProgress(torrentHash, selectedVideo.index);
const directServiceTime = progressFromService?.currentTime || 0;
// Use the direct service method as primary
const initialProgress = directServiceTime || progressFromState;
console.log('🎬 Video selected:', selectedVideo.name, 'Resume from:', initialProgress + 's');
return (
<VideoPlayer
@@ -156,13 +172,9 @@ const TorrentPageNetflix = () => {
src={`${config.apiBaseUrl}/api/torrents/${torrentHash}/files/${selectedVideo.index}/stream`}
title={selectedVideo.name}
onClose={() => setSelectedVideo(null)}
onTimeUpdate={(time) => {
// Update progress service without triggering re-renders
progressService.updateProgress(torrentHash, selectedVideo.index, {
currentTime: time,
duration: selectedVideo.duration || 0
});
// Don't call setRecentProgress here to avoid re-rendering
onTimeUpdate={() => {
// The VideoPlayer itself handles saving progress with correct duration
// We don't need to save it here since VideoPlayer saves every 5 seconds
}}
initialTime={initialProgress}
torrentHash={torrentHash}
@@ -229,7 +241,7 @@ const TorrentPageNetflix = () => {
onClick={() => setSelectedVideo(mainVideoFile)}
>
<Play size={20} />
Play
{recentProgress[mainVideoFile.index] ? 'Resume' : 'Play'}
</button>
)}

View File

@@ -62,6 +62,7 @@ const VideoPlayer = ({
const [showResumeDialog, setShowResumeDialog] = useState(false);
const [resumeData, setResumeData] = useState(null);
const [hasShownResumeDialog, setHasShownResumeDialog] = useState(false);
const [hasAppliedInitialTime, setHasAppliedInitialTime] = useState(false);
// Subtitle/CC support
const [availableSubtitles, setAvailableSubtitles] = useState([]);
@@ -386,19 +387,24 @@ const VideoPlayer = ({
const video = videoRef.current;
if (!video) return;
// Set initial time if provided
if (initialTime > 0) {
video.currentTime = initialTime;
}
const handleLoadedMetadata = () => {
setDuration(video.duration);
setIsLoading(false);
// Set initial time after metadata is loaded
if (initialTime > 0 && !hasAppliedInitialTime) {
console.log('⏰ Resuming video at:', initialTime + 's');
video.currentTime = initialTime;
setCurrentTime(initialTime);
setHasAppliedInitialTime(true);
}
// Check for saved progress and show resume dialog
if (torrentHash && fileIndex !== null && !hasShownResumeDialog) {
// Only show dialog if no initialTime was provided (auto-resume)
if (torrentHash && fileIndex !== null && !hasShownResumeDialog && initialTime === 0) {
const resumeInfo = progressService.shouldResumeVideo(torrentHash, fileIndex);
if (resumeInfo) {
console.log('📋 Showing resume dialog for:', resumeInfo);
setResumeData(resumeInfo);
setShowResumeDialog(true);
}
@@ -432,7 +438,16 @@ const VideoPlayer = ({
};
const handleWaiting = () => setIsLoading(true);
const handleCanPlay = () => setIsLoading(false);
const handleCanPlay = () => {
setIsLoading(false);
// Only try setting initial time when the video can play if we haven't done it yet
if (initialTime > 0 && !hasAppliedInitialTime && Math.abs(video.currentTime - initialTime) > 1) {
console.log('🎬 CanPlay: Resuming video at:', initialTime + 's');
video.currentTime = initialTime;
setCurrentTime(initialTime);
setHasAppliedInitialTime(true);
}
};
const handleCanPlayThrough = () => setIsLoading(false);
video.addEventListener('loadedmetadata', handleLoadedMetadata);
@@ -450,7 +465,7 @@ const VideoPlayer = ({
video.removeEventListener('canplay', handleCanPlay);
video.removeEventListener('canplaythrough', handleCanPlayThrough);
};
}, [src, initialTime, onTimeUpdate, onProgress, updateBufferedProgress, torrentHash, fileIndex, title, hasShownResumeDialog]);
}, [src, initialTime, onTimeUpdate, onProgress, updateBufferedProgress, torrentHash, fileIndex, title, hasShownResumeDialog, hasAppliedInitialTime]);
// Optimized play/pause for instant streaming
const togglePlay = async () => {