Skip to content

Commit

Permalink
fix(client): the autoplay feature is now working consistently across …
Browse files Browse the repository at this point in the history
…browsers and operating systems
  • Loading branch information
Will Moss committed Sep 2, 2024
1 parent c2a5888 commit 478f1dc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/components/VideoCard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const VideoCard = ({ index, url, isLoaded, refForwarder }) => {
src={isLoaded ? url : null}
ref={forward}
onClick={togglePause}
loop={true}
playsInline={true}
muted={true}
preload="auto"
Expand Down
24 changes: 7 additions & 17 deletions src/components/VideoFeed/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ const VideoFeed = ({
// Member - Prevent double jump forward
const hasJumpedForward = useRef(false);

// Mechanism - On video end, call a listener to trigger autoscroll + autoplay if enabled
const throttle = useRef(false);
// Mechanism - On video end, call a listener to trigger autoscroll + autoplay if enabled + progress tracker
const handleVideoTimeUpdate = (e) => {
const progressRate = e.target.currentTime / (e.target.duration % 60);
progressRef.current.style.transform = `scaleX(${progressRate})`;

if (e.target.currentTime === 0 && throttle.current === true) {
throttle.current = false;
onFinishVideo();
setTimeout(() => {
throttle.current = true;
}, 500);
}
};
const replayVideo = (e) => {
e.target.play();
onFinishVideo();
};

// Hook - On mount - Set the current scroll
Expand Down Expand Up @@ -85,12 +80,14 @@ const VideoFeed = ({
videoElement.play().catch((_) => {});
onFocusVideo(videos[currentIndex], currentIndex);
videoElement.addEventListener("timeupdate", handleVideoTimeUpdate, true);
videoElement.addEventListener("ended", replayVideo, true);
progressRef.current.style.transform = `scaleX(0)`;
}
// Case when a video is off-screen or being scrolled in / out of the screen
else {
videoElement.pause();
videoElement.removeEventListener("timeupdate", handleVideoTimeUpdate, true);
videoElement.removeEventListener("ended", replayVideo, true);
}
});

Expand Down Expand Up @@ -166,13 +163,6 @@ const VideoFeed = ({
};
}, [videos]); // eslint-disable-line react-hooks/exhaustive-deps

// Hook - On mount - Set our autoplay flag to ready
useEffect(() => {
setTimeout(() => {
throttle.current = true;
}, 500);
}, []);

initialIndex = _bufferSize === videos.length ? 0 : initialIndex;

return (
Expand Down

0 comments on commit 478f1dc

Please sign in to comment.