fix(source): ensure object update doesn't reset player

This commit is contained in:
2026-07-16 12:27:50 +02:00
parent 61ee6238d5
commit 645b226359
3 changed files with 27 additions and 12 deletions
+3 -3
View File
@@ -23,9 +23,9 @@
"typescript": "^7.0.2",
},
"peerDependencies": {
"react": "19.2.7",
"react-native": "0.86.0",
"react-native-nitro-modules": "0.36.1",
"react": ">=19.0.0",
"react-native": ">=0.86.0",
"react-native-nitro-modules": ">=0.36.0",
},
},
"example": {
+13 -2
View File
@@ -1,6 +1,12 @@
import { createPlayer } from "@videojs/react";
import { videoFeatures } from "@videojs/react/video";
import { createContext, type ReactNode, useContext, useEffect } from "react";
import {
createContext,
type ReactNode,
useContext,
useEffect,
useRef,
} from "react";
import { WebOmniPlayer } from "./player.web";
import type { OmniPlayer } from "./types/player";
import type { Source } from "./types/source";
@@ -39,10 +45,15 @@ const PlayerInitializer = ({
}) => {
const store = VideoPlayer.usePlayer();
const player = useLazyRef(() => new WebOmniPlayer(store));
const seekedForSrc = useRef<string | undefined>(undefined);
useEffect(() => {
player.source = source;
if (source.startTime) store.seek(source.startTime).catch(() => {});
const uri = source.src[0]?.uri;
if (uri !== seekedForSrc.current) {
seekedForSrc.current = uri;
if (source.startTime) store.seek(source.startTime)
}
}, [source, store]);
useEffect(() => {
+11 -7
View File
@@ -114,19 +114,23 @@ export const OmniView = ({
false;
const Tech = isHls ? HlsJsVideo : Video;
const config = useMemo<HlsMediaConfig | undefined>(() => {
const headers = src?.headers;
if (!headers || Object.keys(headers).length === 0) return undefined;
return {
const headersRef = useRef(src?.headers);
headersRef.current = src?.headers;
const config = useMemo<HlsMediaConfig>(
() => ({
hlsJs: {
xhrSetup: (xhr: XMLHttpRequest) => {
const headers = headersRef.current;
if (!headers) return;
for (const [key, value] of Object.entries(headers)) {
if (value) xhr.setRequestHeader(key, value);
}
},
},
};
}, [src?.headers]);
}),
[],
);
return (
<VideoPlayer.Container
@@ -137,7 +141,7 @@ export const OmniView = ({
<Tech
ref={ref}
src={src.uri}
config={config}
config={isHls ? config : undefined}
autoPlay={autoplay}
playsInline
crossOrigin="anonymous"