mirror of
https://github.com/zoriya/react-native-omni.git
synced 2026-08-16 02:44:51 +00:00
Implement media sessions notification
This commit is contained in:
+20
-4
@@ -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
@@ -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 = () => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Source } from "./source";
|
||||
|
||||
export interface OmniPlayer extends OmniPlayerState {
|
||||
source: Source;
|
||||
showNotification?: boolean;
|
||||
|
||||
play(): void;
|
||||
pause(): void;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export interface OmniViewProps {
|
||||
autoplay?: boolean;
|
||||
showNotification?: boolean;
|
||||
autoPip?: boolean;
|
||||
}
|
||||
|
||||
+4
-11
@@ -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
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user