import { useState, useEffect, useRef, useCallback } from 'react' import { useSearchParams } from 'react-router-dom' import { Play, Pause, Volume2, VolumeX, RotateCcw } from 'lucide-react' import './VideoWindow.scss' export default function VideoWindow() { const [searchParams] = useSearchParams() const videoPath = searchParams.get('videoPath') const [isPlaying, setIsPlaying] = useState(false) const [isMuted, setIsMuted] = useState(false) const [currentTime, setCurrentTime] = useState(0) const [duration, setDuration] = useState(0) const [volume, setVolume] = useState(1) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const videoRef = useRef(null) const progressRef = useRef(null) // 格式化时间 const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60) const secs = Math.floor(seconds % 60) return `${mins}:${secs.toString().padStart(2, '0')}` } //播放/暂停 const togglePlay = useCallback(() => { if (!videoRef.current) return if (isPlaying) { videoRef.current.pause() } else { videoRef.current.play() } }, [isPlaying]) // 静音切换 const toggleMute = useCallback(() => { if (!videoRef.current) return videoRef.current.muted = !isMuted setIsMuted(!isMuted) }, [isMuted]) // 进度条点击 const handleProgressClick = useCallback((e: React.MouseEvent) => { if (!videoRef.current || !progressRef.current) return e.stopPropagation() const rect = progressRef.current.getBoundingClientRect() const percent = (e.clientX - rect.left) / rect.width videoRef.current.currentTime = percent * duration }, [duration]) // 音量调节 const handleVolumeChange = useCallback((e: React.ChangeEvent) => { const newVolume = parseFloat(e.target.value) setVolume(newVolume) if (videoRef.current) { videoRef.current.volume = newVolume setIsMuted(newVolume === 0) } }, []) // 重新播放 const handleReplay = useCallback(() => { if (!videoRef.current) return videoRef.current.currentTime = 0 videoRef.current.play() }, []) // 快捷键 useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') window.electronAPI.window.close() if (e.key === ' ') { e.preventDefault() togglePlay() } if (e.key === 'm' || e.key === 'M') toggleMute() if (e.key === 'ArrowLeft' && videoRef.current) { videoRef.current.currentTime -= 5 } if (e.key === 'ArrowRight' && videoRef.current) { videoRef.current.currentTime += 5 } if (e.key === 'ArrowUp' && videoRef.current) { videoRef.current.volume = Math.min(1, videoRef.current.volume + 0.1) setVolume(videoRef.current.volume) } if (e.key === 'ArrowDown' && videoRef.current) { videoRef.current.volume = Math.max(0, videoRef.current.volume - 0.1) setVolume(videoRef.current.volume) } } window.addEventListener('keydown', handleKeyDown) return () => window.removeEventListener('keydown', handleKeyDown) }, [togglePlay, toggleMute]) if (!videoPath) { return (
无效的视频路径
) } const progress = duration > 0 ? (currentTime / duration) * 100 : 0 return (
{isLoading && (
)} {error && (
{error}
)}
) }