New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Video doesn't pause when playing state changes too fast (false->true->false) #1152
Comments
Similar discussion here: #1079 (comment) It has been a while (years) since I looked at the Consider a very simple example using const [playing, setPlaying] = useState(true)
return (
<ReactPlayer
url='file.mp4'
controls
playing={playing}
onPlay={() => setPlaying(true)}
onPause={() => setPlaying(false)}
/>
) Without the
Also, more generally, we don't want to call My suggestion would be to add debounce/throttle logic to your app to ensure that |
I had a similar issue where my use case was that I had to stop the video when it reaches beyond X seconds. So I had to forcefully stop from the internal player like below. I'm using wistia videos. Didn't check with other media. But most of them should have a pause in the internal API. const playerRef = useRef();
useEffect(() => {
const internalPlayer = playerRef?.current ? playerRef.current.getInternalPlayer() : null;
if (!playing && internalPlayer && internalPlayer.state() === 'playing') {
internalPlayer.pause();
}
}, [playing, playerRef?.current]);
return (
<ReactPlayer
ref={playerRef}
playing={playing}
...
/>
); |
Current Behavior
I wrote an Instagram/Facebook like Play/Pause for videos based on video entire visibility. So user will scroll on his page and will have videos that play when in focus ("playing"=true) and pause when goes out of focus ("playing"=false).
Now when i scroll up and down slowly, everything works fine. Video plays when "playing" is true, and pauses when "playing" is false. However when i scroll very quickly top-down and down-top, i notice that even though the "playing" property has been updated to false, some videos continues playing and do not pause.
Expected Behavior
What ever the scroll speed is, the video should play on when it is on focus ("playing"=true) and should stop when it goes out of focus ("playing"=false).
Steps to Reproduce
Investigation
After investigating i found out that react player has a condition to trigger Pause event as below:
Files:
a) react-player/lazy/Player.js
b) react-player/lib/Player.js
Condition
if (prevProps.playing && !playing && this.isPlaying) {
this.player.pause();
}
So it possible that, when the "playing" property changes quickly from (false) to (true) to (false) again, then "this.isPlaying" doesn't have enough time to follow the changes and stays at false because video didn't play yet.
Proposed Fix
if (prevProps.playing && !playing) {
this.player.pause();
}
The text was updated successfully, but these errors were encountered: