Code Monkey home page Code Monkey logo

Comments (6)

stoicbuddha avatar stoicbuddha commented on August 15, 2024

Find a proper way to expose the player API which is returned by IndigoPlayer.init

You could have a prop called onPlayerDraw or some such that fires after a player is ready and sends back the player along with anything else that is needed, like if (typeof onPlayerDraw === 'function') onPlayerDraw({player}); It would allow you to get the player API to the user if they want it (some may not need it) and allow them to run their own functionality to deal with the player draw (like autoplay checks and the like).

from indigo-player.

stoicbuddha avatar stoicbuddha commented on August 15, 2024

I've had to use the same thing on a React project to notify a parent component that a child component is now hidden.

from indigo-player.

matvp91 avatar matvp91 commented on August 15, 2024

Thought about it too but using a callback on a sync instruction (IndigoPlayer.init is sync) feels a bit counter-intuitive. One thing I'd like to try is using the useRef hook to create a reference to the player in combination with the useImperativeHandle hook to provide strictly the player API in the ref.

Eg:

const player = useRef();

// Access controller

// eg: player.current.play(), player.current.pause(), ...

// Access state
// indigo-player can provide these kind of hooks, eg: `usePlayerState(ref)`
const [playerState, setPlayerState] = useState(null);

useEffect(() => {
  if (player.current) {
    player.current.addListener(Events.STATE_CHANGE, state => setPlayerState(state));
    return () => player.current.removeListener(Events.STATE_CHANGE);
  }
}, [player.current]);

// eg: access state like playerState.playing, playerState.paused, ...

<Player ref={player} config={config} />

I'll have a play with this in the upcoming days.

from indigo-player.

FreddyJD avatar FreddyJD commented on August 15, 2024

Any updates on this idea @matvp91 ?

from indigo-player.

matvp91 avatar matvp91 commented on August 15, 2024

@FreddyJD not at the moment, I've recently merged in #53 which is a first step in a better module structure. Once I've cleared out the other roadblocks (and maintain backwards compatibility), you'll be able to install indigo-player as a module. This would make it a whole lot easier to wrap it around a React component.

Edit: The following code should get you started, simply run yarn add indigo-player (or npm) and you're good to go:

import React from "react";
import ReactDOM from "react-dom";
import indigoPlayer from "indigo-player";
import "indigo-player/lib/indigo-theme.css";

function App() {
  const ref = React.useRef();

  React.useEffect(() => {
    indigoPlayer.init(ref.current, {
      sources: [
        {
          type: "mp4",
          src:
            "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
        },
      ],
    });
  }, []);

  return (
    <div>
      <div ref={ref} />
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));

Although there is no official integration for indigo-player with React yet, the sample above should get anyone going who wants to implement this in React.

from indigo-player.

hastarin avatar hastarin commented on August 15, 2024

@matvp91 Thanks for the sample.

Unfortunately I'm new to both React and Typescript and thought I'd found a holy-grail here, but alas I can't get it to work as is.

Property 'init' does not exist on type 'typeof import("/home/jon/source/myapp/node_modules/indigo-player/src/index")'.

I did manage to fix an issue with the useRef usage by making the following change:
const ref = useRef<HTMLDivElement>(null);

from indigo-player.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.