Skip to content

Commit fe87d9e

Browse files
committed
fix: loop between marks
1 parent b3695bd commit fe87d9e

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

frontend/src/containers/VideoPlayer/Trackbar.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const Trackbar = ({
7979

8080
let markOutX = width;
8181
if (markOut) {
82-
markOutX = (markOut / duration) * width;
82+
markOutX = (markOut / duration) * width + frameWidth;
8383
ctx.strokeStyle = 'red';
8484
ctx.beginPath();
8585
ctx.moveTo(markOutX, 0);

frontend/src/containers/VideoPlayer/VideoPlayerBody.jsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ const VideoPlayerBody = ({ ...props }) => {
5050
const [durFrames, setDurFrames] = useState(0);
5151
const [markIn, setMarkIn] = useState();
5252
const [markOut, setMarkOut] = useState();
53-
5453
const [isPlaying, setIsPlaying] = useState(false);
54+
5555
const isPlayingRef = useRef(isPlaying);
56+
const durFramesRef = useRef(durFrames);
57+
const markInRef = useRef(markIn);
58+
const markOutRef = useRef(markOut);
59+
5660
const [loop, setLoop] = useState(false);
5761
const [videoDimensions, setVideoDimensions] = useState(DEFAULT_VIDEO_DIMENSIONS);
5862
const [showOverlay, setShowOverlay] = useState(false);
@@ -63,14 +67,20 @@ const VideoPlayerBody = ({ ...props }) => {
6367
props.setPosition(frames2time(posFrames, props.frameRate));
6468
}, [posFrames]);
6569

70+
useEffect(() => {
71+
durFramesRef.current = durFrames;
72+
}, [durFrames]);
73+
6674
// Propagating markIn and markOut to parent component
6775

6876
useEffect(() => {
6977
if (props.setMarkIn) {
7078
props.setMarkIn(markIn);
79+
markInRef.current = markIn;
7180
}
7281
if (props.setMarkOut) {
7382
props.setMarkOut(markOut);
83+
markOutRef.current = markOut;
7484
}
7585
}, [markIn, markOut]);
7686

@@ -120,7 +130,19 @@ const VideoPlayerBody = ({ ...props }) => {
120130
// Position
121131
//
122132
const updatePos = () => {
123-
setPosFrames(time2frames(videoRef.current.currentTime, props.frameRate));
133+
const video = videoRef.current;
134+
const atFrame = time2frames(video.currentTime, props.frameRate);
135+
setPosFrames(atFrame);
136+
if (!isPlayingRef.current) return;
137+
if (!loop) return;
138+
139+
const markOutFrame =
140+
time2frames(markOutRef.current, props.frameRate) || durFramesRef.current - 1;
141+
142+
if (atFrame === markOutFrame) {
143+
videoRef.current.currentTime = markInRef.current || 0;
144+
videoRef.current.play();
145+
}
124146
};
125147

126148
const updatePosMon = () => {
@@ -135,21 +157,20 @@ const VideoPlayerBody = ({ ...props }) => {
135157

136158
useEffect(() => {
137159
updatePosMon();
138-
}, [videoRef, isPlaying, durFrames]);
160+
}, [videoRef, isPlaying, loop]);
139161

140162
const seekToFrame = (frame) => {
141-
// console.log('seekToFrame', frame);
142163
const videoElement = videoRef.current;
143164
if (!videoElement) return;
144-
const newTime = frames2time(frame, props.frameRate);
165+
const correctedFrame = Math.max(0, Math.min(frame, durFramesRef.current - 1));
166+
const newTime = frames2time(correctedFrame, props.frameRate);
145167
videoElement.currentTime = newTime;
146168
};
147169

148170
const onScrubFinished = (atTime) => {
149171
setTimeout(() => {
150172
const fr = time2frames(atTime, props.frameRate);
151-
// console.log('onScrubFinished', fr);
152-
seekToFrame(fr + 1);
173+
seekToFrame(fr);
153174
}, 40);
154175
};
155176

@@ -184,12 +205,7 @@ const VideoPlayerBody = ({ ...props }) => {
184205
};
185206

186207
const handleEnded = () => {
187-
if (!isPlaying) return;
188-
setIsPlaying(loop);
189-
if (loop) {
190-
videoRef.current.currentTime = 0;
191-
videoRef.current.play();
192-
}
208+
// unused
193209
};
194210

195211
const handleProgress = (e) => {

0 commit comments

Comments
 (0)