mirror of
https://github.com/zoriya/react-native-omni.git
synced 2026-08-16 02:44:51 +00:00
fix(chromecast): fix custom subtitle on multiple devices
This commit is contained in:
+25
-33
@@ -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>;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user