Skip to content

Commit

Permalink
fix(client): muting / Unmuting the player won't trigger a freeze anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Moss committed Jul 30, 2024
1 parent 25cd651 commit 5507870
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
25 changes: 19 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ const App = () => {
const [loading, setLoading] = useState(false);
const [hasReachedRemoteServer, setHasReachedRemoteServer] = useState(false);

// Member - Determines whether the audio is currently muted
const [muted, setMuted] = useState(true);
const toggleMute = () => {
const _newMuted = !muted;

const currentVideo = document.querySelector(`video[data-index="${currentVideoIndex}"]`);
currentVideo.muted = _newMuted;

setMuted(!muted);
};

// Member - Saves a { url , title } dictionary for every video discovered
const [videos, setVideos] = useState([]);
const [currentVideoIndex, setCurrentVideoIndex] = useState(0);
Expand All @@ -128,10 +139,6 @@ const App = () => {
[videos, blackListUpdater] // eslint-disable-line react-hooks/exhaustive-deps
);

// Member - Determines whether the audio is currently muted
const [muted, setMuted] = useState(true);
const toggleMute = () => setMuted(!muted);

// Video control - Download the current video
const download = () => {
const url = visibleVideos[currentVideoIndex].url;
Expand Down Expand Up @@ -298,6 +305,13 @@ const App = () => {
});
};

// Hook - When a new video is focused, update its muted state
useEffect(() => {
const currentVideo = document.querySelector(`video[data-index="${currentVideoIndex}"]`);
if (!currentVideo) return;
currentVideo.muted = muted;
}, [currentVideoIndex]); // eslint-disable-line react-hooks/exhaustive-deps

// Memoized component - Video Feed
const Feed = useMemo(
() => {
Expand All @@ -316,12 +330,11 @@ const App = () => {
}
jumpBackForward={previousVideoIndex !== visibleVideos.length && currentVideoIndex > 1}
videos={visibleVideos}
isMuted={muted}
onFocusVideo={handleVideoFocus}
/>
);
},
[muted, visibleVideos] // eslint-disable-line react-hooks/exhaustive-deps
[visibleVideos] // eslint-disable-line react-hooks/exhaustive-deps
);

// Hook - On mount - Retrieve the locally-stored secret / Attempt to hide the address bar / Retrieve the cache if any
Expand Down
8 changes: 4 additions & 4 deletions src/components/VideoCard/index.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Dependencies
import { useCallback, useMemo, useRef } from "react";
import { useCallback, useRef } from "react";

// Assets
import "./index.css";

const VideoCard = ({ index, url, isLoaded, isMuted, refForwarder }) => {
const VideoCard = ({ index, url, isLoaded, refForwarder }) => {
const videoRef = useRef(null);

// Toggle play/pause on click
Expand All @@ -16,7 +16,7 @@ const VideoCard = ({ index, url, isLoaded, isMuted, refForwarder }) => {
const forward = useCallback((_ref) => {
videoRef.current = _ref;
refForwarder(_ref);
}, []);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

return (
<div className="video">
Expand All @@ -26,9 +26,9 @@ const VideoCard = ({ index, url, isLoaded, isMuted, refForwarder }) => {
src={isLoaded ? url : null}
ref={forward}
onClick={togglePause}
muted={isMuted}
loop={true}
playsInline={true}
muted={true}
preload="auto"
/>
</div>
Expand Down
3 changes: 1 addition & 2 deletions src/components/VideoFeed/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useRef } from "react";
import VideoCard from "../VideoCard";
import "./index.css";

const VideoFeed = ({ videos, initialIndex, jumpToEnd, jumpBackForward, isMuted, onFocusVideo }) => {
const VideoFeed = ({ videos, initialIndex, jumpToEnd, jumpBackForward, onFocusVideo }) => {
// Member - Track the currently-visible video (0-indexed)
const currentVideoIndex = useRef(0);

Expand Down Expand Up @@ -146,7 +146,6 @@ const VideoFeed = ({ videos, initialIndex, jumpToEnd, jumpBackForward, isMuted,
index={k + initialIndex}
url={videos[k + initialIndex] ? videos[k + initialIndex].url : ""}
isLoaded={true}
isMuted={isMuted}
refForwarder={saveVideoRef(k)}
/>
))}
Expand Down

0 comments on commit 5507870

Please sign in to comment.