fix: 쇼츠 넘길 때 검은 화면·로딩이 잠깐 보이던 문제 — 앞뒤 영상을 소리 없이 틀어 첫 장면을 받아 둔 뒤 멈추고 처음으로, 유튜브 조작 막대는 숨기고 소리 켜기 버튼을 따로 둠

This commit is contained in:
minaChoi committed 2026-10-26 14:34:29 +09:00
1 parent ddcb4ee771
commit b4d09574da
2 files changed
+62 -19

No files matched your search

+1 -1
View File
@@ -60,7 +60,7 @@
| [ ] | 토스 미니앱 최신 버전 재등록 — 웹이 바뀌면 자동 반영 안 됨 | 10/21 회의 | |
| [ ] | 유튜브 숏폼 자동화 진행 방안 협의 (김성경, 10/22) | 10/21 회의 | |
| [ ] | 오늘 화면 쇼츠 줄 — YouTube Data API 키(`YOUTUBE_API_KEY`) 발급해 운영 .env 에 넣기. 없으면 `youtube_shorts`(영상 ID 3개 고정)만 나와 새 쇼츠가 자동으로 안 붙음 (https://console.cloud.google.com/apis/library/youtube.googleapis.com) | 채널 RSS가 404라 키 없이 채널 목록을 못 받음 | `backend/config/settings.py`, `backend/services/shorts_service.py` |
| [ ] | **[결정 필요]** 쇼츠 줄 문구 '날씨각 쇼츠' 검토 | 화면 문구 규칙 | `frontend/src/b/Shorts.tsx` |
| [ ] | **[결정 필요]** 쇼츠 문구 검토: '날씨각 쇼츠', '소리 켜기'/'소리 끄기' | 화면 문구 규칙 | `frontend/src/b/Shorts.tsx` |
| [ ] | 쇼츠 자동재생 아이폰 사파리 실기기 확인 — 소리 꺼도 자동재생을 막는다는 보고 있음 | 헤드리스 크롬에서만 확인 | `frontend/src/b/Shorts.tsx` |
| [ ] | 인스타 운영 관리자 페이지(자동 게시 전 승인 메일) + 초기 이야기형 피드(각 100개·빨래각·소맥각·점수 산정법) | 10/21 회의 | |
| [ ] | 레이어 공유 아이디어 — 사용자 사진 위에 날씨각 정보를 얹어 공유(캐시워크식), 앱 기능으로 | 10/21 회의 (황 본부장 의견, 대표 방향) | |
+61 -18
View File
@@ -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"
/>
)}