Skip to content
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

React control? #9

Closed
larrywal opened this issue Nov 2, 2020 · 4 comments
Closed

React control? #9

larrywal opened this issue Nov 2, 2020 · 4 comments

Comments

@larrywal
Copy link

larrywal commented Nov 2, 2020

Hi - I want to use this in a react project - have you considered making a react wrapper? Right now I'm hacking it in using Refs but it's messy and buggy.

@zhw2590582
Copy link
Owner

import React, { useEffect, useState } from "react";
import WFPlayer from "wfplayer";

export default () => {
  const [waveform, setWaveform] = useState(null);
  const $waveform = React.createRef();
  const $video = React.createRef();

  useEffect(() => {
    const waveform = new WFPlayer({
      container: $waveform.current,
      mediaElement: $video.current,
    });

    setWaveform(waveform);
  }, []);

  return (
    <div>
      <video src="test.mp4" ref={$video} />
      <div ref={$waveform} />
    </div>
  );
};

@larrywal
Copy link
Author

larrywal commented Nov 3, 2020

Lol - thanks :). This would clean up much of what I had done. Although...

  1. I'm using it for audio and the file path needs to be dynamic, so I'm assuming I'd update the 3rd line (export default) to include a path parameter and pass to video src? And obviously I'd make the video tag an audio tag...

  2. This wrapping doesn't make it obvious how I'd use the rest of the WFPlayer controls - e.g. the events, etc.. seek, setOptions, adding click handlers, etc... Seems like that wouldn't really work unless waveform is exposed externally, and then it gets messy, no?

@zhw2590582
Copy link
Owner

If your file path is dynamic, it is recommended to destroy the waveform and re instantiate it every time the path changes

@larrywal
Copy link
Author

larrywal commented Nov 4, 2020

Thanks - that helps...although...I'm new to React...not sure how I would do that, right? Because React controls the lifecycle of the included component you created above that I would be including into my page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants