import React, { useState, useRef, useEffect } from 'react'; import { GeneratedVideo } from '../types'; import { GeminiService } from '../geminiService'; const VideoPlayer: React.FC<{ url: string }> = ({ url }) => { const videoRef = useRef(null); const [isPlaying, setIsPlaying] = useState(false); const [progress, setProgress] = useState(0); const [volume, setVolume] = useState(1); const [isMuted, setIsMuted] = useState(false); const [duration, setDuration] = useState(0); const [currentTime, setCurrentTime] = useState(0); const [showControls, setShowControls] = useState(true); const controlsTimeoutRef = useRef(null); const togglePlay = () => { if (videoRef.current) { if (isPlaying) { videoRef.current.pause(); } else { videoRef.current.play(); } setIsPlaying(!isPlaying); } }; const handleTimeUpdate = () => { if (videoRef.current) { const current = videoRef.current.currentTime; const total = videoRef.current.duration; setCurrentTime(current); setProgress((current / total) * 100); } }; const handleSeek = (e: React.ChangeEvent) => { const seekTime = (parseFloat(e.target.value) / 100) * duration; if (videoRef.current) { videoRef.current.currentTime = seekTime; setProgress(parseFloat(e.target.value)); } }; const handleVolumeChange = (e: React.ChangeEvent) => { const val = parseFloat(e.target.value); setVolume(val); if (videoRef.current) { videoRef.current.volume = val; setIsMuted(val === 0); } }; const toggleMute = () => { if (videoRef.current) { const newMuteState = !isMuted; setIsMuted(newMuteState); videoRef.current.muted = newMuteState; if (!newMuteState && volume === 0) { setVolume(0.5); videoRef.current.volume = 0.5; } } }; const formatTime = (time: number) => { const minutes = Math.floor(time / 60); const seconds = Math.floor(time % 60); return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`; }; const handleMouseMove = () => { setShowControls(true); if (controlsTimeoutRef.current) window.clearTimeout(controlsTimeoutRef.current); controlsTimeoutRef.current = window.setTimeout(() => { if (isPlaying) setShowControls(false); }, 3000); }; useEffect(() => { const video = videoRef.current; if (video) { const onLoadedMetadata = () => setDuration(video.duration); video.addEventListener('loadedmetadata', onLoadedMetadata); return () => video.removeEventListener('loadedmetadata', onLoadedMetadata); } }, []); return (
isPlaying && setShowControls(false)} >