fix: 쇼츠 넘길 때 검은 화면·로딩이 잠깐 보이던 문제 — 앞뒤 영상을 소리 없이 틀어 첫 장면을 받아 둔 뒤 멈추고 처음으로, 유튜브 조작 막대는 숨기고 소리 켜기 버튼을 따로 둠
This commit is contained in:
1 parent
ddcb4ee771
commit
b4d09574da
2 files changed
+62
-19
No files matched your search
+61
-18
@@ -16,19 +16,23 @@ const FIT = { width: 'min(100vw, calc((100dvh - env(safe-area-inset-top, 0px) -
|
||||
|
||||
const YT_ORIGIN = 'https://www.youtube-nocookie.com'
|
||||
|
||||
function embed(id: string, controls: boolean, autoplay = true) {
|
||||
function embed(id: string) {
|
||||
const q = new URLSearchParams({
|
||||
autoplay: autoplay ? '1' : '0', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: controls ? '1' : '0', rel: '0', modestbranding: '1',
|
||||
autoplay: '1', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: '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)
|
||||
type Func = 'playVideo' | 'pauseVideo' | 'seekTo' | 'mute' | 'unMute'
|
||||
|
||||
/** 이미 띄운 플레이어에 명령 (IFrame API 메시지) */
|
||||
function command(frame: HTMLIFrameElement | null | undefined, func: Func, args: unknown[] = []) {
|
||||
frame?.contentWindow?.postMessage(JSON.stringify({ event: 'command', func, args }), YT_ORIGIN)
|
||||
}
|
||||
|
||||
const PLAYING = 1
|
||||
|
||||
/** 화면에 보이는 동안만 true */
|
||||
function useVisible<T extends Element>(threshold = 0.5) {
|
||||
const [el, ref] = useState<T | null>(null)
|
||||
@@ -82,7 +86,7 @@ function ShortCard({ s, playing, onOpen }: { s: ShortItem; playing: boolean; onO
|
||||
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" loading="lazy" className="absolute inset-0 size-full object-cover" />
|
||||
{playing && (
|
||||
<iframe
|
||||
src={embed(s.id, false)}
|
||||
src={embed(s.id)}
|
||||
title={s.title}
|
||||
allow="autoplay; encrypted-media; picture-in-picture"
|
||||
tabIndex={-1}
|
||||
@@ -96,13 +100,18 @@ 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 }) {
|
||||
const dialog = useRef<HTMLDialogElement>(null)
|
||||
const list = useRef<HTMLDivElement>(null)
|
||||
const [at, setAt] = useState(start)
|
||||
const [sound, setSound] = useState(false)
|
||||
const frames = useRef(new Map<number, HTMLIFrameElement>())
|
||||
const atRef = useRef(start)
|
||||
const soundRef = useRef(false)
|
||||
|
||||
useEffect(() => {
|
||||
const el = dialog.current
|
||||
@@ -130,18 +139,56 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
|
||||
return () => io.disconnect()
|
||||
}, [])
|
||||
|
||||
// 미리 띄운 영상이 재생되기 시작하면(첫 장면을 받음) 보이는 영상이 아닐 때 멈추고 처음으로
|
||||
useEffect(() => {
|
||||
const onMessage = (e: MessageEvent) => {
|
||||
if (e.origin !== YT_ORIGIN || typeof e.data !== 'string') return
|
||||
let msg: { info?: { playerState?: number } }
|
||||
try {
|
||||
msg = JSON.parse(e.data)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
if (msg.info?.playerState !== PLAYING) return
|
||||
for (const [i, f] of frames.current) {
|
||||
if (f.contentWindow !== e.source || i === atRef.current) continue
|
||||
command(f, 'pauseVideo')
|
||||
command(f, 'seekTo', [0, true])
|
||||
}
|
||||
}
|
||||
window.addEventListener('message', onMessage)
|
||||
return () => window.removeEventListener('message', onMessage)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
atRef.current = at
|
||||
for (const [i, f] of frames.current) command(f, i === at ? 'playVideo' : 'pauseVideo')
|
||||
for (const [i, f] of frames.current) {
|
||||
if (i === at) {
|
||||
command(f, soundRef.current ? 'unMute' : 'mute')
|
||||
command(f, 'playVideo')
|
||||
} else {
|
||||
command(f, 'pauseVideo')
|
||||
command(f, 'mute')
|
||||
}
|
||||
}
|
||||
}, [at])
|
||||
|
||||
const toggleSound = () => {
|
||||
const on = !sound
|
||||
soundRef.current = on
|
||||
setSound(on)
|
||||
command(frames.current.get(at), on ? 'unMute' : 'mute')
|
||||
}
|
||||
|
||||
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">
|
||||
{/* 유튜브 플레이어 위쪽 로고·소리 버튼을 가리지 않게 닫기 줄을 따로 둔다 */}
|
||||
<div className="flex h-[calc(env(safe-area-inset-top,0px)+52px)] items-end px-3 pb-2">
|
||||
<div className="flex h-[calc(env(safe-area-inset-top,0px)+52px)] items-end justify-between px-3 pb-2">
|
||||
<button type="button" onClick={onClose} aria-label="닫기" className={cx('relative grid size-9 cursor-pointer place-items-center rounded-full bg-white/15 text-xl text-white', TAP44)}>
|
||||
×
|
||||
</button>
|
||||
<button type="button" onClick={toggleSound} aria-pressed={sound} className="h-9 cursor-pointer rounded-full bg-white/15 px-3.5 text-[13px] font-bold text-white">
|
||||
{sound ? '소리 끄기' : '소리 켜기'}
|
||||
</button>
|
||||
</div>
|
||||
<div ref={list} className="h-[calc(100dvh-env(safe-area-inset-top,0px)-52px)] snap-y snap-mandatory overflow-y-auto [scrollbar-width:none]">
|
||||
{items.map((s, i) => (
|
||||
@@ -154,15 +201,11 @@ function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: nu
|
||||
if (f) frames.current.set(i, f)
|
||||
else frames.current.delete(i)
|
||||
}}
|
||||
src={embed(s.id, true, i === start)}
|
||||
src={embed(s.id)}
|
||||
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)
|
||||
}}
|
||||
allow="autoplay; encrypted-media; picture-in-picture"
|
||||
// 재생 상태를 받으려면 플레이어에 먼저 '듣고 있다'고 알린다
|
||||
onLoad={(e) => e.currentTarget.contentWindow?.postMessage(JSON.stringify({ event: 'listening', id: s.id }), YT_ORIGIN)}
|
||||
className="absolute inset-0 size-full border-0"
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user