import React, { useEffect, useRef, useContext, useState } from "react"; import Hls from "hls.js"; import { motion, AnimatePresence } from "framer-motion"; import { PlayIcon, PauseIcon, SpeakerWaveIcon, SpeakerXMarkIcon, Cog6ToothIcon, ArrowsPointingOutIcon, ArrowsPointingInIcon, } from "@heroicons/react/24/solid"; import "plyr/dist/plyr.css"; import { VideoPlayerContext } from "./VideoPlayer"; import { formatTime } from "./utlis"; import { PlaybackSpeed } from "./type"; export const Controls = () => { const { showControls, setShowControls, isSettingsOpen, setIsSettingsOpen, playbackSpeed, setPlaybackSpeed, videoRef, isReady, setIsReady, isPlaying, setIsPlaying, bufferingState, setBufferingState, volume, setVolume, isMuted, setIsMuted, loadingProgress, setLoadingProgress, currentTime, setCurrentTime, duration, setDuration, brightness, setBrightness, isDragging, setIsDragging, isHovering, setIsHovering, progressRef, } = useContext(VideoPlayerContext); const handleProgressClick = (e: React.MouseEvent) => { if (!videoRef.current || !progressRef.current) return; const rect = progressRef.current.getBoundingClientRect(); const percent = (e.clientX - rect.left) / rect.width; videoRef.current.currentTime = percent * videoRef.current.duration; }; // 控制栏显示逻辑 useEffect(() => { let timer: number; if (!isHovering && !isDragging) { timer = window.setTimeout(() => { setShowControls(false); }, 2000); } return () => { if (timer) window.clearTimeout(timer); }; }, [isHovering, isDragging]); return ( {/* 进度条 */}
{ setIsDragging(true); handleProgressClick(e); }}> {/* 背景条 */}
{/* 播放进度 */} {/* 进度球 */} {/* 预览进度 */}
{/* 控制按钮区域 */}
{/* 播放/暂停按钮 */} {/* 音量控制 */}
{ const newVolume = parseFloat( e.target.value ); setVolume(newVolume); if (videoRef.current) { videoRef.current.volume = newVolume; } }} className="h-24 w-2 accent-primary-500 [-webkit-appearance:slider-vertical]" />
{/* 时间显示 */} {duration && ( {formatTime(currentTime)} / {formatTime(duration)} )}
{/* 右侧控制按钮 */}
{/* 设置按钮 */}
{/* 设置菜单 */} {isSettingsOpen && ( {/* 倍速选择 */}

播放速度

{[0.5, 0.75, 1.0, 1.25, 1.5, 2.0].map( (speed) => ( ) )}
{/* 亮度调节 */}

亮度

setBrightness( parseFloat(e.target.value) ) } className="w-full accent-primary-500" />
)}
{/* 全屏按钮 */}
); }; export default Controls;