fix: 쇼츠 넘길 때마다 깜빡이던 문제 — 앞뒤 영상 플레이어를 미리 띄워 두고, 넘기면 새로 띄우지 않고 재생·일시정지만 바꿈
This commit is contained in:
1 parent
8ea8c9b368
commit
ddcb4ee771
1 file changed
+39
-7
@@ -14,9 +14,19 @@ const SCALE = (CARD_W / PLAYER_W) * 1.4
|
|||||||
// 전체 화면 창의 영상 칸: 닫기 줄 아래 남은 높이와 화면 너비 안에 9:16으로 맞춘다
|
// 전체 화면 창의 영상 칸: 닫기 줄 아래 남은 높이와 화면 너비 안에 9:16으로 맞춘다
|
||||||
const FIT = { width: 'min(100vw, calc((100dvh - env(safe-area-inset-top, 0px) - 52px) * 9 / 16))', aspectRatio: '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 YT_ORIGIN = 'https://www.youtube-nocookie.com'
|
||||||
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}`
|
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 */
|
/** 화면에 보이는 동안만 true */
|
||||||
@@ -86,11 +96,13 @@ function ShortCard({ s, playing, onOpen }: { s: ShortItem; playing: boolean; onO
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 전체 화면 재생 창: 네이티브 <dialog>라 ESC·포커스 가두기가 기본. 보이는 영상 하나만 띄운다 */
|
/** 전체 화면 재생 창: 네이티브 <dialog>라 ESC·포커스 가두기가 기본. 넘길 때 새로 띄우며 깜빡이지 않게 앞뒤 영상까지 미리 띄워 두고, 보이는 것만 재생한다 */
|
||||||
function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: number; onClose: () => void }) {
|
function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: number; onClose: () => void }) {
|
||||||
const dialog = useRef<HTMLDialogElement>(null)
|
const dialog = useRef<HTMLDialogElement>(null)
|
||||||
const list = useRef<HTMLDivElement>(null)
|
const list = useRef<HTMLDivElement>(null)
|
||||||
const [at, setAt] = useState(start)
|
const [at, setAt] = useState(start)
|
||||||
|
const frames = useRef(new Map<number, HTMLIFrameElement>())
|
||||||
|
const atRef = useRef(start)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const el = dialog.current
|
const el = dialog.current
|
||||||
@@ -118,6 +130,11 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
|
|||||||
return () => io.disconnect()
|
return () => io.disconnect()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
atRef.current = at
|
||||||
|
for (const [i, f] of frames.current) command(f, i === at ? 'playVideo' : 'pauseVideo')
|
||||||
|
}, [at])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<dialog ref={dialog} aria-label="날씨각 쇼츠" onClose={onClose} className="fixed inset-0 m-0 size-full max-h-none max-w-none bg-black p-0 text-white">
|
<dialog ref={dialog} aria-label="날씨각 쇼츠" onClose={onClose} className="fixed inset-0 m-0 size-full max-h-none max-w-none bg-black p-0 text-white">
|
||||||
{/* 유튜브 플레이어 위쪽 로고·소리 버튼을 가리지 않게 닫기 줄을 따로 둔다 */}
|
{/* 유튜브 플레이어 위쪽 로고·소리 버튼을 가리지 않게 닫기 줄을 따로 둔다 */}
|
||||||
@@ -130,9 +147,24 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
|
|||||||
{items.map((s, i) => (
|
{items.map((s, i) => (
|
||||||
<div key={s.id} data-i={i} className="relative grid h-full snap-start place-items-center">
|
<div key={s.id} data-i={i} className="relative grid h-full snap-start place-items-center">
|
||||||
<div className="relative" style={FIT}>
|
<div className="relative" style={FIT}>
|
||||||
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" className={cx('absolute inset-0 size-full object-cover', i !== at && 'opacity-60')} />
|
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" className="absolute inset-0 size-full object-cover" />
|
||||||
{i === at && (
|
{Math.abs(i - at) <= 1 && (
|
||||||
<iframe src={embed(s.id, true)} title={s.title} allow="autoplay; encrypted-media; picture-in-picture; fullscreen" allowFullScreen className="absolute inset-0 size-full border-0" />
|
<iframe
|
||||||
|
ref={(f) => {
|
||||||
|
if (f) frames.current.set(i, f)
|
||||||
|
else frames.current.delete(i)
|
||||||
|
}}
|
||||||
|
src={embed(s.id, true, i === start)}
|
||||||
|
title={s.title}
|
||||||
|
allow="autoplay; encrypted-media; picture-in-picture; fullscreen"
|
||||||
|
allowFullScreen
|
||||||
|
// 미리 띄운 플레이어가 다 뜨기 전에 넘어왔으면, 뜬 뒤에 재생을 건다
|
||||||
|
onLoad={(e) => {
|
||||||
|
const f = e.currentTarget
|
||||||
|
setTimeout(() => command(f, atRef.current === i ? 'playVideo' : 'pauseVideo'), 300)
|
||||||
|
}}
|
||||||
|
className="absolute inset-0 size-full border-0"
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in new issue
Block a user