| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { useRef, useEffect } from "react";
- import videojs from "video.js";
- interface IProps {
- options: videojs.PlayerOptions;
- onReady: (player: videojs.Player) => void;
- }
- const VideoPlayerWidget = ({ options, onReady }: IProps) => {
- const videoRef = useRef<HTMLDivElement>(null);
- const playerRef = useRef<VideoJsPlayer | null>(null);
- useEffect(() => {
- if (!playerRef.current) {
- const videoElement = document.createElement("video-js");
- videoElement.classList.add("vjs-big-play-centered");
- videoRef.current?.appendChild(videoElement);
- const player = (playerRef.current = videojs(videoElement, options, () =>
- onReady?.(player)
- ));
- } else {
- const player = playerRef.current;
- if (options.autoplay !== undefined) {
- player.autoplay(options.autoplay);
- }
- if (options.sources !== undefined) {
- player.src(options.sources);
- }
- }
- }, [options, onReady]);
- useEffect(() => {
- return () => {
- const player = playerRef.current;
- if (player && !player.isDisposed()) {
- player.dispose();
- playerRef.current = null;
- }
- };
- }, []);
- return (
- <div data-vjs-player>
- <div ref={videoRef} className="video-js" />
- </div>
- );
- };
- export default VideoPlayerWidget;
|