연령제한 곡 재생: 유튜브가 퍼가기 플레이어에서 막는(오류 150) 연령제한 영상은 서버가 성인 인증된 계정 쿠키로 소리만 받아 두고(/data/audio, yt-dlp) 게임은 그 파일을 숨긴 <audio>로 재생. 찾기는 소리 크기·하이라이트 수집(로그인 없는 페이지의 성인 인증 화면) + 게임 중 오류 시 age-audio 작업이 다시 확인해 되돌림. 받기 전에는 출제 안 함. prepare·catchup에 videoId 대신 문제마다 새 무작위 주소(/api/audio/<토큰>, Range 지원)라 영상 id가 안 보임. 같은 시각 시작·위치 정지·새로고침 이어 듣기·소리 크기(유튜브 -14 보정 없이) 그대로. 관리자 수집 화면·cli age-audio, Dockerfile에 yt-dlp(버전·체크섬 고정), 수집 CLI가 서버 DB 경로를 물려받게(테스트가 기본 DB를 건드리던 것). 실제 크롬 e2e로 받은 음원+유튜브 곡 섞어 재생 확인
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
* 0.3s after the start → blocked
|
||||
* - catchUp: back in a running question (refresh / reconnect): load muted where everyone is, then play along; a fresh
|
||||
* page first waits for "참가 (소리 켜기)" (the browser blocks sound before a click), the others never stop for it
|
||||
* 연령제한 곡 (YouTube refuses them in embedded players): the server sends its own file's address (`/api/audio/…`) instead of a
|
||||
* video id; an <audio> element plays it behind the same interface, so all of the above works the same.
|
||||
* A fake player (localStorage mq:fakePlayer = "1") lets e2e runs play 30 questions without YouTube.
|
||||
*/
|
||||
import { create } from 'zustand';
|
||||
@@ -140,6 +142,98 @@ function youtubeEngine(l: Listener): Engine {
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- our own audio file (연령제한 곡)
|
||||
/** A short silent file the entry click plays, so this <audio> element may play with sound later (Safari unlocks per element). */
|
||||
const SILENCE = '/silence.m4a';
|
||||
|
||||
function fileEngine(l: Listener): Engine {
|
||||
let a: HTMLAudioElement | null = null;
|
||||
let current: string | null = null;
|
||||
let state = -1;
|
||||
const set = (s: number) => {
|
||||
if (state === s) return;
|
||||
state = s;
|
||||
l.onState?.(s);
|
||||
};
|
||||
const el = () => {
|
||||
if (a) return a;
|
||||
a = new Audio();
|
||||
a.preload = 'auto';
|
||||
a.addEventListener('playing', () => current !== SILENCE && set(1));
|
||||
a.addEventListener('pause', () => current !== SILENCE && set(a!.ended ? 0 : 2));
|
||||
a.addEventListener('waiting', () => current !== SILENCE && set(3));
|
||||
a.addEventListener('ended', () => current !== SILENCE && set(0));
|
||||
// 4 = the address answers 404 (gone) or the file is broken: like a deleted video (100). Others: network, retried by prepare.
|
||||
a.addEventListener('error', () => current && current !== SILENCE && l.onError?.(a!.error?.code === 4 ? 100 : 5));
|
||||
return a;
|
||||
};
|
||||
const start = (x: HTMLAudioElement) =>
|
||||
x.play().catch((e: unknown) => {
|
||||
if ((e as { name?: string })?.name === 'NotAllowedError' && current !== SILENCE) l.onBlocked?.();
|
||||
});
|
||||
return {
|
||||
init: () => Promise.resolve(void el()),
|
||||
unlock() {
|
||||
const x = el();
|
||||
// Inside the click: play something with sound on (volume 0), like the YouTube player's unlock.
|
||||
x.volume = 0;
|
||||
x.muted = false;
|
||||
if (!current) {
|
||||
current = SILENCE;
|
||||
x.src = SILENCE;
|
||||
}
|
||||
void start(x);
|
||||
},
|
||||
load(src, startSec) {
|
||||
const x = el();
|
||||
current = src;
|
||||
state = -1;
|
||||
x.src = src;
|
||||
x.addEventListener('loadedmetadata', () => current === src && (x.currentTime = startSec), { once: true });
|
||||
void start(x);
|
||||
},
|
||||
seek: (sec) => void (el().currentTime = sec),
|
||||
play: () => void start(el()),
|
||||
pause: () => el().pause(),
|
||||
mute: (on) => void (el().muted = on),
|
||||
isMuted: () => el().muted,
|
||||
volume: (v) => void (el().volume = Math.max(0, Math.min(1, v / 100))),
|
||||
time: () => a?.currentTime ?? 0,
|
||||
state: () => (current === SILENCE ? -1 : state),
|
||||
videoId: () => (current === SILENCE ? null : current),
|
||||
};
|
||||
}
|
||||
|
||||
/** The YouTube player for video ids, our <audio> for `/api/audio/…` addresses; one of them is in use at a time. */
|
||||
function routedEngine(yt: Engine, file: Engine): Engine {
|
||||
let cur = yt;
|
||||
return {
|
||||
init: () => Promise.all([yt.init(), file.init()]).then(() => {}),
|
||||
unlock() {
|
||||
// Both, so the next question may use either; the one not in use stops again after the click.
|
||||
yt.unlock();
|
||||
file.unlock();
|
||||
const other = cur === yt ? file : yt;
|
||||
setTimeout(() => other !== cur && other.pause(), 2000);
|
||||
},
|
||||
load(id, startSec) {
|
||||
const next = id.startsWith('/') ? file : yt;
|
||||
if (next !== cur) cur.pause();
|
||||
cur = next;
|
||||
cur.load(id, startSec);
|
||||
},
|
||||
seek: (sec) => cur.seek(sec),
|
||||
play: () => cur.play(),
|
||||
pause: () => cur.pause(),
|
||||
mute: (on) => (yt.mute(on), file.mute(on)),
|
||||
isMuted: () => cur.isMuted(),
|
||||
volume: (v) => (yt.volume(v), file.volume(v)),
|
||||
time: () => cur.time(),
|
||||
state: () => cur.state(),
|
||||
videoId: () => cur.videoId(),
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- fake engine for e2e (no network)
|
||||
function fakeEngine(l: Listener): Engine {
|
||||
let state = -1;
|
||||
@@ -248,7 +342,7 @@ function eng(): Engine {
|
||||
},
|
||||
onBlocked: () => blocked(),
|
||||
};
|
||||
engine = useFake() ? fakeEngine(l) : youtubeEngine(l);
|
||||
engine = useFake() ? fakeEngine(l) : routedEngine(youtubeEngine(l), fileEngine(l));
|
||||
window.mqPlayer = { pos: () => engine!.time(), state: () => engine!.state(), muted: () => engine!.isMuted(), video: () => engine!.videoId() };
|
||||
}
|
||||
return engine;
|
||||
|
||||
@@ -128,7 +128,7 @@ function onMessage(m: ServerMessage): void {
|
||||
return;
|
||||
}
|
||||
case 'prepare':
|
||||
if (m.code === useRoom.getState().code) void player.prepare(m.n, m.videoId, m.startSec, m.endSec, m.gain);
|
||||
if (m.code === useRoom.getState().code) void player.prepare(m.n, m.audio ?? m.videoId!, m.startSec, m.endSec, m.gain);
|
||||
return;
|
||||
case 'play':
|
||||
if (m.code === useRoom.getState().code) player.play(m.n, m.startAt, m.atSec, m.endSec);
|
||||
@@ -137,7 +137,7 @@ function onMessage(m: ServerMessage): void {
|
||||
if (m.code === useRoom.getState().code) player.pause();
|
||||
return;
|
||||
case 'catchup':
|
||||
if (m.code === useRoom.getState().code) void player.catchUp(m.n, m.videoId, m.clipStart, m.startAt, m.atSec, m.endSec, m.gain);
|
||||
if (m.code === useRoom.getState().code) void player.catchUp(m.n, m.audio ?? m.videoId!, m.clipStart, m.startAt, m.atSec, m.endSec, m.gain);
|
||||
return;
|
||||
case 'typing':
|
||||
if (m.code === useRoom.getState().code) useRoom.setState({ typing: m.ids });
|
||||
|
||||
Reference in New Issue
Block a user