cookpete / react-player

A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion
https://cookpete.github.io/react-player
MIT License
8.89k stars 1.12k forks source link

Add source and destination seconds for `onSeek` #1774

Closed Zeyuzhao closed 3 weeks ago

Zeyuzhao commented 4 weeks ago

I'm trying to track a "heatmap" of what parts a video is viewed. I would like to track when a user seeks in a video, where the seek left and where it is going. Currently, I'm able to get the "destination": that's the seconds parameter in (property) onSeek?: ((seconds: number) => void) | undefined.

My current solution is to set a buffer of frames in the past:

const [offsetMs, setOffsetMs] = useState<number>(0);
const [offsetHistory, setOffsetHistory] = useState<number[]>([0, 0]);
useEffect(() => {
  setOffsetHistory(prevHistory => {
    const newHistory = [offsetMs, ...prevHistory];
    if (newHistory.length > OFFSET_HISTORY_LENGTH) {
      newHistory.pop();
    }
    return newHistory;
  });
}, [offsetMs]);
...
<FilePlayer
  onProgress={progress => {
    setOffsetMs(progress.playedSeconds * 1000);
  }}
</FilePlayer>

But, the solution is incredibly hacky. What is the best way to get source time? Thanks!