🎥 Make videos programmatically with React
number of shared audio tags option
Jonny Burger1d013268e79c
diff --git a/packages/core/src/RemotionRoot.tsx b/packages/core/src/RemotionRoot.tsxindex bb0d6812b0..59f22a724e 100644--- a/packages/core/src/RemotionRoot.tsx+++ b/packages/core/src/RemotionRoot.tsx@@ -162,7 +162,9 @@ export const RemotionRoot: React.FC = ({children}) => {<TimelineContext.Provider value={timelineContextValue}><SetTimelineContext.Provider value={setTimelineContextValue}><CompositionManager.Provider value={contextValue}>- <SharedAudioContextProvider>{children}</SharedAudioContextProvider>+ <SharedAudioContextProvider numberOfAudioTags={5}>+ {children}+ </SharedAudioContextProvider></CompositionManager.Provider></SetTimelineContext.Provider></TimelineContext.Provider>diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsxindex b1fedffc70..bdf9d18539 100644--- a/packages/core/src/audio/shared-audio-tags.tsx+++ b/packages/core/src/audio/shared-audio-tags.tsx@@ -41,19 +41,19 @@ export const SharedAudioContext = createContext<SharedContext>({},});-const numberOfAudios = 5;--export const SharedAudioContextProvider: React.FC = ({children}) => {+export const SharedAudioContextProvider: React.FC<{+ numberOfAudioTags: number;+}> = ({children, numberOfAudioTags}) => {const [audios, setAudios] = useState<AudioElem[]>([]);const refs = useMemo(() => {- return new Array(numberOfAudios).fill(true).map(() => {+ return new Array(numberOfAudioTags).fill(true).map(() => {return {id: Math.random(), ref: createRef<HTMLAudioElement>()};});- }, []);+ }, [numberOfAudioTags]);const takenAudios = useRef<(false | number)[]>(- new Array(numberOfAudios).fill(false)+ new Array(numberOfAudioTags).fill(false));const registerAudio = useCallback(@@ -61,7 +61,9 @@ export const SharedAudioContextProvider: React.FC = ({children}) => {const firstFreeAudio = takenAudios.current.findIndex((a) => a === false);if (firstFreeAudio === -1) {throw new Error(- 'Trying to register more than XXX audio elements at the same time'+ `Tried to simultaneously mount ${+ numberOfAudioTags + 1+ } <Audio /> tags at the same time. The maximum amount of <Audio /> tags is currently limited to ${numberOfAudioTags} at the same time. Remotion pre-mounts silent audio tags to help avoid browser autoplay restrictions. See https://remotion.dev/docs/player#numberofsharedaudiotags for more information on how to increase this limit.`);}diff --git a/packages/docs/docs/audio.md b/packages/docs/docs/audio.mdindex ad21e39343..7dcbebef22 100644--- a/packages/docs/docs/audio.md+++ b/packages/docs/docs/audio.md@@ -5,10 +5,6 @@ id: audioUsing this component, you can add audio to your video. All audio formats which are supported by Chromium are supported by the component.-:::info-Version 1.x doesn't render audio to an output. Make sure to upgrade to [Remotion 2.0](/docs/2-0-migration) to render audio.-:::-## API / ExampleUse an import or require to load an audio file and pass it to the `src` props of the `<Audio />` component.diff --git a/packages/docs/docs/player.md b/packages/docs/docs/player.mdindex 7b9f2fc1f4..ddf7fb33e3 100644--- a/packages/docs/docs/player.md+++ b/packages/docs/docs/player.md@@ -129,6 +129,12 @@ _optional_A regular `style` prop for a HTMLDivElement. You can pass a different height and width if you would like different dimensions for the player than the original composition dimensions.+### `numberOfSharedAudioTags`++_optional - available since v.2.3.1_++If you use an [`<Audio />`](/docs/audio) tag, it might not play in some browsers (specifically iOS Safari) due to browser autoplay policies. This is why the Remotion Player pre-mounts a set of audio tags with silent audio that get played upon user interaction. These audio tags can then be used to play real audio later and will not be subject to the autoplay policy of the browser. This option controls how many audio tags are being rendered, the default is `5`. If you mount more audio tags than shared audio tags are available, then an error will be thrown.+## `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 4c69a82ac3..cff858470d 100644--- a/packages/player/src/Player.tsx+++ b/packages/player/src/Player.tsx@@ -47,6 +47,7 @@ export type PlayerProps<T> = {clickToPlay?: boolean;doubleClickToFullscreen?: boolean;spaceKeyToPlayOrPause?: boolean;+ numberOfSharedAudioTags?: number;} & PropsIfHasProps<T> &CompProps<T>;@@ -54,6 +55,7 @@ Internals.CSSUtils.injectCSS(Internals.CSSUtils.makeDefaultCSS(`.${PLAYER_CSS_CLASSNAME}`));+// eslint-disable-next-line complexityexport const PlayerFn = <T,>({durationInFrames,@@ -70,6 +72,7 @@ export const PlayerFn = <T,>(clickToPlay,doubleClickToFullscreen = false,spaceKeyToPlayOrPause = true,+ numberOfSharedAudioTags = 5,...componentProps}: PlayerProps<T>,ref: MutableRefObject<PlayerRef>@@ -176,6 +179,16 @@ export const PlayerFn = <T,>();}+ if (+ typeof numberOfSharedAudioTags !== 'number' ||+ numberOfSharedAudioTags % 1 !== 0 ||+ numberOfSharedAudioTags < 0+ ) {+ throw new TypeError(+ `'numberOfSharedAudioTags' must be an positive integer but got '${typeof numberOfSharedAudioTags}' instead`+ );+ }+const setMediaVolumeAndPersist = useCallback((vol: number) => {setMediaVolume(vol);persistVolume(vol);@@ -268,7 +281,9 @@ export const PlayerFn = <T,>(<Internals.SetMediaVolumeContext.Providervalue={setMediaVolumeContextValue}>- <Internals.SharedAudioContextProvider>+ <Internals.SharedAudioContextProvider+ numberOfAudioTags={numberOfSharedAudioTags}+ ><PlayerEventEmitterContext.Provider value={emitter}><PlayerUIref={rootRef}