import React, { useEffect, useState, useRef } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faPlay, faPause } from "@fortawesome/free-solid-svg-icons"; import { Toast } from "antd-mobile"; import BackgroundPlayer from "next-video/background-player"; import { toggleFullScreen } from "@/utils/tools/handleFuns"; import Slider from "rc-slider"; import "rc-slider/assets/index.css"; export default function VideoPlayer({ video }) { const [videoState, setVideoState] = useState({ play: true, currentTimePercentage: 0, }); const [showController, setShowController] = useState(false); const [screenState, setScreenState] = useState(false); const isSliding = useRef(null); const userAgent = window && window.navigator?.userAgent; // const currentTime = useMemo(()=>{ // const videoPlayer = document.getElementById("videoPlayer"); // if (videoPlayer) { // return videoPlayer.currentTime; // } // return 0; // }) useEffect(() => { window.onresize = function () { if (document.fullscreenElement) { console.log("进入全屏"); } else { console.log("退出全屏"); const video = document.getElementById("videoContainer"); const controller = document.getElementById("controllerContainer"); if (!video || !controller) return; video.firstChild.style.transform = "none"; video.firstChild.style.width = "auto"; video.firstChild.style.height = "auto"; video.firstChild.style.transformOrigin = ""; controller.style.width = "100%"; controller.style.height = "100%"; controller.firstChild.style.marginLeft = "0"; setScreenState(false); } }; const videoPlayer = document.getElementById("videoPlayer"); const openVolum = () => { videoPlayer.muted = false; }; const pauseVideo = () => { setVideoState((old) => ({ ...old, play: false })); }; const updateTime = () => { // console.log((videoPlayer.currentTime / videoPlayer.duration) * 100); if (!isSliding.current) setVideoState((old) => ({ ...old, currentTimePercentage: (videoPlayer.currentTime / videoPlayer.duration) * 100, })); }; if (videoPlayer) { videoPlayer.addEventListener("loadedmetadata", openVolum); videoPlayer.addEventListener("ended", pauseVideo); videoPlayer.addEventListener("timeupdate", updateTime); } return () => { if (videoPlayer) { videoPlayer.removeEventListener("loadedmetadata", openVolum); videoPlayer.removeEventListener("ended", pauseVideo); videoPlayer.removeEventListener("timeupdate", updateTime); } }; }, []); const getCurrentTime = (value) => { const videoPlayer = document.getElementById("videoPlayer"); if (!videoPlayer) null; const totalTime = videoPlayer?.duration; let timeSec = 0; timeSec = (totalTime * (100 - value)) / 100; const hour = Math.floor(timeSec / (60 * 60)); const minute = Math.floor((timeSec - hour * (60 * 60)) / 60); const second = Math.floor(timeSec - hour * (60 * 60) - minute * 60); return { desc: `${hour > 9 ? hour : "0" + hour}:${ minute > 9 ? minute : "0" + minute }:${second > 9 ? second : "0" + second}`, timeSec: (totalTime * value) / 100, }; }; const toastValue = (value) => { isSliding.current = true; const videoPlayer = document.getElementById("videoPlayer"); if (!videoPlayer) return; videoPlayer.pause(); const currentTimeStr = getCurrentTime(value); console.log("currentTimeStr-----", currentTimeStr.desc); // if (!currentTimeStr) return; Toast.show({ content: currentTimeStr.desc, position: "top", maskStyle: { "--z-index": 999999 }, }); videoPlayer.currentTime = currentTimeStr.timeSec; // console.log("currentTimeStr", currentTimeStr); setVideoState((old) => ({ play: false, currentTimePercentage: value })); }; const handleStart = () => { const videoPlayer = document.getElementById("videoPlayer"); if (!videoPlayer) return; videoPlayer.play(); isSliding.current = false; setVideoState((old) => ({ ...old, play: true })); }; return (
setShowController((old) => !old)} >
video.h ? "flex-col-reverse items-start justify-evenly" : "w-full flex-row-reverse justify-between items-end gap-4 py-10" }`} style={{ display: showController ? "flex" : "none", }} id="controllerContainer" > {/*
} style={{ "--fill-color": "#ff669e" }} /> */}
video.h ? "grid grid-rows-[83px,30px,30px] mb-4 w-max " : "flex" }`} >
video.h ? "rotate-90 -translate-y-7 " : "" }`} > {getCurrentTime(videoState.currentTimePercentage).desc}
video.h ? "rotate-90 -translate-y-7 origin-bottom-left" : "" } > {/Android/i.test(userAgent) && (
{ toggleFullScreen( "videoContainer", "controllerContainer", setScreenState ); }} className="rounded-full bg-[#ffffff80] p-2 w-[30px] h-[30px]" > {screenState ? ( ) : ( <> )}
)}
video.h ? "rotate-90 -translate-y-7 origin-bottom-left" : "" }`} > { const videoPlayer = document.getElementById("videoPlayer"); if (videoState.play) { setVideoState((old) => ({ ...old, play: false })); videoPlayer.pause(); } else { setVideoState((old) => ({ ...old, play: true })); videoPlayer.play(); } }} />
); }