VideoPlayer.tsx 920 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { useRef, useEffect } from "react";
  2. import videojs, { VideoJsPlayer } from "video.js";
  3. const VideoPlayer = ({
  4. getVideoHandle,
  5. src
  6. }: {
  7. getVideoHandle: (videoHandle: VideoJsPlayer) => void;
  8. src: string;
  9. }) => {
  10. const videoPlayerRef = useRef();
  11. const video: { current: VideoJsPlayer | undefined } = useRef();
  12. useEffect(() => {
  13. const tmp = videojs(videoPlayerRef.current, {
  14. width: 512,
  15. loop: true,
  16. controls: true
  17. });
  18. getVideoHandle(tmp);
  19. video.current = tmp;
  20. return () => {
  21. if (tmp) tmp.dispose();
  22. };
  23. }, []);
  24. useEffect(() => {
  25. if (video.current) {
  26. const paused = video.current.paused();
  27. video.current.src(src);
  28. if (!paused) video.current.play();
  29. }
  30. }, [src]);
  31. return (
  32. <div data-vjs-player>
  33. <video ref={videoPlayerRef as any} className="video-js vjs-16-9" />
  34. </div>
  35. );
  36. };
  37. export default VideoPlayer;