Skip to content

Commit 6a0577f

Browse files
committed
Track player duration in video players
1 parent 80dbf8d commit 6a0577f

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function CapVideoPlayer({
138138
const [isMobile, setIsMobile] = useState(false);
139139
const [hasError, setHasError] = useState(false);
140140
const [isRetryingProcessing, setIsRetryingProcessing] = useState(false);
141-
const [duration, setDuration] = useState(fallbackDuration ?? 0);
141+
const [playerDuration, setPlayerDuration] = useState(fallbackDuration ?? 0);
142142
const queryClient = useQueryClient();
143143

144144
useEffect(() => {
@@ -190,7 +190,7 @@ export function CapVideoPlayer({
190190

191191
// Track video duration for comment markers
192192
useEffect(() => {
193-
setDuration(fallbackDuration ?? 0);
193+
setPlayerDuration(fallbackDuration ?? 0);
194194
}, [fallbackDuration]);
195195

196196
useEffect(() => {
@@ -199,12 +199,12 @@ export function CapVideoPlayer({
199199

200200
const handleLoadedMetadata = () => {
201201
if (Number.isFinite(video.duration) && video.duration > 0) {
202-
setDuration(video.duration);
202+
setPlayerDuration(video.duration);
203203
}
204204
};
205205

206206
if (Number.isFinite(video.duration) && video.duration > 0) {
207-
setDuration(video.duration);
207+
setPlayerDuration(video.duration);
208208
}
209209

210210
video.addEventListener("loadedmetadata", handleLoadedMetadata);
@@ -229,10 +229,10 @@ export function CapVideoPlayer({
229229

230230
useEffect(() => {
231231
// Only show markers when we have duration, comments, and video element
232-
if (duration > 0 && comments.length > 0 && videoRef.current) {
232+
if (playerDuration > 0 && comments.length > 0 && videoRef.current) {
233233
setMarkersReady(true);
234234
}
235-
}, [duration, comments.length, videoRef.current]);
235+
}, [playerDuration, comments.length, videoRef.current]);
236236

237237
useEffect(() => {
238238
if (resolvedSrc.data) {
@@ -687,7 +687,7 @@ export function CapVideoPlayer({
687687
);
688688

689689
return filteredComments.map((comment) => {
690-
const position = (Number(comment.timestamp) / duration) * 100;
690+
const position = (Number(comment.timestamp) / playerDuration) * 100;
691691
const containerPadding = 20;
692692
const availableWidth = `calc(100% - ${containerPadding * 2}px)`;
693693
const adjustedPosition = `calc(${containerPadding}px + (${position}% * ${availableWidth} / 100%))`;
@@ -730,7 +730,7 @@ export function CapVideoPlayer({
730730
// enhancedAudioMuted={enhancedAudioMuted}
731731
// setEnhancedAudioMuted={setEnhancedAudioMuted}
732732
/>
733-
<MediaPlayerTime fallbackDuration={fallbackDuration} />
733+
<MediaPlayerTime fallbackDuration={playerDuration} />
734734
</div>
735735
<div className="flex gap-2 items-center">
736736
{!disableCaptions && (

apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export function HLSVideoPlayer({
112112
const [hasPlayedOnce, setHasPlayedOnce] = useState(false);
113113
const [isRetryingProcessing, setIsRetryingProcessing] = useState(false);
114114
const [sourceVersion, setSourceVersion] = useState(0);
115+
const [playerDuration, setPlayerDuration] = useState(fallbackDuration ?? 0);
115116
const queryClient = useQueryClient();
116117
const playbackSrc =
117118
sourceVersion === 0
@@ -124,6 +125,10 @@ export function HLSVideoPlayer({
124125
setSourceVersion((current) => current + 1);
125126
}, []);
126127

128+
useEffect(() => {
129+
setPlayerDuration(fallbackDuration ?? 0);
130+
}, [fallbackDuration]);
131+
127132
useEffect(() => {
128133
const video = videoRef.current;
129134
if (!video) return;
@@ -154,6 +159,12 @@ export function HLSVideoPlayer({
154159
setHasPlayedOnce(true);
155160
};
156161

162+
const handleLoadedMetadata = () => {
163+
if (Number.isFinite(video.duration) && video.duration > 0) {
164+
setPlayerDuration(video.duration);
165+
}
166+
};
167+
157168
const handleError = (e: Event) => {
158169
const error = (e.target as HTMLVideoElement).error;
159170
console.error("HLSVideoPlayer: Video error detected:", {
@@ -165,11 +176,16 @@ export function HLSVideoPlayer({
165176
};
166177

167178
video.addEventListener("loadeddata", handleLoadedData);
179+
video.addEventListener("loadedmetadata", handleLoadedMetadata);
168180
video.addEventListener("canplay", handleCanPlay);
169181
video.addEventListener("load", handleLoad);
170182
video.addEventListener("play", handlePlay);
171183
video.addEventListener("error", handleError);
172184

185+
if (Number.isFinite(video.duration) && video.duration > 0) {
186+
setPlayerDuration(video.duration);
187+
}
188+
173189
if (video.readyState >= 2) {
174190
setVideoLoaded(true);
175191
if (!hasPlayedOnce) {
@@ -179,6 +195,7 @@ export function HLSVideoPlayer({
179195

180196
return () => {
181197
video.removeEventListener("loadeddata", handleLoadedData);
198+
video.removeEventListener("loadedmetadata", handleLoadedMetadata);
182199
video.removeEventListener("canplay", handleCanPlay);
183200
video.removeEventListener("load", handleLoad);
184201
video.removeEventListener("play", handlePlay);
@@ -566,7 +583,7 @@ export function HLSVideoPlayer({
566583
// enhancedAudioMuted={enhancedAudioMuted}
567584
// setEnhancedAudioMuted={setEnhancedAudioMuted}
568585
/>
569-
<MediaPlayerTime fallbackDuration={fallbackDuration} />
586+
<MediaPlayerTime fallbackDuration={playerDuration} />
570587
</div>
571588
<div className="flex gap-2 items-center">
572589
{!disableCaptions && (

0 commit comments

Comments
 (0)