@@ -57,6 +57,10 @@ export const MP4VideoPlayer = memo(
57
57
const video = videoRef . current ;
58
58
if ( ! video ) return ;
59
59
60
+ // Store the current position before reloading
61
+ const currentPosition = video . currentTime ;
62
+ const wasPlaying = ! video . paused ;
63
+
60
64
// Get a fresh URL from the API
61
65
const newUrl = await fetchNewUrl ( ) ;
62
66
@@ -69,6 +73,20 @@ export const MP4VideoPlayer = memo(
69
73
// Reset video and reload with new source
70
74
video . load ( ) ;
71
75
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
+
72
90
// Update the last attempt time
73
91
lastAttemptTime . current = Date . now ( ) ;
74
92
} , [ fetchNewUrl ] ) ;
@@ -135,6 +153,10 @@ export const MP4VideoPlayer = memo(
135
153
const handleLoadedData = ( ) => {
136
154
console . log ( "Video loaded successfully" ) ;
137
155
setIsLoaded ( true ) ;
156
+ // Dispatch canplay event to notify parent component
157
+ if ( videoRef . current ) {
158
+ videoRef . current . dispatchEvent ( new Event ( "canplay" ) ) ;
159
+ }
138
160
// Clear any retry timeouts if video is loaded
139
161
if ( retryTimeout . current ) {
140
162
clearTimeout ( retryTimeout . current ) ;
@@ -143,8 +165,8 @@ export const MP4VideoPlayer = memo(
143
165
} ;
144
166
145
167
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
148
170
} ;
149
171
150
172
const handleError = ( e : ErrorEvent ) => {
0 commit comments