0

Here is my code:

useEffect(() => {
    playerRef.current.seekTo(convertedflat[currentTimeIndex], 'seconds');
  });
  return (
    <>
      <main>
        <div className={style.main_container}>
          <NavBar />

          <div className={style.heronext}>
            <div className={style.hero_container}>
              <ReactPlayer
                ref={playerRef}
                playing
                controls={true}
                url={`videos/${episList[currentEpisodeIndex]}.mkv`}
                width="90%"
                height="55rem"
                />
               </div>

              <button
              className={style.next}
              onClick={() => {
                setCurrentTimeIndex((prevTimeIndex) => prevTimeIndex + 1) %
                  convertedflat.length;

                setcurrentEpisodeIndex((prevTimeIndex) => prevTimeIndex + 1) %
                  convertedflat.length;

                console.log(currentTimeIndex);
                console.log(currentEpisodeIndex);
              }}
            >
              Next
            </button>

basically when you click on the next button it moves to the next timestamp until the array finishes. but I want to it starts again when the array list is finished.Currently when the array navigation is finished it shows error.

I used % length but it doesnt work. Here is the error message when the list is finished:

TypeError: Failed to set the 'currentTime' property on 'HTMLMediaElement': The provided double value is non-finite.

1 Answer 1

1

You should be careful with these lines

setCurrentTimeIndex((prevTimeIndex) => prevTimeIndex + 1) % convertedflat.length;

I think there is a problem with brackets. It should be something like;

setCurrentTimeIndex((prevTimeIndex) => (prevTimeIndex + 1) % convertedflat.length);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it worked! What if I wanted add a previous button? setcurrentEpisodeIndex( (prevTimeIndex) => (prevTimeIndex - 1) % convertedflat.length ); this didnt work.
Of course, it wouldn't. If you still want to do it inline. You can do; setcurrentEpisodeIndex( (prevTimeIndex) => (convertedflat.length + prevTimeIndex - 1) % convertedflat.length );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.