|
| 1 | +// src/hooks/useFigureTrailer.ts |
| 2 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 3 | +import { getPublicTrailerUrl } from '../utils/public/publicMediaUrl'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Plays a figure's ~50-second page trailer (rendered audio, served from R2). |
| 7 | + * |
| 8 | + * Deliberately a standalone HTML5 <audio> player, decoupled from the |
| 9 | + * conversation/TTS audio pipeline — a figure trailer is one short clip, and a |
| 10 | + * minimal surface is the most predictable thing on mobile. Tap-to-play only, |
| 11 | + * never autoplay: play() runs inside the tap gesture, which is what iOS needs. |
| 12 | + */ |
| 13 | + |
| 14 | +// Each trailer is on R2 as both webm (~370 KB, Opus) and mp3 (~2.1 MB). webm is |
| 15 | +// ~6x smaller, so prefer it — but only when the browser is confident it can |
| 16 | +// play webm audio. iOS Safari reports no confidence and so gets mp3, which is |
| 17 | +// universally supported. Probed once and cached. |
| 18 | +let cachedExt: 'webm' | 'mp3' | null = null; |
| 19 | +function trailerExt(): 'webm' | 'mp3' { |
| 20 | + if (cachedExt) return cachedExt; |
| 21 | + let webm = false; |
| 22 | + try { |
| 23 | + const probe = document.createElement('audio'); |
| 24 | + webm = |
| 25 | + probe.canPlayType('audio/webm; codecs="opus"') === 'probably' || |
| 26 | + probe.canPlayType('audio/webm; codecs="vorbis"') === 'probably'; |
| 27 | + } catch { |
| 28 | + webm = false; |
| 29 | + } |
| 30 | + cachedExt = webm ? 'webm' : 'mp3'; |
| 31 | + return cachedExt; |
| 32 | +} |
| 33 | + |
| 34 | +export type TrailerStatus = 'idle' | 'loading' | 'playing' | 'error'; |
| 35 | + |
| 36 | +export interface FigureTrailerControls { |
| 37 | + /** Figure id whose trailer is currently active (loading or playing), or null. */ |
| 38 | + activeId: string | null; |
| 39 | + status: TrailerStatus; |
| 40 | + /** Play this figure's trailer — or pause it if it is already this figure's. */ |
| 41 | + toggle: (figureId: string, language: string) => void; |
| 42 | + /** Stop and reset — call on figure change, modal close, or unmount. */ |
| 43 | + stop: () => void; |
| 44 | +} |
| 45 | + |
| 46 | +export function useFigureTrailer(): FigureTrailerControls { |
| 47 | + const audioRef = useRef<HTMLAudioElement | null>(null); |
| 48 | + const [activeId, setActiveId] = useState<string | null>(null); |
| 49 | + const [status, setStatus] = useState<TrailerStatus>('idle'); |
| 50 | + |
| 51 | + const stop = useCallback(() => { |
| 52 | + const audio = audioRef.current; |
| 53 | + if (audio) { |
| 54 | + audio.pause(); |
| 55 | + try { audio.currentTime = 0; } catch { /* ignore — seek before load */ } |
| 56 | + } |
| 57 | + setActiveId(null); |
| 58 | + setStatus('idle'); |
| 59 | + }, []); |
| 60 | + |
| 61 | + // Never leave a trailer playing behind a closed / unmounted modal. |
| 62 | + useEffect(() => { |
| 63 | + return () => { |
| 64 | + const audio = audioRef.current; |
| 65 | + if (audio) { |
| 66 | + audio.pause(); |
| 67 | + audio.removeAttribute('src'); |
| 68 | + audio.load(); |
| 69 | + } |
| 70 | + audioRef.current = null; |
| 71 | + }; |
| 72 | + }, []); |
| 73 | + |
| 74 | + const toggle = useCallback( |
| 75 | + (figureId: string, language: string) => { |
| 76 | + const audio = audioRef.current; |
| 77 | + |
| 78 | + // Tap on the figure that is already active → pause it. |
| 79 | + if (audio && activeId === figureId && status !== 'idle' && status !== 'error') { |
| 80 | + audio.pause(); |
| 81 | + setStatus('idle'); |
| 82 | + setActiveId(null); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const el = audio ?? new Audio(); |
| 87 | + if (!audioRef.current) { |
| 88 | + el.preload = 'none'; |
| 89 | + audioRef.current = el; |
| 90 | + } |
| 91 | + |
| 92 | + el.pause(); |
| 93 | + el.onplaying = () => setStatus('playing'); |
| 94 | + el.onended = () => { setStatus('idle'); setActiveId(null); }; |
| 95 | + el.onerror = () => { setStatus('error'); setActiveId(null); }; |
| 96 | + el.src = getPublicTrailerUrl(figureId, language === 'de' ? 'de' : 'en', trailerExt()); |
| 97 | + |
| 98 | + setActiveId(figureId); |
| 99 | + setStatus('loading'); |
| 100 | + |
| 101 | + // play() must run inside the tap gesture (iOS) — toggle is always an onClick. |
| 102 | + const result = el.play(); |
| 103 | + if (result && typeof result.catch === 'function') { |
| 104 | + result.catch(() => { setStatus('error'); setActiveId(null); }); |
| 105 | + } |
| 106 | + }, |
| 107 | + [activeId, status] |
| 108 | + ); |
| 109 | + |
| 110 | + return { activeId, status, toggle, stop }; |
| 111 | +} |
0 commit comments