import React, { FC, useState } from "react" import { MediaOption, PlayerState, Subtitle } from "../../lib/types" import InteractionHandler from "../action/InteractionHandler" import IconBigPause from "../icon/IconBigPause" import IconBigPlay from "../icon/IconBigPlay" import classNames from "classnames" import InputSlider from "../input/InputSlider" import IconPlay from "../icon/IconPlay" import IconPause from "../icon/IconPause" import IconReplay from "../icon/IconReplay" import { secondsToTime, SYNC_DELTA } from "../../lib/utils" import IconCompress from "../icon/IconCompress" import IconExpand from "../icon/IconExpand" import ControlButton from "../input/ControlButton" import VolumeControl from "./VolumeControl" import IconBackward from "../icon/IconBackward" import IconForward from "../icon/IconForward" import PlayerMenu from "./PlayerMenu" import { Tooltip } from "react-tooltip" import IconNewTab from "../icon/IconNewTab" import Link from "next/link" interface Props extends PlayerState { roomId: string setCurrentSrc: (src: MediaOption) => void setCurrentSub: (sub: Subtitle) => void setPaused: (paused: boolean) => void setVolume: (volume: number) => void setMuted: (muted: boolean) => void setProgress: (progress: number) => void setPlaybackRate: (playbackRate: number) => void setFullscreen: (fullscreen: boolean) => void setLoop: (loop: boolean) => void playIndex: (index: number) => void setSeeking: (seeking: boolean) => void playAgain: () => void } let interaction = false let doubleClick = false let interactionTime = 0 let lastMouseMove = 0 const Controls: FC = ({ roomId, playing, playlist, currentSrc, setCurrentSrc, currentSub, setCurrentSub, paused, setPaused, volume, setVolume, muted, setMuted, progress, setProgress, playbackRate, loop, setLoop, setPlaybackRate, fullscreen, setFullscreen, duration, playIndex, setSeeking, playAgain, }) => { const [showControls, setShowControls] = useState(true) const [showTimePlayed, setShowTimePlayed] = useState(true) const [menuOpen, setMenuOpen] = useState(false) const interact = () => { interaction = true interactionTime = new Date().getTime() setTimeout(() => { if (new Date().getTime() - interactionTime > 350) { if (interaction && !doubleClick) { doubleClick = false if (playEnded()) { playAgain() } else { setPaused(!paused) } } interaction = false } }, 400) } const showControlsAction = (touch: boolean | null) => { if (!showControls) { setShowControls(true) } mouseMoved(touch) } const playEnded = () => { return paused && progress > duration - SYNC_DELTA } const mouseMoved = (touch: boolean | null = null) => { lastMouseMove = new Date().getTime() setTimeout( () => { if (new Date().getTime() - lastMouseMove > (touch ? 3150 : 1550)) { setShowControls(false) } }, touch ? 3200 : 1600 ) } const show = showControls || menuOpen return ( <> { setShowControls(!touch) }} tabIndex={1} onKey={(key) => { console.log("Key down", key) if (key === " ") { setPaused(!paused) } }} > { if (interaction) { doubleClick = true interaction = false console.log("Toggled fullscreen") setFullscreen(!fullscreen) } else if (touch) { setShowControls(!showControls) setMenuOpen(false) } interact() mouseMoved(touch) }} onMove={(_, touch) => { showControlsAction(!touch) }} > {paused ? : } { setProgress(value) mouseMoved() }} max={duration} setSeeking={setSeeking} showValueHover={true} />
{playlist.currentIndex > 0 && ( { if (show && playlist.currentIndex > 0) { playIndex(playlist.currentIndex - 1) } }} interaction={showControlsAction} > )} { if (show) { if (playEnded()) { playAgain() } else { setPaused(!paused) } } }} interaction={showControlsAction} > {playEnded() ? ( ) : paused ? ( ) : ( )} {playlist.currentIndex < playlist.items.length - 1 && ( { if (show && playlist.currentIndex < playlist.items.length - 1) { playIndex(playlist.currentIndex + 1) } }} interaction={showControlsAction} > )} { if (show) { setShowTimePlayed(!showTimePlayed) } }} interaction={showControlsAction} > {(showTimePlayed ? secondsToTime(progress) : "-" + secondsToTime(duration - progress)) + " / " + secondsToTime(duration)} { window.open(currentSrc.src, "_blank")?.focus() }} interaction={showControlsAction} > { console.log("Toggled fullscreen") setFullscreen(!fullscreen) }} interaction={showControlsAction} > {fullscreen ? : }
) } export default Controls