import { useImperativeHandle, useRef, type Ref } from 'react'; import { WebView } from 'react-native-webview'; import { SITE_URL } from '@/lib/site'; const YT_ORIGIN = 'https://www.youtube-nocookie.com'; export type PlayerFunc = 'playVideo' | 'seekTo' | 'mute' | 'unMute'; export type PlayerHandle = { command: (func: PlayerFunc, args?: unknown[]) => void }; function embed(id: string) { const q = new URLSearchParams({ autoplay: '1', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: '0', rel: '0', modestbranding: '1', enablejsapi: '1', origin: SITE_URL }); return `${YT_ORIGIN}/embed/${id}?${q}`; } /** * 유튜브 플레이어를 날씨각 주소를 기준으로 한 작은 문서 안 iframe 으로 띄운다. * 앱 웹뷰에서 embed 주소를 바로 열면 보낸 곳(Referer)이 없어 유튜브가 막는다. 웹과 같이 IFrame API 메시지로 재생 상태를 받고 명령한다 */ function html(id: string) { return ` `; } /** 소리 없이 자동재생하는 쇼츠 플레이어. 첫 장면이 나오면 onPlaying (그 전엔 위에 표지를 덮어 둔다) */ export function ShortPlayer({ id, onPlaying, ref }: { id: string; onPlaying: () => void; ref?: Ref }) { const view = useRef(null); useImperativeHandle(ref, () => ({ command: (func, args = []) => view.current?.injectJavaScript(`window.cmd && window.cmd(${JSON.stringify(func)}, ${JSON.stringify(args)}); true;`), })); return ( e.nativeEvent.data === 'playing' && onPlaying()} mediaPlaybackRequiresUserAction={false} allowsInlineMediaPlayback scrollEnabled={false} setSupportMultipleWindows={false} style={{ backgroundColor: 'transparent' }} /> ); }