fix(chromecast): fix custom subtitle on multiple devices

This commit is contained in:
2026-07-25 15:27:27 +02:00
parent 096ee2c41d
commit f5ba273b1b
+25 -33
View File
@@ -73,42 +73,34 @@ const PlayerInitializer = ({
}, [showNotification]); }, [showNotification]);
useEffect(() => { useEffect(() => {
const ctx = window.cast?.framework?.CastContext?.getInstance(); let media: chrome.cast.media.Media | null = null;
if (!window.cast?.framework || !ctx) return;
let attached: cast.framework.CastSession | null = null; const onMediaUpdate = () => {
const data = media?.customData as { subtitle?: unknown } | undefined;
const onSessionChange = () => { player.applyRemoteSubtitle(
const session = ctx.getCurrentSession(); typeof data?.subtitle === "string" ? data.subtitle : null,
if (!session || session === attached) return;
attached = session;
session.addMessageListener(
"urn:x-cast:dev.zoriya.omni",
(_ns: string, message: string) => {
let data: unknown;
try {
data = JSON.parse(message);
} catch {
return;
}
if (data && typeof data === "object" && "subtitle" in data) {
const id = (data as { subtitle?: unknown }).subtitle;
player.applyRemoteSubtitle(typeof id === "string" ? id : null);
}
},
); );
}; };
onSessionChange();
ctx.addEventListener( const syncMedia = () => {
window.cast?.framework.CastContextEventType.SESSION_STATE_CHANGED, const next =
onSessionChange, window.cast.framework.CastContext.getInstance()
); .getCurrentSession()
return () => ?.getMediaSession() ?? null;
ctx.removeEventListener( if (next === media) return;
window.cast?.framework.CastContextEventType.SESSION_STATE_CHANGED, media?.removeUpdateListener(onMediaUpdate);
onSessionChange, media = next;
); media?.addUpdateListener(onMediaUpdate);
}, []); onMediaUpdate();
};
const unsubscribe = store.subscribe(syncMedia);
syncMedia();
return () => {
unsubscribe();
media?.removeUpdateListener(onMediaUpdate);
};
}, [store]);
return <PlayerCtx.Provider value={player}>{children}</PlayerCtx.Provider>; return <PlayerCtx.Provider value={player}>{children}</PlayerCtx.Provider>;
}; };