Implement media sessions notification

This commit is contained in:
2026-04-22 22:51:10 +02:00
parent 6635266fc8
commit a0b43ae009
25 changed files with 173 additions and 219 deletions
+20 -4
View File
@@ -20,10 +20,17 @@ export const useEvent = <Event extends keyof OmniEvents>(
}, [player, event, callback]);
};
// export const usePlayerState = (key: "progress", refresh?: number): OmniPlayerState["progress"];
export const usePlayerState = <Key extends keyof OmniPlayerState>(
export function usePlayerState<Key extends keyof OmniPlayerState>(
key: Key,
): OmniPlayerState[Key] => {
): OmniPlayerState[Key];
export function usePlayerState(
key: "currentTime",
refresh?: number,
): OmniPlayerState["currentTime"];
export function usePlayerState<Key extends keyof OmniPlayerState>(
key: Key,
refresh?: number,
): OmniPlayerState[Key] {
const player = usePlayer() as OmniPlayer;
const [ret, setState] = useState<any>(player[key]);
@@ -47,5 +54,14 @@ export const usePlayerState = <Key extends keyof OmniPlayerState>(
}
}, [player, key]);
if (key === "currentTime") refresh ??= 1;
useEffect(() => {
if (!refresh || refresh <= 0) return;
const int = setInterval(() => {
setState(player[key])
}, refresh);
return () => clearInterval(int);
}, [refresh, key, player]);
return ret;
};
}
+10 -7
View File
@@ -5,28 +5,31 @@ import type { OmniPlayer } from "./types/player";
import type { Source } from "./types/source";
import { useLazyRef } from "./utils/lazy-ref";
const ProviderFactory = NitroModules.createHybridObject<OmniPlayerFactory>(
"OmniPlayerFactory",
);
const ProviderFactory =
NitroModules.createHybridObject<OmniPlayerFactory>("OmniPlayerFactory");
const PlayerCtx = createContext<OmniPlayer>(null!);
export const OmniProvider = ({
children,
source,
showNotification = false,
}: {
source: Source;
children: ReactNode;
showNotification?: boolean;
}) => {
const player = useLazyRef(() => ProviderFactory.createPlayer(source));
useEffect(() => {
player.current.source = source;
player.source = source;
}, [source]);
return (
<PlayerCtx.Provider value={player.current}>{children}</PlayerCtx.Provider>
);
useEffect(() => {
player.showNotification = showNotification;
}, [showNotification]);
return <PlayerCtx.Provider value={player}>{children}</PlayerCtx.Provider>;
};
export const usePlayer = () => {
+1
View File
@@ -2,6 +2,7 @@ import type { Source } from "./source";
export interface OmniPlayer extends OmniPlayerState {
source: Source;
showNotification?: boolean;
play(): void;
pause(): void;
-1
View File
@@ -1,5 +1,4 @@
export interface OmniViewProps {
autoplay?: boolean;
showNotification?: boolean;
autoPip?: boolean;
}
+4 -11
View File
@@ -1,13 +1,6 @@
import { type RefObject, useRef } from "react";
import { useState } from "react";
const empty = Symbol("useLazyRef empty value");
export const useLazyRef = <T>(init: () => T): RefObject<T> => {
const resultRef = useRef<T | typeof empty>(empty);
if (resultRef.current === empty) {
resultRef.current = init();
}
return resultRef as RefObject<T>;
export const useLazyRef = <T>(init: () => T): T => {
const [ret] = useState<T>(init);
return ret
};