@@ -133,7 +133,9 @@ export const LoopVideo = ({
133133 const pauseVideo = (
134134 reason : Extract <
135135 PlayerStates ,
136- 'PAUSED_BY_USER' | 'PAUSED_BY_INTERSECTION_OBSERVER'
136+ | 'PAUSED_BY_USER'
137+ | 'PAUSED_BY_INTERSECTION_OBSERVER'
138+ | 'PAUSED_BY_BROWSER'
137139 > ,
138140 ) => {
139141 if ( ! vidRef . current ) return ;
@@ -239,6 +241,9 @@ export const LoopVideo = ({
239241 } ;
240242 } , [ uniqueId ] ) ;
241243
244+ /**
245+ * Keeps track of whether the video has been in view or not.
246+ */
242247 useEffect ( ( ) => {
243248 if ( isInView && ! hasBeenInView ) {
244249 /**
@@ -274,7 +279,7 @@ export const LoopVideo = ({
274279 playerState === 'PAUSED_BY_INTERSECTION_OBSERVER' )
275280 ) {
276281 /**
277- * check if the video has not been in view before tracking the play.
282+ * Check if the video has not been in view before tracking the play.
278283 * This is so we only track the first play.
279284 */
280285 if ( ! hasBeenInView ) {
@@ -294,8 +299,8 @@ export const LoopVideo = ({
294299 ] ) ;
295300
296301 /**
297- * Stops playback when the video is scrolled out of view, resumes playbacks
298- * when the video is back in the viewport.
302+ * Stops playback when the video is scrolled out of view.
303+ * Resumes playback when the video is back in the viewport.
299304 */
300305 useEffect ( ( ) => {
301306 if ( ! vidRef . current || ! hasBeenInView ) return ;
@@ -309,7 +314,7 @@ export const LoopVideo = ({
309314 /**
310315 * If a user action paused the video, they have indicated
311316 * that they don't want to watch the video. Therefore, don't
312- * resume the video when it comes back in view
317+ * resume the video when it comes back in view.
313318 */
314319 const isBackInView =
315320 playerState === 'PAUSED_BY_INTERSECTION_OBSERVER' && isInView ;
@@ -326,8 +331,8 @@ export const LoopVideo = ({
326331 useEffect ( ( ) => {
327332 const shouldShowPlayIcon =
328333 playerState === 'PAUSED_BY_USER' ||
329- ( ! isAutoplayAllowed && playerState === 'NOT_STARTED' ) ;
330-
334+ playerState === 'PAUSED_BY_BROWSER' ||
335+ ( playerState === 'NOT_STARTED' && ! isAutoplayAllowed ) ;
331336 setShowPlayIcon ( shouldShowPlayIcon ) ;
332337 } , [ playerState , isAutoplayAllowed ] ) ;
333338
@@ -381,6 +386,22 @@ export const LoopVideo = ({
381386 }
382387 } ;
383388
389+ /**
390+ * If the video was paused and we know that it wasn't paused by the user
391+ * or the intersection observer, we can deduce that it was paused by the
392+ * browser. Therefore we need to apply the pause state to the video.
393+ */
394+ const handlePause = ( ) => {
395+ if (
396+ playerState === 'PAUSED_BY_USER' ||
397+ playerState === 'PAUSED_BY_INTERSECTION_OBSERVER'
398+ ) {
399+ return ;
400+ }
401+
402+ pauseVideo ( 'PAUSED_BY_BROWSER' ) ;
403+ } ;
404+
384405 /**
385406 * If the video could not be loaded due to an error, report to
386407 * Sentry and log in the console.
@@ -471,6 +492,7 @@ export const LoopVideo = ({
471492 handlePlayPauseClick = { handlePlayPauseClick }
472493 handleAudioClick = { handleAudioClick }
473494 handleKeyDown = { handleKeyDown }
495+ handlePause = { handlePause }
474496 onError = { onError }
475497 AudioIcon = { AudioIcon }
476498 preloadPartialData = { preloadPartialData }
0 commit comments