| 1 | import React, { useRef, useEffect } from 'react'; |
| 2 | import { Modal } from 'antd'; |
| 3 | import styles from './videoPlayer.module.scss'; |
| 4 | |
| 5 | interface VideoPlayerProps { |
| 6 | videoUrl: string; |
| 7 | visible: boolean; |
| 8 | onClose: () => void; |
| 9 | title?: string; |
| 10 | } |
| 11 | |
| 12 | const VideoPlayer: React.FC<VideoPlayerProps> = ({ |
| 13 | videoUrl, |
| 14 | visible, |
| 15 | onClose, |
| 16 | title, |
| 17 | }) => { |
| 18 | const videoRef = useRef<HTMLVideoElement>(null); |
| 19 | |
| 20 | useEffect(() => { |
| 21 | // 当模态框关闭时暂停视频 |
| 22 | if (!visible && videoRef.current) { |
| 23 | videoRef.current.pause(); |
| 24 | } |
| 25 | }, [visible]); |
| 26 | |
| 27 | return ( |
| 28 | <Modal |
| 29 | title={title || '视频播放'} |
| 30 | open={visible} |
| 31 | onCancel={onClose} |
| 32 | footer={null} |
| 33 | width={800} |
| 34 | centered |
| 35 | destroyOnClose |
| 36 | className={styles.videoModal} |
| 37 | > |
| 38 | <div className={styles.videoContainer}> |
| 39 | <video |
| 40 | ref={videoRef} |
| 41 | src={videoUrl} |
| 42 | controls |
| 43 | autoPlay |
| 44 | className={styles.videoPlayer} |
| 45 | /> |
| 46 | </div> |
| 47 | </Modal> |
| 48 | ); |
| 49 | }; |
| 50 | |
| 51 | export default VideoPlayer; |
| 52 |