// 视频预览播放器 import { useCallback, useEffect, useRef, useState } from 'react' import { Renderer } from '../video/render' import { AudioClock } from '../video/player' import { TL, DURATION, sceneIndexAt } from '../video/timeline' import { loadVoices, renderSegment } from '../video/audio' import { cx, fmtTime, Switch } from './ui' const CH_LABEL: Record = { prologue: '序章', shot1: '镜头一 航拍', shot2: '镜头二 跟拍', shot3: '镜头三 延时', shot4: '镜头四 特写', shot5: '镜头五 空镜', depth: '深读', recite: '背诵', epilogue: '尾声', } export default function PlayerView({ orient, setOrient, ready }: { orient: 'l' | 'p'; setOrient: (o: 'l' | 'p') => void; ready: boolean }) { const wrapRef = useRef(null) const boxRef = useRef(null) const canvasRef = useRef(null) const rendRef = useRef(null) const clockRef = useRef(new AudioClock()) const dirty = useRef(true) const [t, setT] = useState(0) const [playing, setPlaying] = useState(false) const [buffering, setBuffering] = useState(false) const [music, setMusic] = useState(true) const [vol, setVol] = useState(1) const [size, setSize] = useState<[number, number]>([0, 0]) const [voicePct, setVoicePct] = useState(0) useEffect(() => { loadVoices((p) => setVoicePct(p)) .then(() => renderSegment(0, true)) .catch(() => undefined) const c = clockRef.current c.onEnded = () => setPlaying(false) c.onBuffering = (b) => setBuffering(b) return () => c.dispose() }, []) // 画布尺寸 useEffect(() => { const el = boxRef.current if (!el) return let tm: number | undefined const ro = new ResizeObserver(() => { clearTimeout(tm) tm = window.setTimeout(() => { const r = el.getBoundingClientRect() const dpr = Math.min(window.devicePixelRatio || 1, 2) let w = Math.round(r.width * dpr) let h = Math.round(r.height * dpr) const cap = orient === 'l' ? [1920, 1080] : [1080, 1920] const k = Math.min(1, cap[0] / w, cap[1] / h) w = Math.round(w * k) h = Math.round(h * k) setSize((s) => (s[0] === w && s[1] === h ? s : [w, h])) }, 120) }) ro.observe(el) return () => { ro.disconnect() clearTimeout(tm) } }, [orient]) useEffect(() => { if (!size[0] || !ready) return const c = canvasRef.current! c.width = size[0] c.height = size[1] rendRef.current?.dispose() rendRef.current = new Renderer(size[0], size[1]) dirty.current = true // 空闲时预热各场景素材,避免播放时卡顿 const r = rendRef.current const scratch = document.createElement('canvas') scratch.width = size[0] scratch.height = size[1] const sg = scratch.getContext('2d')! const times = TL.scenes.flatMap((s) => [s.start + s.intro + 1, s.start + s.dur * 0.6]) let i = 0 let cancel = false const step = () => { if (cancel || i >= times.length || rendRef.current !== r) return if (!clockRef.current.playing) r.render(sg, times[i++]) setTimeout(step, 60) } const id = window.setTimeout(step, 800) return () => { cancel = true clearTimeout(id) } }, [size, ready]) // 渲染循环 useEffect(() => { let raf = 0 let lastUi = 0 const loop = (now: number) => { raf = requestAnimationFrame(loop) const r = rendRef.current const c = canvasRef.current if (!r || !c) return const clock = clockRef.current if (clock.playing || dirty.current) { const tt = clock.now() r.render(c.getContext('2d')!, tt) dirty.current = false if (now - lastUi > 120) { lastUi = now setT(tt) } } } raf = requestAnimationFrame(loop) return () => cancelAnimationFrame(raf) }, []) const toggle = useCallback(async () => { const c = clockRef.current if (c.playing) { c.pause() setPlaying(false) setT(c.now()) } else { setPlaying(true) try { await c.play() } catch (e) { console.error(e) setPlaying(false) } } }, []) const seek = useCallback((v: number) => { clockRef.current.seek(v) setT(v) dirty.current = true }, []) useEffect(() => { const onKey = (e: KeyboardEvent) => { const tag = (e.target as HTMLElement)?.tagName if (tag === 'INPUT' || tag === 'TEXTAREA') return if (e.code === 'Space') { e.preventDefault() toggle() } else if (e.code === 'ArrowRight') seek(clockRef.current.now() + 5) else if (e.code === 'ArrowLeft') seek(clockRef.current.now() - 5) } window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [toggle, seek]) const fs = () => { const el = wrapRef.current as (HTMLElement & { webkitRequestFullscreen?: () => void }) | null if (!el) return if (document.fullscreenElement) document.exitFullscreen() else if (el.requestFullscreen) el.requestFullscreen() else el.webkitRequestFullscreen?.() } const cur = sceneIndexAt(t) return (
{(['l', 'p'] as const).map((o) => ( ))}
{ setMusic(v); clockRef.current.setMusic(v) }} label="背景音乐" />
{(!ready || buffering) && (
{!ready ? '正在加载字体…' : `正在合成声音… ${voicePct < 1 ? Math.round(voicePct * 100) + '%' : ''}`}
)} {ready && !playing && !buffering && ( )}
{/* 控制条 */}
{TL.scenes.slice(1).map((s) => (
))} seek(Number(e.target.value))} className="absolute inset-0 w-full opacity-0 cursor-pointer" aria-label="进度" />
{fmtTime(t)} / {fmtTime(DURATION)}
{TL.scenes.map((s, i) => ( ))}
) }