Skip to content

🎥 Make videos programmatically with React

implement initialframe

Jonny Burger28ac58ff7731
diff --git a/packages/docs/docs/player/api.md b/packages/docs/docs/player/api.md
index ab60a26861..b47ffbc7c1 100644
--- a/packages/docs/docs/player/api.md
+++ b/packages/docs/docs/player/api.md
@@ -101,7 +101,13 @@ A regular `style` prop for a HTMLDivElement. You can pass a different height and
_optional - available since v3.1.3_
-A HTML class name to be applied to the conainer.
+A HTML class name to be applied to the container.
+
+### `initialFrame`
+
+_optional - available since v3.1.14_
+
+Start the playback from a specific frame. Default `0`. Once the player is mounted, this property cannot be changed.
### `numberOfSharedAudioTags`
diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsx
index 84e0b75564..b111a74558 100644
--- a/packages/player/src/Player.tsx
+++ b/packages/player/src/Player.tsx
@@ -25,6 +25,7 @@ import {PLAYER_CSS_CLASSNAME} from './player-css-classname';
import type {PlayerRef} from './player-methods';
import type {RenderLoading} from './PlayerUI';
import PlayerUI from './PlayerUI';
+import {validateInitialFrame} from './utils/validate-initial-frame';
import {validatePlaybackRate} from './utils/validate-playbackrate';
import {getPreferredVolume, persistVolume} from './volume-persistance';
@@ -58,6 +59,7 @@ export type PlayerProps<T> = {
renderLoading?: RenderLoading;
moveToBeginningWhenEnded?: boolean;
className?: string;
+ initialFrame?: number;
} & PropsIfHasProps<T> &
CompProps<T>;
@@ -93,6 +95,7 @@ export const PlayerFn = <T,>(
playbackRate = 1,
renderLoading,
className,
+ initialFrame,
...componentProps
}: PlayerProps<T>,
ref: MutableRefObject<PlayerRef>
@@ -130,7 +133,8 @@ export const PlayerFn = <T,>(
const component = Internals.useLazyComponent(componentProps);
- const [frame, setFrame] = useState(0);
+ validateInitialFrame({initialFrame, durationInFrames});
+ const [frame, setFrame] = useState(() => initialFrame ?? 0);
const [playing, setPlaying] = useState<boolean>(false);
const [rootId] = useState<string>('player-comp');
const [emitter] = useState(() => new PlayerEmitter());
diff --git a/packages/player/src/utils/validate-initial-frame.ts b/packages/player/src/utils/validate-initial-frame.ts
new file mode 100644
index 0000000000..5b473645da
--- /dev/null
+++ b/packages/player/src/utils/validate-initial-frame.ts
@@ -0,0 +1,51 @@
+export const validateInitialFrame = ({
+ initialFrame,
+ durationInFrames,
+}: {
+ initialFrame: unknown;
+ durationInFrames: unknown;
+}) => {
+ if (typeof durationInFrames !== 'number') {
+ throw new Error(
+ `\`durationInFrames\` must be a number, but is ${JSON.stringify(
+ durationInFrames
+ )}`
+ );
+ }
+
+ if (typeof initialFrame === 'undefined') {
+ return;
+ }
+
+ if (typeof initialFrame !== 'number') {
+ throw new Error(
+ `\`initialFrame\` must be a number, but is ${JSON.stringify(
+ initialFrame
+ )}`
+ );
+ }
+
+ if (Number.isNaN(initialFrame)) {
+ throw new Error(`\`initialFrame\` must be a number, but is NaN`);
+ }
+
+ if (!Number.isFinite(initialFrame)) {
+ throw new Error(`\`initialFrame\` must be a number, but is Infinity`);
+ }
+
+ if (initialFrame % 1 !== 0) {
+ throw new Error(
+ `\`initialFrame\` must be an integer, but is ${JSON.stringify(
+ initialFrame
+ )}`
+ );
+ }
+
+ if (initialFrame > durationInFrames - 1) {
+ throw new Error(
+ `\`initialFrame\` must be less or equal than \`durationInFrames - 1\`, but is ${JSON.stringify(
+ initialFrame
+ )}`
+ );
+ }
+};