Merge branch 'feat/youtube-shorts' — 쇼츠 토스판과 같은 코드
This commit is contained in:
commit
f24e790bd7
1 file changed
+22
-3
@@ -37,6 +37,11 @@ function useTabBarHeight() {
|
|||||||
|
|
||||||
const YT_ORIGIN = 'https://www.youtube-nocookie.com'
|
const YT_ORIGIN = 'https://www.youtube-nocookie.com'
|
||||||
|
|
||||||
|
// 토스 웹뷰에는 플레이어가 보내는 재생 상태가 오지 않는다. 그래서 토스는 첫 장면이 나왔는지를 시간으로 보고 표지를 걷는다 (나머지는 웹과 같다)
|
||||||
|
const PLAIN = IS_TOSS
|
||||||
|
// 토스는 재생 상태가 안 오므로, 플레이어 문서가 뜨고 이만큼 지나면 첫 장면이 나왔다고 보고 표지를 걷는다 (그 전엔 플레이어의 검은 화면 대신 표지)
|
||||||
|
const PLAIN_SHOW_MS = 1000
|
||||||
|
|
||||||
function embed(id: string) {
|
function embed(id: string) {
|
||||||
const q = new URLSearchParams({
|
const q = new URLSearchParams({
|
||||||
autoplay: '1', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: '0', rel: '0', modestbranding: '1',
|
autoplay: '1', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: '0', rel: '0', modestbranding: '1',
|
||||||
@@ -147,6 +152,13 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
const tabH = useTabBarHeight()
|
const tabH = useTabBarHeight()
|
||||||
// 첫 장면을 받은 플레이어. 그 전엔 플레이어의 검은 화면 대신 표지를 보인다
|
// 첫 장면을 받은 플레이어. 그 전엔 플레이어의 검은 화면 대신 표지를 보인다
|
||||||
const [ready, setReady] = useState<ReadonlySet<number>>(new Set())
|
const [ready, setReady] = useState<ReadonlySet<number>>(new Set())
|
||||||
|
// 토스: 표지를 걷은 플레이어들. 다음 영상도 미리 띄워 두므로 여럿
|
||||||
|
const [plainShown, setPlainShown] = useState<ReadonlySet<number>>(new Set())
|
||||||
|
const plainTimers = useRef(new Set<number>())
|
||||||
|
useEffect(() => {
|
||||||
|
const timers = plainTimers.current
|
||||||
|
return () => timers.forEach((t) => window.clearTimeout(t))
|
||||||
|
}, [])
|
||||||
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)
|
||||||
@@ -303,8 +315,8 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
<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" />
|
||||||
{/* 웹은 앞뒤 영상까지 미리 띄워 넘길 때 깜빡이지 않게, 토스 앱은 무거워 지금 영상 하나만 */}
|
{/* 릴스처럼 앞뒤 영상을 소리 없이 미리 받아 둔다 */}
|
||||||
{(IS_TOSS ? i === at : 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)
|
||||||
@@ -315,6 +327,13 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
allow="autoplay; encrypted-media; picture-in-picture"
|
allow="autoplay; encrypted-media; picture-in-picture"
|
||||||
// 재생 상태를 받으려면 플레이어에 먼저 '듣고 있다'고 알린다
|
// 재생 상태를 받으려면 플레이어에 먼저 '듣고 있다'고 알린다
|
||||||
onLoad={(e) => {
|
onLoad={(e) => {
|
||||||
|
if (PLAIN) {
|
||||||
|
const t = window.setTimeout(() => {
|
||||||
|
plainTimers.current.delete(t)
|
||||||
|
setPlainShown((r) => (r.has(i) ? r : new Set(r).add(i)))
|
||||||
|
}, PLAIN_SHOW_MS)
|
||||||
|
plainTimers.current.add(t)
|
||||||
|
}
|
||||||
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)
|
||||||
// 앞뒤 밖으로 밀려나 내려갔다 다시 뜬 플레이어는 첫 장면을 새로 받을 때까지 다시 숨긴다
|
// 앞뒤 밖으로 밀려나 내려갔다 다시 뜬 플레이어는 첫 장면을 새로 받을 때까지 다시 숨긴다
|
||||||
setReady((r) => {
|
setReady((r) => {
|
||||||
@@ -324,7 +343,7 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
|||||||
return next
|
return next
|
||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
className={cx('absolute inset-0 size-full border-0 transition-opacity duration-200', !IS_TOSS && !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')}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<div
|
<div
|
||||||
|
|||||||
Reference in new issue
Block a user