diff --git a/frontend/src/b/Shorts.tsx b/frontend/src/b/Shorts.tsx index 4a97374..52b47bc 100644 --- a/frontend/src/b/Shorts.tsx +++ b/frontend/src/b/Shorts.tsx @@ -14,9 +14,19 @@ const SCALE = (CARD_W / PLAYER_W) * 1.4 // 전체 화면 창의 영상 칸: 닫기 줄 아래 남은 높이와 화면 너비 안에 9:16으로 맞춘다 const FIT = { width: 'min(100vw, calc((100dvh - env(safe-area-inset-top, 0px) - 52px) * 9 / 16))', aspectRatio: '9 / 16' } -function embed(id: string, controls: boolean) { - const q = new URLSearchParams({ autoplay: '1', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: controls ? '1' : '0', rel: '0', modestbranding: '1' }) - return `https://www.youtube-nocookie.com/embed/${id}?${q}` +const YT_ORIGIN = 'https://www.youtube-nocookie.com' + +function embed(id: string, controls: boolean, autoplay = true) { + const q = new URLSearchParams({ + autoplay: autoplay ? '1' : '0', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: controls ? '1' : '0', rel: '0', modestbranding: '1', + enablejsapi: '1', origin: window.location.origin, + }) + return `${YT_ORIGIN}/embed/${id}?${q}` +} + +/** 이미 띄운 플레이어에 재생·일시정지 (IFrame API 메시지) */ +function command(frame: HTMLIFrameElement | null | undefined, func: 'playVideo' | 'pauseVideo') { + frame?.contentWindow?.postMessage(JSON.stringify({ event: 'command', func, args: [] }), YT_ORIGIN) } /** 화면에 보이는 동안만 true */ @@ -86,11 +96,13 @@ function ShortCard({ s, playing, onOpen }: { s: ShortItem; playing: boolean; onO ) } -/** 전체 화면 재생 창: 네이티브 라 ESC·포커스 가두기가 기본. 보이는 영상 하나만 띄운다 */ +/** 전체 화면 재생 창: 네이티브 라 ESC·포커스 가두기가 기본. 넘길 때 새로 띄우며 깜빡이지 않게 앞뒤 영상까지 미리 띄워 두고, 보이는 것만 재생한다 */ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: number; onClose: () => void }) { const dialog = useRef(null) const list = useRef(null) const [at, setAt] = useState(start) + const frames = useRef(new Map()) + const atRef = useRef(start) useEffect(() => { const el = dialog.current @@ -118,6 +130,11 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu return () => io.disconnect() }, []) + useEffect(() => { + atRef.current = at + for (const [i, f] of frames.current) command(f, i === at ? 'playVideo' : 'pauseVideo') + }, [at]) + return ( {/* 유튜브 플레이어 위쪽 로고·소리 버튼을 가리지 않게 닫기 줄을 따로 둔다 */} @@ -130,9 +147,24 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu {items.map((s, i) => (
- - {i === at && ( -