react-player icon indicating copy to clipboard operation
react-player copied to clipboard

Error changing video source when in StrinctMode/DevMode and using Hls

Open rostalevicek opened this issue 2 years ago • 2 comments

Current Behavior

While in StrinctMode/DevMode, React mounts component twice. ReactPlayer prevents initialization of player and loading sources when player is already instantiated:

Player.js:

handlePlayerMount = player => {
    **if (this.player) {
      this.progress() // Ensure onProgress is still called in strict mode
      return // Return here to prevent loading twice in strict mode
    }**
    this.player = player
    this.player.load(this.props.url)
    this.progress()
}

However, unmouting of the first component will destroy hls and its source loader: FilePlayer.js

componentWillUnmount () {
    this.player.src = ''
    this.removeListeners(this.player)
    **if (this.hls) {
      this.hls.destroy()
    }**
}

Therefore the new source is not loaded and played.

Expected Behavior

The new source should be loaded and played.

Other Information

Simple commenting of the condition Player.js, handlePlayerMount, solves this problem.

rostalevicek avatar Feb 06 '23 08:02 rostalevicek

Seems I have the same problem, when changing the URL without unmount everything works ok - as in the demo. The file is loaded normally. But when the unmount method is called, the new resource is not loaded after that. But for me preventing the rerender with a useEffect helped me in this situation.

This works for me:

export const MyReactPlayer = ({ url }) => {
  const [videoUrl, setVideoUrl] = useState('');

  useEffect(() => { setVideoUrl(url) }, [url])

  return (
    <ReactPlayer
        url={videoUrl}
        config={{
          hlsOptions: {
            enableWorker: false,
            manifestLoadingMaxRetry: 3,
            maxPlaylistRetries: 5
          }
        }}
      />
  );
};

ghost-vk avatar Feb 18 '23 09:02 ghost-vk

Current Behavior

While in StrinctMode/DevMode, React mounts component twice. ReactPlayer prevents initialization of player and loading sources when player is already instantiated:

Player.js:

handlePlayerMount = player => {
    **if (this.player) {
      this.progress() // Ensure onProgress is still called in strict mode
      return // Return here to prevent loading twice in strict mode
    }**
    this.player = player
    this.player.load(this.props.url)
    this.progress()
}

However, unmouting of the first component will destroy hls and its source loader: FilePlayer.js

componentWillUnmount () {
    this.player.src = ''
    this.removeListeners(this.player)
    **if (this.hls) {
      this.hls.destroy()
    }**
}

Therefore the new source is not loaded and played.

Expected Behavior

The new source should be loaded and played.

Other Information

Simple commenting of the condition Player.js, handlePlayerMount, solves this problem.

Hello, I am also faced with the same issue now. In detail, I am building a simple one-page react app that shows multiple camera players in a grid layout, which a user selects in a camera list. In this case, it can not avoid the unmount of react player in strict mode. I hope you fix this issue as soon as possible. Thanks and regards.

sieman216485 avatar Sep 22 '23 16:09 sieman216485