Skip to content

Commit

Permalink
fix: only run playback range update loop while the video is playing
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyanreyer committed Oct 6, 2021
1 parent 2f8ca28 commit 3c10998
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/hooks/useManageVideoPlayback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ export default function useManageVideoPlayback(
videoRef,
]);

// Effect adds starts an update loop if a playback range is set to ensure
// the video stays within the bounds of its playback range
// Effect starts an update loop while the video is playing
// to ensure the video stays within the bounds of its playback range
useEffect(() => {
if (
// If we don't have a playback range set, we don't need to do anything here
Expand Down Expand Up @@ -259,7 +259,7 @@ export default function useManageVideoPlayback(

// If the video is paused, start playing it again (when the video reaches the end
// of the playback range for the first time, most browsers will pause it)
if (shouldPlayVideo && videoElement.paused) {
if (shouldPlayVideo && (videoElement.paused || videoElement.ended)) {
attemptToPlayVideo();
}
} else {
Expand All @@ -276,10 +276,14 @@ export default function useManageVideoPlayback(
videoElement.currentTime = playbackRangeMinTime;
}

animationFrameId = requestAnimationFrame(checkPlaybackRangeTime);
// If the video is playing, keep the update loop going for the next frame
if (shouldPlayVideo) {
animationFrameId = requestAnimationFrame(checkPlaybackRangeTime);
}
};

// Start the animation frame loop
// Run our update loop at least once; if the video is playing,
// it will continue running every frame until the video is paused again
animationFrameId = requestAnimationFrame(checkPlaybackRangeTime);

return () => {
Expand Down
7 changes: 7 additions & 0 deletions tests/cypress/component/playbackRangeEnd.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ describe('Playback works as expected when only playbackRangeEnd is set', () => {
);
cy.get(videoElementSelector).invoke('prop', 'currentTime', 5);

cy.get(videoElementSelector)
.invoke('prop', 'currentTime')
.should('equal', 5);

// Make the video start playing
cy.triggerEventOnPlayer('mouseenter');

cy.log(
'The video should have been set back to the end of the playback range'
);
Expand Down

0 comments on commit 3c10998

Please sign in to comment.