fix: 토스판 쇼츠를 웹과 같이 — 소리 없이 자동재생·재생 버튼 없음·앞뒤 미리 받기·손가락으로 넘기기·소리 켜기 버튼, 재생 신호가 안 오는 토스만 1초 뒤 영상을 보임
This commit is contained in:
1 parent
7ffeecbb4f
commit
8191881f6a
1 file changed
+9
-46
@@ -37,15 +37,14 @@ function useTabBarHeight() {
|
|||||||
|
|
||||||
const YT_ORIGIN = 'https://www.youtube-nocookie.com'
|
const YT_ORIGIN = 'https://www.youtube-nocookie.com'
|
||||||
|
|
||||||
// 토스 웹뷰에는 플레이어가 보내는 재생 상태가 오지 않아, 쇼츠 페이지는 플레이어를 숨기거나 명령하지 않고 유튜브 기본 조작(재생 버튼)과 소리를 켠 채 그대로 보인다.
|
// 토스 웹뷰에는 플레이어가 보내는 재생 상태가 오지 않는다. 그래서 토스는 첫 장면이 나왔는지를 시간으로 보고 표지를 걷는다 (나머지는 웹과 같다)
|
||||||
// 영상 위 터치는 유튜브가 가져가므로 넘기기는 버튼으로
|
|
||||||
const PLAIN = IS_TOSS
|
const PLAIN = IS_TOSS
|
||||||
// 토스는 재생 상태가 안 오므로, 플레이어 문서가 뜨고 이만큼 지나면 첫 장면이 나왔다고 보고 표지를 걷는다 (그 전엔 플레이어의 검은 화면 대신 표지)
|
// 토스는 재생 상태가 안 오므로, 플레이어 문서가 뜨고 이만큼 지나면 첫 장면이 나왔다고 보고 표지를 걷는다 (그 전엔 플레이어의 검은 화면 대신 표지)
|
||||||
const PLAIN_SHOW_MS = 1000
|
const PLAIN_SHOW_MS = 1000
|
||||||
|
|
||||||
function embed(id: string, plain = false, autoplay = true) {
|
function embed(id: string) {
|
||||||
const q = new URLSearchParams({
|
const q = new URLSearchParams({
|
||||||
autoplay: autoplay ? '1' : '0', mute: plain ? '0' : '1', playsinline: '1', loop: '1', playlist: id, controls: plain ? '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,
|
enablejsapi: '1', origin: window.location.origin,
|
||||||
})
|
})
|
||||||
return `${YT_ORIGIN}/embed/${id}?${q}`
|
return `${YT_ORIGIN}/embed/${id}?${q}`
|
||||||
@@ -160,17 +159,6 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
const timers = plainTimers.current
|
const timers = plainTimers.current
|
||||||
return () => timers.forEach((t) => window.clearTimeout(t))
|
return () => timers.forEach((t) => window.clearTimeout(t))
|
||||||
}, [])
|
}, [])
|
||||||
// 토스: 플레이어마다 처음 띄울 때 지금 영상이면 자동재생, 미리 받아 두는 다음 영상은 멈춘 채로. 한 번 정하면 주소가 바뀌어 다시 받지 않게 기억한다
|
|
||||||
const [plainAutoplay, setPlainAutoplay] = useState<ReadonlyMap<number, boolean>>(() => new Map([[start, true], [start + 1, false]]))
|
|
||||||
/** 지금 영상이 n 으로 바뀔 때: 새로 띄울 지금 영상은 자동재생, 다음 영상은 멈춘 채로 (이미 띄운 것은 그대로) */
|
|
||||||
const keepAutoplay = (n: number) =>
|
|
||||||
setPlainAutoplay((m) => {
|
|
||||||
const next = new Map([...m].filter(([i]) => i === n || i === n + 1))
|
|
||||||
if (!next.has(n)) next.set(n, true)
|
|
||||||
if (!next.has(n + 1)) next.set(n + 1, false)
|
|
||||||
return next
|
|
||||||
})
|
|
||||||
const autoplayOnMount = (i: number) => plainAutoplay.get(i) ?? i === at
|
|
||||||
const frames = useRef(new Map<number, HTMLIFrameElement>())
|
const frames = useRef(new Map<number, HTMLIFrameElement>())
|
||||||
const atRef = useRef(start)
|
const atRef = useRef(start)
|
||||||
const soundRef = useRef(false)
|
const soundRef = useRef(false)
|
||||||
@@ -202,12 +190,7 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
if (!box) return
|
if (!box) return
|
||||||
const io = new IntersectionObserver(
|
const io = new IntersectionObserver(
|
||||||
(entries) => {
|
(entries) => {
|
||||||
for (const e of entries) {
|
for (const e of entries) if (e.isIntersecting) setAt(Number((e.target as HTMLElement).dataset.i))
|
||||||
if (!e.isIntersecting) continue
|
|
||||||
const n = Number((e.target as HTMLElement).dataset.i)
|
|
||||||
setAt(n)
|
|
||||||
if (PLAIN) keepAutoplay(n)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{ root: box, threshold: 0.6 },
|
{ root: box, threshold: 0.6 },
|
||||||
)
|
)
|
||||||
@@ -244,13 +227,12 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
atRef.current = at
|
atRef.current = at
|
||||||
paused.current = false
|
paused.current = false
|
||||||
for (const [i, f] of frames.current) {
|
for (const [i, f] of frames.current) {
|
||||||
// 토스는 소리를 켠 채 유튜브 기본 플레이어로 두므로 재생·멈춤만 (소리 명령 없음)
|
|
||||||
if (i === at) {
|
if (i === at) {
|
||||||
if (!PLAIN) command(f, soundRef.current ? 'unMute' : 'mute')
|
command(f, soundRef.current ? 'unMute' : 'mute')
|
||||||
command(f, 'playVideo')
|
command(f, 'playVideo')
|
||||||
} else {
|
} else {
|
||||||
command(f, 'pauseVideo')
|
command(f, 'pauseVideo')
|
||||||
if (!PLAIN) command(f, 'mute')
|
command(f, 'mute')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [at, items])
|
}, [at, items])
|
||||||
@@ -295,10 +277,6 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
box.scrollTo({ top: target * h, behavior: 'smooth' })
|
box.scrollTo({ top: target * h, behavior: 'smooth' })
|
||||||
}
|
}
|
||||||
|
|
||||||
const go = (step: number) => {
|
|
||||||
const box = list.current
|
|
||||||
if (box) box.scrollTo({ top: (at + step) * box.clientHeight, behavior: 'smooth' })
|
|
||||||
}
|
|
||||||
|
|
||||||
const toggleSound = () => {
|
const toggleSound = () => {
|
||||||
const on = !sound
|
const on = !sound
|
||||||
@@ -329,34 +307,23 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
<button type="button" onClick={onClose} aria-label="닫기" className={cx('relative grid size-9 cursor-pointer place-items-center rounded-full bg-white/15 text-white', TAP44)}>
|
<button type="button" onClick={onClose} aria-label="닫기" className={cx('relative grid size-9 cursor-pointer place-items-center rounded-full bg-white/15 text-white', TAP44)}>
|
||||||
<CloseIcon className="size-[18px]" />
|
<CloseIcon className="size-[18px]" />
|
||||||
</button>
|
</button>
|
||||||
{PLAIN ? (
|
|
||||||
<div className="flex gap-2">
|
|
||||||
<button type="button" onClick={() => go(-1)} disabled={at === 0} className="h-9 cursor-pointer rounded-full bg-white/15 px-3.5 text-[13px] font-bold text-white disabled:opacity-40">
|
|
||||||
▲ 이전
|
|
||||||
</button>
|
|
||||||
<button type="button" onClick={() => go(1)} disabled={at === items.length - 1} className="h-9 cursor-pointer rounded-full bg-white/15 px-3.5 text-[13px] font-bold text-white disabled:opacity-40">
|
|
||||||
▼ 다음
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<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">
|
<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 ? '소리 끄기' : '소리 켜기'}
|
{sound ? '소리 끄기' : '소리 켜기'}
|
||||||
</button>
|
</button>
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
<div ref={list} className={cx('relative min-h-0 flex-1 overflow-y-auto [scrollbar-width:none]', !dragging && 'snap-y snap-mandatory')}>
|
<div ref={list} className={cx('relative min-h-0 flex-1 overflow-y-auto [scrollbar-width:none]', !dragging && 'snap-y snap-mandatory')}>
|
||||||
{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="absolute inset-0 size-full object-cover" />
|
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" className="absolute inset-0 size-full object-cover" />
|
||||||
{/* 릴스처럼 다음 영상을 미리 받아 둔다. 토스는 재생 상태가 안 와 멈추게 할 수 없을 수 있어, 지난 영상은 내려 소리가 겹치지 않게 지금·다음만 */}
|
{/* 릴스처럼 앞뒤 영상을 소리 없이 미리 받아 둔다 */}
|
||||||
{(PLAIN ? i === at || i === at + 1 : Math.abs(i - at) <= 1) && (
|
{Math.abs(i - at) <= 1 && (
|
||||||
<iframe
|
<iframe
|
||||||
ref={(f) => {
|
ref={(f) => {
|
||||||
if (f) frames.current.set(i, f)
|
if (f) frames.current.set(i, f)
|
||||||
else frames.current.delete(i)
|
else frames.current.delete(i)
|
||||||
}}
|
}}
|
||||||
src={embed(s.id, PLAIN, !PLAIN || autoplayOnMount(i))}
|
src={embed(s.id)}
|
||||||
title={s.title}
|
title={s.title}
|
||||||
allow="autoplay; encrypted-media; picture-in-picture"
|
allow="autoplay; encrypted-media; picture-in-picture"
|
||||||
// 재생 상태를 받으려면 플레이어에 먼저 '듣고 있다'고 알린다
|
// 재생 상태를 받으려면 플레이어에 먼저 '듣고 있다'고 알린다
|
||||||
@@ -367,8 +334,6 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
setPlainShown((r) => (r.has(i) ? r : new Set(r).add(i)))
|
setPlainShown((r) => (r.has(i) ? r : new Set(r).add(i)))
|
||||||
}, PLAIN_SHOW_MS)
|
}, PLAIN_SHOW_MS)
|
||||||
plainTimers.current.add(t)
|
plainTimers.current.add(t)
|
||||||
// 미리 받아 둔 영상으로 넘어온 뒤 뜬 경우엔 바로 재생
|
|
||||||
if (i === atRef.current) command(e.currentTarget, 'playVideo')
|
|
||||||
}
|
}
|
||||||
e.currentTarget.contentWindow?.postMessage(JSON.stringify({ event: 'listening', id: s.id }), YT_ORIGIN)
|
e.currentTarget.contentWindow?.postMessage(JSON.stringify({ event: 'listening', id: s.id }), YT_ORIGIN)
|
||||||
// 앞뒤 밖으로 밀려나 내려갔다 다시 뜬 플레이어는 첫 장면을 새로 받을 때까지 다시 숨긴다
|
// 앞뒤 밖으로 밀려나 내려갔다 다시 뜬 플레이어는 첫 장면을 새로 받을 때까지 다시 숨긴다
|
||||||
@@ -382,7 +347,6 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
className={cx('absolute inset-0 size-full border-0 transition-opacity duration-200', (PLAIN ? !plainShown.has(i) : !ready.has(i)) && 'opacity-0')}
|
className={cx('absolute inset-0 size-full border-0 transition-opacity duration-200', (PLAIN ? !plainShown.has(i) : !ready.has(i)) && 'opacity-0')}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{!PLAIN && (
|
|
||||||
<div
|
<div
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
onPointerDown={onPointerDown}
|
onPointerDown={onPointerDown}
|
||||||
@@ -395,7 +359,6 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
onClick={() => lastPointer.current !== 'mouse' && togglePause()}
|
onClick={() => lastPointer.current !== 'mouse' && togglePause()}
|
||||||
className={cx('absolute inset-0 touch-pan-y select-none', dragging ? 'cursor-grabbing' : 'cursor-grab')}
|
className={cx('absolute inset-0 touch-pan-y select-none', dragging ? 'cursor-grabbing' : 'cursor-grab')}
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
Reference in new issue
Block a user