🎥 Make videos programmatically with React
Add initialVolume prop to Player
JonnyBurgerd5a704618e84
diff --git a/packages/docs/docs/player/api.mdx b/packages/docs/docs/player/api.mdxindex 08d6a08a46..bd32a56806 100644--- a/packages/docs/docs/player/api.mdx+++ b/packages/docs/docs/player/api.mdx@@ -436,6 +436,8 @@ By default, Remotion uses the key `"remotion.volumePreference"`.This prop is useful if you would like to scope the volume preference to a specific Player instance.+To set a fixed initial volume and **not** use `localStorage` at all, use [`initialVolume`](#initialvolume) instead of this prop.+**Example:**```tsx@@ -446,6 +448,22 @@ This prop is useful if you would like to scope the volume preference to a specif/>```+### `initialVolume?`<AvailableFrom v="4.0.453" />++A number between `0` and `1` that sets the volume when the Player mounts (same range as the volume slider).++If you pass this prop, the Player **does not** read the starting volume from `localStorage` or write volume changes back to `localStorage`. Omit it to keep the default behavior: load and save using [`volumePersistenceKey`](#volumepersistencekey) (or the default key).++**Example:**++```tsx+<Player+ component={MyVideo}+ // ... other props+ initialVolume={0.8}+/>+```+## `PlayerRef`You may attach a ref to the player and control it in an imperative manner.diff --git a/packages/player/src/Player.tsx b/packages/player/src/Player.tsxindex 2cd1f548d9..5962f59856 100644--- a/packages/player/src/Player.tsx+++ b/packages/player/src/Player.tsx@@ -97,6 +97,7 @@ export type PlayerProps<readonly acknowledgeRemotionLicense?: boolean;readonly audioLatencyHint?: AudioContextLatencyCategory;readonly volumePersistenceKey?: string;+ readonly initialVolume?: number;} & CompProps<Props> &PropsIfHasProps<Schema, Props>;@@ -167,6 +168,7 @@ const PlayerFn = <acknowledgeRemotionLicense,audioLatencyHint = 'playback',volumePersistenceKey,+ initialVolume,...componentProps}: PlayerProps<Schema, Props>,ref: MutableRefObject<PlayerRef>,@@ -319,6 +321,27 @@ const PlayerFn = <);}+ if (+ typeof initialVolume !== 'undefined' &&+ typeof initialVolume !== 'number'+ ) {+ throw new TypeError(+ `'initialVolume' must be a number or undefined but got '${typeof initialVolume}' instead`,+ );+ }++ if (+ typeof initialVolume === 'number' &&+ (!Number.isFinite(initialVolume) ||+ Number.isNaN(initialVolume) ||+ initialVolume < 0 ||+ initialVolume > 1)+ ) {+ throw new TypeError(+ `'initialVolume' must be between 0 and 1 but got '${initialVolume}' instead`,+ );+ }+if (typeof numberOfSharedAudioTags !== 'number' ||numberOfSharedAudioTags % 1 !== 0 ||@@ -409,6 +432,7 @@ const PlayerFn = <logLevel={logLevel}audioLatencyHint={audioLatencyHint}volumePersistenceKey={volumePersistenceKey}+ initialVolume={initialVolume}inputProps={actualInputProps}audioEnabled>diff --git a/packages/player/src/SharedPlayerContext.tsx b/packages/player/src/SharedPlayerContext.tsxindex 058a50602e..b4795aab36 100644--- a/packages/player/src/SharedPlayerContext.tsx+++ b/packages/player/src/SharedPlayerContext.tsx@@ -29,6 +29,7 @@ export const SharedPlayerContexts: React.FC<{readonly logLevel: LogLevel;readonly audioLatencyHint: AudioContextLatencyCategory;readonly volumePersistenceKey?: string;+ readonly initialVolume?: number;readonly inputProps: Record<string, unknown>;readonly audioEnabled: boolean;}> = ({@@ -44,9 +45,11 @@ export const SharedPlayerContexts: React.FC<{logLevel,audioLatencyHint,volumePersistenceKey,+ initialVolume,inputProps,audioEnabled,}) => {+ const persistVolumeToStorage = initialVolume === undefined;const compositionManagerContext: CompositionManagerContext = useMemo(() => {const context: CompositionManagerContext = {compositions: [@@ -95,7 +98,9 @@ export const SharedPlayerContexts: React.FC<{const [mediaMuted, setMediaMuted] = useState<boolean>(() => initiallyMuted);const [mediaVolume, setMediaVolume] = useState<number>(() =>- getPreferredVolume(volumePersistenceKey ?? null),+ persistVolumeToStorage+ ? getPreferredVolume(volumePersistenceKey ?? null)+ : initialVolume,);const mediaVolumeContextValue = useMemo((): MediaVolumeContextValue => {@@ -108,9 +113,11 @@ export const SharedPlayerContexts: React.FC<{const setMediaVolumeAndPersist = useCallback((vol: number) => {setMediaVolume(vol);- persistVolume(vol, logLevel, volumePersistenceKey ?? null);+ if (persistVolumeToStorage) {+ persistVolume(vol, logLevel, volumePersistenceKey ?? null);+ }},- [logLevel, volumePersistenceKey],+ [persistVolumeToStorage, logLevel, volumePersistenceKey],);const setMediaVolumeContextValue = useMemo((): SetMediaVolumeContextValue => {diff --git a/packages/player/src/test/validate-prop.test.tsx b/packages/player/src/test/validate-prop.test.tsxindex e5905cb9e2..09078f0fc5 100644--- a/packages/player/src/test/validate-prop.test.tsx+++ b/packages/player/src/test/validate-prop.test.tsx@@ -208,6 +208,59 @@ test('volumePersistenceKey of string should be okay', () => {expect(true).toBe(true);});+test('initialVolume should be okay', () => {+ render(+ <Player+ compositionWidth={500}+ compositionHeight={400}+ fps={30}+ durationInFrames={500}+ component={HelloWorld}+ controls+ showVolumeControls+ initialVolume={0.75}+ />,+ );+ expect(true).toBe(true);+});++test('invalid initialVolume type should give errors', () => {+ expect(() => {+ render(+ <Player+ compositionWidth={500}+ compositionHeight={400}+ fps={30}+ durationInFrames={500}+ component={HelloWorld}+ controls+ showVolumeControls+ // @ts-expect-error+ initialVolume="loud"+ />,+ );+ }).toThrow(+ /'initialVolume' must be a number or undefined but got 'string' instead/,+ );+});++test('initialVolume out of range should give errors', () => {+ expect(() => {+ render(+ <Player+ compositionWidth={500}+ compositionHeight={400}+ fps={30}+ durationInFrames={500}+ component={HelloWorld}+ controls+ showVolumeControls+ initialVolume={2}+ />,+ );+ }).toThrow(/'initialVolume' must be between 0 and 1 but got '2' instead/);+});+test('passing in <Composition /> instance should not be possible', () => {expect(() => {render(