Skip to content

Commit 8fc1071

Browse files
committed
feat: Improved video loading/playblack on mobile + thumbnail on seek
1 parent c303c52 commit 8fc1071

File tree

3 files changed

+748
-85
lines changed

3 files changed

+748
-85
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export const MP4VideoPlayer = memo(
5757
const video = videoRef.current;
5858
if (!video) return;
5959

60+
// Store the current position before reloading
61+
const currentPosition = video.currentTime;
62+
const wasPlaying = !video.paused;
63+
6064
// Get a fresh URL from the API
6165
const newUrl = await fetchNewUrl();
6266

@@ -69,6 +73,20 @@ export const MP4VideoPlayer = memo(
6973
// Reset video and reload with new source
7074
video.load();
7175

76+
// Restore position and play state after loading
77+
if (currentPosition > 0) {
78+
const restorePosition = () => {
79+
video.currentTime = currentPosition;
80+
if (wasPlaying) {
81+
video
82+
.play()
83+
.catch((err) => console.error("Error resuming playback:", err));
84+
}
85+
video.removeEventListener("canplay", restorePosition);
86+
};
87+
video.addEventListener("canplay", restorePosition);
88+
}
89+
7290
// Update the last attempt time
7391
lastAttemptTime.current = Date.now();
7492
}, [fetchNewUrl]);
@@ -135,6 +153,10 @@ export const MP4VideoPlayer = memo(
135153
const handleLoadedData = () => {
136154
console.log("Video loaded successfully");
137155
setIsLoaded(true);
156+
// Dispatch canplay event to notify parent component
157+
if (videoRef.current) {
158+
videoRef.current.dispatchEvent(new Event("canplay"));
159+
}
138160
// Clear any retry timeouts if video is loaded
139161
if (retryTimeout.current) {
140162
clearTimeout(retryTimeout.current);
@@ -143,8 +165,8 @@ export const MP4VideoPlayer = memo(
143165
};
144166

145167
const handleLoadedMetadata = () => {
146-
// Trigger a canplay event after metadata is loaded
147-
video.dispatchEvent(new Event("canplay"));
168+
// We'll let the loadeddata event handle dispatching canplay
169+
// This ensures we don't trigger the event too early
148170
};
149171

150172
const handleError = (e: ErrorEvent) => {

0 commit comments

Comments
 (0)