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

Question: Ref.seekTo is not a function #1548

Closed
yek99kr opened this issue Nov 22, 2022 · 8 comments
Closed

Question: Ref.seekTo is not a function #1548

yek99kr opened this issue Nov 22, 2022 · 8 comments

Comments

@yek99kr
Copy link

yek99kr commented Nov 22, 2022

I am using react-player with my next.js site and am having trouble using seekTo function. It results in error saying: "playerRef.seekTo is not a function"

I tried "playerRef.current.seekTo(parseFloat(e.target.value))" as well but same error occurs

This was my code:

import { useRef } from "react";
 const ReactPlayer = dynamic(() => import("react-player"), { ssr: false });

 const Player = ({url}) => {

 const playerRef = useRef();

 const [player, setPlayer] = useState({
     playing: true,
     muted: true,
     played: 0,
     loaded: 0,
     duration: 0,
     seeking:false
   });

 const handlePlayPause = function () {
 setPlayer({ ...player, playing: !player.playing });
};

const handleMuted = () => {
   setPlayer({ ...player, muted: !player.muted });
};

const handleSeekMouseDown = (e) => {
 setPlayer({ ...player, seeking: true });
};

const handleSeekChange = (e) => {
 setPlayer({ ...player, played: parseFloat(e.target.value) });
};

const handleSeekMouseUp = (e) => {
 setPlayer({ ...player, seeking: false });
 playerRef.seekTo(parseFloat(e.target.value));
};

const handleProgress = (state) => {
 if (!player.seeking) {
   setPlayer(state);
 }
};

return (
 <div
   className={`relative justify-center content-center w-full`}
  
 >
   <ReactPlayer
     ref={playerRef}
     className="absolute rounded"
     width="100%"
     height="100%"
     url={url}
     volume={1}
     playIcon={<></>}
     playing={player.playing}
     loop={true}
     muted={player.muted}
     onProgress={handleProgress}
   ></ReactPlayer>

   <div
     className={"absolute w-full h-full"}
     onClick={handlePlayPause}
   ></div>

   <div>
     <input
       type="range"
       min={0}
       max={0.999999}
       step="any"
       value={player.played}
       onMouseDown={handleSeekMouseDown}
       onChange={handleSeekChange}
       onMouseUp={handleSeekMouseUp}
     />
   </div>
 </div>
);
};
 
export default Player;

Even when I copy paste exact code from demo(https://github.com/cookpete/react-player/blob/master/src/demo/App.js), same error shows, I'm not sure why it happens :(

@GOATS2K
Copy link

GOATS2K commented Nov 24, 2022

I had to specify the type info to get the ref to work properly.

const playerRef = React.useRef<ReactPlayer>(null);

Used like this:

    <ReactPlayer
      ref={playerRef}
      url={streamTrack.link}
      playing={playState}
      onPlay={() => announceMediaSession()}
      onProgress={(state) => {
        setSecondsPlayed(state.playedSeconds);
      }}
      onError={(error, data, hlsInstance) => {
        console.log({ error, data, hlsInstance });
      }}
      onEnded={() => nextTrack()}
      width={0}
      height={0}
      style={{ display: "none" }}
    ></ReactPlayer>

@Abdullah0991
Copy link

You need to call it like this: playerRef.current.seekTo().

@yek99kr
Copy link
Author

yek99kr commented Nov 28, 2022

I had to specify the type info to get the ref to work properly.

const playerRef = React.useRef<ReactPlayer>(null);

I'm not using typescript :(

@yek99kr
Copy link
Author

yek99kr commented Nov 28, 2022

You need to call it like this: playerRef.current.seekTo().

Yes I've tried but same error occurs

@pszopa-tl
Copy link

Same issue but with showPreview() method. The only thing that is under playerRef.current is a mysterious function retry().

I'm using [email protected]

@yanckst
Copy link

yanckst commented Dec 1, 2022

I had to specify the type info to get the ref to work properly.

const playerRef = React.useRef<ReactPlayer>(null);

I tried, but still have the issue

@singh-inder
Copy link

Try this solution

#1455 (comment)

@yanckst
Copy link

yanckst commented Dec 8, 2022

Try this solution

#1455 (comment)

I tried the solution describe in the comment. I needed to adapt the code a bit because I got compilation error using [email protected] and [email protected]

Here's the code for anyone interested:

// main file
import dynamic from "next/dynamic";

const ReactPlayerComponent = dynamic<ReactPlayerProps>(
    () => import("./ReactPlayer").then(r => r.ReactPlayerComponent),
    {
        ssr: false
    }
);

// ReactPlayer.tsx
const ReactPlayerComponent: React.FC<ReactPlayerProps> = ({
  playerRef,
}) => {
  return (
    <ReactPlayer
      ref={playerRef}
    />
  );
};

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

7 participants