-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f611cf
commit 3e3629a
Showing
4 changed files
with
158 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,164 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { ReactSVG } from "react-svg"; | ||
import { Maximize } from "lucide-react"; | ||
import { cn } from "@nextui-org/theme"; | ||
|
||
import PlayerProgress from "../player-progress"; | ||
|
||
import Viewer, { ViewerProps } from "."; | ||
|
||
import PlayIcon from "@/styles/icons/play.svg"; | ||
import PauseIcon from "@/styles/icons/pause.svg"; | ||
import { emitter } from "@/lib/emitter"; | ||
import { CircularProgress } from "@nextui-org/progress"; | ||
|
||
interface VideoViewerProps extends ViewerProps {} | ||
|
||
interface VideoViewerState { | ||
isLoading: boolean | ||
value: string | ||
|
||
paused: boolean | ||
duration: number | ||
currentTime: number | ||
} | ||
|
||
export default class VideoViewer extends Viewer<VideoViewerProps, VideoViewerState> { | ||
private readonly isFlv: boolean; | ||
private blob: Blob = new Blob(); | ||
private videoRef = React.createRef<HTMLVideoElement>(); | ||
|
||
private readonly eventController = new AbortController(); | ||
|
||
public constructor(props: VideoViewerProps) { | ||
super(props, "视频播放器"); | ||
|
||
this.state = { | ||
value: "" // video url | ||
isLoading: true, | ||
value: "", // video url | ||
paused: true, | ||
duration: 0, | ||
currentTime: 0 | ||
}; | ||
|
||
this.isFlv = this.props.fileName.split(".").findLast(() => true) === "flv"; | ||
} | ||
|
||
private handlePlayButtonClick() { | ||
if(!this.videoRef.current || this.state.isLoading) { | ||
this.setState({ paused: true }); | ||
|
||
return; | ||
} | ||
|
||
if(this.videoRef.current.paused) { | ||
this.setState({ paused: false }); | ||
this.videoRef.current.play(); | ||
} else { | ||
this.setState({ paused: true }); | ||
this.videoRef.current.pause(); | ||
} | ||
} | ||
|
||
private handleFullScreen() { | ||
if(!this.videoRef.current) return; | ||
|
||
this.videoRef.current.requestFullscreen(); | ||
} | ||
|
||
private handleTimeChange(time: number) { | ||
if(!this.videoRef.current) return; | ||
|
||
this.videoRef.current.currentTime = time; | ||
this.setState({ currentTime: time }); | ||
} | ||
|
||
public render(): React.ReactNode { | ||
return ( | ||
<div className="w-full h-full flex flex-col items-center"> | ||
<video | ||
className="max-h-[700px]" | ||
src={this.state.value} | ||
controls> | ||
<track kind="captions"/> | ||
</video> | ||
|
||
<div className="w-full h-6 mt-2 px-2 flex justify-center items-center"> | ||
<span className="text-default-400 text-sm ml-2">{this.props.fileName}</span> | ||
<div className="w-full h-full flex justify-center items-center"> | ||
<div className="flex flex-col relative"> | ||
<video | ||
src={this.state.value} | ||
className={cn("rounded-t-lg relative", this.state.isLoading ? "bg-default-300" : "")} | ||
onDurationChange={(e) => this.setState({ duration: e.currentTarget.duration })} | ||
onTimeUpdate={(e) => this.setState({ currentTime: e.currentTarget.currentTime })} | ||
onEnded={() => this.setState({ paused: true })} | ||
ref={this.videoRef}> | ||
<track kind="captions"/> | ||
</video> | ||
|
||
{this.state.isLoading && ( | ||
<div className="absolute top-0 left-0 right-0 bottom-0 flex justify-center items-center"> | ||
<CircularProgress | ||
size="sm" | ||
classNames={{ | ||
indicator: "stroke-default-800" | ||
}}/> | ||
</div> | ||
)} | ||
|
||
<div className="absolute left-0 right-0 bottom-8 h-10 p-2 flex justify-between"> | ||
<div className="flex items-center"> | ||
<button | ||
onClick={() => this.handlePlayButtonClick()} | ||
disabled={this.state.isLoading}> | ||
<ReactSVG | ||
src={this.state.paused ? PlayIcon["src"] : PauseIcon["src"]} | ||
className="w-6 h-6 [&_svg]:w-full [&_svg]:h-full"/> | ||
</button> | ||
</div> | ||
|
||
<div className="flex items-center"> | ||
<button | ||
onClick={() => this.handleFullScreen()} | ||
disabled={this.state.isLoading}> | ||
<Maximize size={22} color="#e8eaed" className="pr-1"/> | ||
</button> | ||
</div> | ||
</div> | ||
|
||
<PlayerProgress | ||
duration={this.state.duration} | ||
current={this.state.currentTime} | ||
timePlacement="bottom" | ||
disabled={this.state.isLoading} | ||
onTimeChange={(time) => this.handleTimeChange(time)}/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
public async componentDidMount() { | ||
/** @todo */ | ||
// this.setState({ value: URL.createObjectURL(new Blob([Buffer.from(await this.loadFile())])) }); | ||
const data = await this.loadFile(true) as ArrayBuffer; | ||
|
||
this.blob = new Blob([Buffer.from(data)], { type: "video/mp4" }); | ||
this.setState({ isLoading: false }); | ||
|
||
this.setState({ value: URL.createObjectURL(this.blob) }); | ||
this.initEvents(); | ||
} | ||
|
||
public componentWillUnmount() { | ||
if(!this.blob) return; | ||
|
||
URL.revokeObjectURL(this.state.value); | ||
this.blob = new Blob(); | ||
this.setState({ value: "" }); | ||
|
||
this.eventController.abort(); | ||
emitter.removeAllListeners("viewer:audio-player-time-change"); | ||
} | ||
|
||
private initEvents() { | ||
document.body.addEventListener("keydown", (e) => { | ||
if(e.code === "Space") { | ||
this.handlePlayButtonClick(); | ||
|
||
/** @see https://www.codeproject.com/Questions/1044876/one-enter-keypress-runs-event-handler-twice */ | ||
e.stopImmediatePropagation(); | ||
} | ||
}, { signal: this.eventController.signal }); | ||
} | ||
} |