Skip to content

Techniques for preventing multiple instances of ReactPlayer from playing audio simultaneously. #418

@dancrew32

Description

@dancrew32

I recently setup this auto-playing video experiment. It is setup to autoplay videos, muted and loop them ala Instagram. Somehow multiple instances of the same video's audio stream manage to queue up and I am unable to recreate the issue on my personal machine (of course).

Using VisibilitySensor, when the user scrolls the video into view, it mounts the ReactPlayer. When the player scrolls out of view, it sets state to stop playing, mute the audio. When it scrolls back into view, it is supposed to resume play with audio muted.

On certain machines, when loading an mp4 in the player, the audio stream will begin duplicating and it spirals out of control :(

Any chance there can be a core change to ensure multiple audio streams do not play if multiple instances exist?

export class DocumentVideo extends React.Component<VideoPropsType, VideoStateType> {
  state = {
    playing: false,
    muted: true,
    loop: true,
    loaded: false,
    initialized: false,
  };

  onClick = () => {
    this.setState({
      muted: !this.state.muted,
    });
  };

  onReady = () => {
    this.setState({
      loaded: true,
      playing: true,
      loop: true,
    });
  };

  onVisibilityChange = (isVisible: boolean) => {
    if (isVisible) {
      this.setState({
        initialized: isVisible,
        playing: true,
        muted: true,
        loop: true,
      });
    } else {
      this.setState({
        playing: false,
        loop: false,
        muted: true,
      });
    }
  };

  render() {
    return (
      <div className="document-video-container" onClick={this.onClick}>
        <ErrorBoundary
          onError={(error: Object, componentStack: string) => {
            log.error(`Cannot render video: ${error.message}`, {
              url: this.props.url,
              message: error.message,
              componentStack: componentStack,
            });
          }}
        >
          <VisibilitySensor
            delayedCall={!this.state.initialized}
            partialVisibility={!this.state.initialized}
            onChange={this.onVisibilityChange}
          />
          {this.state.initialized && (
            <ReactPlayer
              onReady={this.onReady}
              className={classNames('document-video-player', {
                'document-video-loaded': this.state.loaded,
              })}
              controls
              playing={this.state.playing}
              muted={this.state.muted}
              loop={this.state.loop}
              url={this.props.url}
              width="100%"
              height="100%"
            />
          )}
        </ErrorBoundary>
      </div>
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions