# 西瓜视频 xgplayer

import React, { useRef, useEffect, useState } from 'react';
import Player from 'xgplayer'; // 确保你已经正确引入了 xgplayer

const VideoPlayer = ({ anchorId, src, height, width }) => {
  const playerRef = useRef(null);

  useEffect(() => {
    const playerInstance = new Player({
      id: anchorId,
      url: src,
      height: height,
      width: width,
      lang: 'zh',
      autoplay: false, // 根据需要设置
    });
    playerRef.current = playerInstance;

    return () => {
      if (playerRef.current) {
        playerRef.current.destroy(); // 清理播放器实例
      }
    };
  }, [anchorId, src, height, width]);

  return (
    <div>
      <div id={anchorId}></div>
    </div>
  );
};

export default VideoPlayer;
最后更新: 1/4/2025, 6:56:47 PM