Skip to content

🎥 Make videos programmatically with React

API page

Jonny Burger2357e842e790
diff --git a/packages/core/src/audio/shared-audio-tags.tsx b/packages/core/src/audio/shared-audio-tags.tsx
index 69c1eeec2a..8598f90d29 100644
--- a/packages/core/src/audio/shared-audio-tags.tsx
+++ b/packages/core/src/audio/shared-audio-tags.tsx
@@ -70,7 +70,7 @@ export const SharedAudioContextProvider: React.FC<{
throw new Error(
`Tried to simultaneously mount ${
numberOfAudioTags + 1
- } <Audio /> tags at the same time. With the current settings, the maximum amount of <Audio /> tags is 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.`
+ } <Audio /> tags at the same time. With the current settings, the maximum amount of <Audio /> tags is limited to ${numberOfAudioTags} at the same time. Remotion pre-mounts silent audio tags to help avoid browser autoplay restrictions. See /docs/player/autoplay#use-the-numberofsharedaudiotags-property for more information on how to increase this limit.`
);
}
@@ -116,18 +116,16 @@ export const SharedAudioContextProvider: React.FC<{
const updateAudio = useCallback((id: number, aud: RemotionAudioProps) => {
setAudios((prevAudios) => {
- return prevAudios.map(
- (prevA): AudioElem => {
- if (prevA.id === id) {
- return {
- ...prevA,
- props: aud,
- };
- }
-
- return prevA;
+ return prevAudios.map((prevA): AudioElem => {
+ if (prevA.id === id) {
+ return {
+ ...prevA,
+ props: aud,
+ };
}
- );
+
+ return prevA;
+ });
});
}, []);
@@ -175,19 +173,17 @@ export const SharedAudioContextProvider: React.FC<{
export const useSharedAudio = (aud: RemotionAudioProps) => {
const ctx = useContext(SharedAudioContext);
- const [elem] = useState(
- (): AudioElem => {
- if (ctx && ctx.numberOfAudioTags > 0) {
- return ctx.registerAudio(aud);
- }
-
- return {
- el: React.createRef<HTMLAudioElement>(),
- id: Math.random(),
- props: aud,
- };
+ const [elem] = useState((): AudioElem => {
+ if (ctx && ctx.numberOfAudioTags > 0) {
+ return ctx.registerAudio(aud);
}
- );
+
+ return {
+ el: React.createRef<HTMLAudioElement>(),
+ id: Math.random(),
+ props: aud,
+ };
+ });
useEffect(() => {
return () => {
diff --git a/packages/docs/blog/2021-09-27-remotion-2-4.md b/packages/docs/blog/2021-09-27-remotion-2-4.md
index 4901c174df..cbe565f1e7 100644
--- a/packages/docs/blog/2021-09-27-remotion-2-4.md
+++ b/packages/docs/blog/2021-09-27-remotion-2-4.md
@@ -50,7 +50,7 @@ We want to add new features to the Preview, but not bloat Remotion by adding ton
- **New `<Series />` component**: Introduced in 2.3.2, we added a new [`<Series />`](/docs/series) component that helps you layout many sequences in a row. See this post ([Instagram](https://www.instagram.com/p/CTMCMX1q-Lm/) | [Twitter](https://twitter.com/JNYBGR/status/1432225492116123651)) for additional infographic explanation!
-- **Better handling for browser autoplay policies**: If you use the [`<Player />`](/docs/player) and include audio in it, you might hit a browser limitation where audio cannot be played because of a browsers autoplay policy. Remotion can now avoid some of those scenarios by playing some silent audio when the user actively triggers a play on the Remotion Player. If you then later in the video want to play some audio, Remotion will route that audio to an `<audio />` tag that was already playing silent audio and was already freed from the playback restrictions of the browser. You can control the amount of silent audio tags that Remotion should place using the [`numberOfSharedAudioTags`](/docs/player#numberofsharedaudiotags) prop.
+- **Better handling for browser autoplay policies**: If you use the [`<Player />`](/docs/player) and include audio in it, you might hit a browser limitation where audio cannot be played because of a browsers autoplay policy. Remotion can now avoid some of those scenarios by playing some silent audio when the user actively triggers a play on the Remotion Player. If you then later in the video want to play some audio, Remotion will route that audio to an `<audio />` tag that was already playing silent audio and was already freed from the playback restrictions of the browser. You can control the amount of silent audio tags that Remotion should place using the [`numberOfSharedAudioTags`](/docs/player/api#numberofsharedaudiotags) prop.
- **Better handling of invalid dimensions**: It turns out that MP4s can only have even dimensions. So while a 1000x1000px MP4 is completely fine, a 999x999px MP4 is not possible according to the spec. Instead of erroring out when rendering, we now warn you early using a new ESLint rule, and also when you use the "New composition" dialog.
diff --git a/packages/docs/blog/2021-10-26-remotion-2-5.md b/packages/docs/blog/2021-10-26-remotion-2-5.md
index 00c7b473d7..d8d299b17d 100644
--- a/packages/docs/blog/2021-10-26-remotion-2-5.md
+++ b/packages/docs/blog/2021-10-26-remotion-2-5.md
@@ -32,7 +32,7 @@ _Thanks to [Brian Pederson](https://github.com/remotion-dev/remotion/pull/638) f
You can now change the playback rate in the editor and play a video in slow-motion, in fast-forward, and even in reverse! We support speeds between -4x and 4x. This makes debugging animations that don’t look clean much easier.
-It also works in the `<Player />`! See the new [`playbackRate`](/docs/player#playbackrate) prop and we also added a [`ratechange`](/docs/player#ratechange) event - just like the native HTML5 Video element.
+It also works in the `<Player />`! See the new [`playbackRate`](/docs/player/api#playbackrate) prop and we also added a [`ratechange`](/docs/player/api#ratechange) event - just like the native HTML5 Video element.
<img src="/img/playbackrate.gif" />
diff --git a/packages/docs/docs/player/api.md b/packages/docs/docs/player/api.md
new file mode 100644
index 0000000000..cce200aefe
--- /dev/null
+++ b/packages/docs/docs/player/api.md
@@ -0,0 +1,470 @@
+---
+id: api
+title: "API"
+---
+
+## `<Player />`
+
+A React component which takes the following props
+
+### `component` or `lazyComponent`
+
+Pass a React component in directly **or** pass a function that returns a dynamic import. Passing neither or both of the props is an error.
+
+### `durationInFrames`
+
+The duration of the video in frames. Must be an integer and greater than 0.
+
+### `fps`
+
+The frame rate of the video. Must be a number.
+
+### `compositionWidth`
+
+The width of the composition in pixels.
+
+### `compositionHeight`
+
+The height of the composition in pixels.
+
+### `loop`
+
+_optional_
+
+Whether the video should restart when it ends. Default `false`.
+
+### `autoPlay`
+
+_optional_
+
+Whether the video should start immediately after loaded. Default `false`.
+
+### `controls`
+
+_optional_
+
+Whether the video should display a seek bar and a play/pause button. Default `false.`
+
+### `showVolumeControls`
+
+_optional_
+
+Whether the video should display a volume slider and a mute button. Only has an effect if `controls` is also set to true. Default `true`.
+
+### `allowFullscreen`
+
+_optional_
+
+Whether the video can go fullscreen. By default `true`.
+
+### `clickToPlay`
+
+_optional_
+
+A boolean property defining whether you can play, pause or resume the video with a single click into the player. Default `true` if `controls` are true, otherwise `false`.
+
+### `doubleClickToFullscreen`
+
+_optional_
+
+A boolean property defining whether you can go fullscreen and exit fullscreen in the video with double click into the player. If enabled, clicking on the video once will delay pausing the video for 200ms to wait for a possible second click. Default `false`.
+
+### `spaceKeyToPlayOrPause`
+
+_optional_
+
+A boolean property defining whether you can play or pause a video using space key. If enabled, playing the video and subsequently pressing the space key pauses and resumes the video. Only works if `controls` is true. Default `true`.
+
+### `inputProps`
+
+_optional_
+
+Pass props to the component that you have specified using the `component` prop. The Typescript definition takes the shape of the props that you have given to your `component`. Default `undefined`.
+
+### `style`
+
+_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.
+
+If you'd like to opt out of this behavior, you can pass `0` to mount native audio tags simultaneously as you mount Remotion's [`<Audio />`](/docs/audio) tags.
+
+Once you have set this prop, you cannot change it anymore or an error will be thrown.
+
+### `playbackRate`
+
+_optional_
+
+A number between -4 and 4 (excluding 0) for the speed that the Player will run the media.
+
+A `playbackRate` of `2` means the video plays twice as fast. A playbackRate of `0.5` means the video plays twice as slow. A playbackRate of `-1` means the video plays in reverse. Note that [`<Audio/>`](/docs/audio) and [`<Video/>`](/docs/video) tags cannot be played in reverse, this is a browser limitation.
+
+Default `1`.
+
+### `errorFallback`
+
+_optional_
+
+A callback for rendering a custom error message. See [Handling errors](#handling-errors) section for an example.
+
+### `renderLoading`
+
+_optional_
+
+A callback function that allows you to return a custom UI that gets displayed while the player is loading.
+
+The first parameter contains the `height` and `width` of the player as it gets rendered.
+
+```tsx twoslash
+import { Player, RenderLoading } from "@remotion/player";
+import { useCallback } from "react";
+import { AbsoluteFill } from "remotion";
+
+const Component: React.FC = () => null;
+
+// ---cut---
+
+const MyApp: React.FC = () => {
+ // `RenderLoading` type can be imported from "@remotion/player"
+ const renderLoading: RenderLoading = useCallback(({ height, width }) => {
+ return (
+ <AbsoluteFill style={{ backgroundColor: "gray" }}>
+ Loading player ({height}x{width})
+ </AbsoluteFill>
+ );
+ }, []);
+
+ return (
+ <Player
+ fps={30}
+ component={Component}
+ durationInFrames={100}
+ compositionWidth={1080}
+ compositionHeight={1080}
+ renderLoading={renderLoading}
+ />
+ );
+};
+```
+
+:::info
+A player needs to be loaded if it contains elements that use React Suspense, or if the `lazyComponent` props is being used.
+:::
+
+## `PlayerRef`
+
+You may attach a ref to the player and control it in an imperative manner.
+
+```tsx twoslash {15}
+// @allowUmdGlobalAccess
+
+// @filename: MyComposition.tsx
+export const MyComposition: React.FC = () => null;
+
+// @filename: index.tsx
+// ---cut---
+import { useEffect, useRef } from "react";
+import { Player, PlayerRef } from "@remotion/player";
+import { MyComposition } from "./MyComposition";
+
+const MyComp: React.FC = () => {
+ const playerRef = useRef<PlayerRef>(null);
+
+ useEffect(() => {
+ if (playerRef.current) {
+ console.log(playerRef.current.getCurrentFrame());
+ }
+ }, []);
+
+ return (
+ <Player
+ ref={playerRef}
+ durationInFrames={30}
+ compositionWidth={1080}
+ compositionHeight={1080}
+ fps={30}
+ component={MyComposition}
+ // Many other optional props are available.
+ />
+ );
+};
+```
+
+The following methods are available on the player ref:
+
+### `pause()`
+
+Pause the video. Nothing happens if the video is already paused.
+
+### `play()`
+
+Play the video. Nothing happens if the video is already playing.
+
+If you play the video from a user gesture, pass the `SyntheticEvent` in as an argument so [browser autoplay restrictions do not apply](/docs/player/autoplay).
+
+### `toggle()`
+
+Pauses the video if it's playing. Plays the video if it's paused.
+
+If you play the video from a user gesture, pass the `SyntheticEvent` in as an argument so [browser autoplay restrictions do not apply](/docs/player/autoplay).
+
+### `getCurrentFrame()`
+
+Gets the current postition expressed as the current frame. Divide by the `fps` you passed to get the time in seconds.
+
+### `isPlaying()`
+
+_Available from v2.5.7_
+
+Returns a boolean indicating whether the video is playing.
+
+### `getContainerNode()`
+
+_Available from v2.4.2_
+
+Gets the container `HTMLDivElement` of the player. Useful if you'd like to manually attach listeners to the player element.
+
+```tsx twoslash
+import { useRef, useEffect } from "react";
+import { PlayerRef } from "@remotion/player";
+// ---cut---
+const playerRef = useRef<PlayerRef>(null);
+
+useEffect(() => {
+ if (!playerRef.current) {
+ return;
+ }
+ const container = playerRef.current.getContainerNode();
+ if (!container) {
+ return;
+ }
+
+ const onClick = () => {
+ console.log("player got clicked");
+ };
+
+ container.addEventListener("click", onClick);
+ return () => {
+ container.removeEventListener("click", onClick);
+ };
+}, []);
+```
+
+### `mute()`
+
+Mutes the video.
+
+### `unmute()`
+
+Unmutes the video.
+
+### `getVolume()`
+
+Gets the volume of the video. The volume is a value between 0 and 1 and is initially 1.
+
+### `setVolume()`
+
+#### Arguments
+
+- `volume`: `number`
+
+Set the volume of the video. Must be a value between 0 and 1, otherwise an exception will be thrown.
+
+### `isMuted()`
+
+Returns a boolean specifying whether the video is muted.
+
+### `seekTo()`
+
+#### Arguments
+
+- `frame`: `number`
+
+Move the position in the video to a specific frame. If the video is playing, it will pause for a brief moment, then start playing again after the seek is completed.
+
+### `isFullscreen()`
+
+Returns a boolean whether the video is currently playing in fullscreen.
+
+_To observe the fullscreen state and react to changes, listen to the [`fullscreenchange`](https://developer.mozilla.org/en-US/docs/Web/API/Document/fullscreenchange_event) event on the global document._
+
+### `requestFullscreen()`
+
+Requests the video to go to fullscreen. This method throws if the `allowFullscreen` prop is false or the browser doesn't support allow the player to go into fullscreen.
+
+### `exitFullscreen()`
+
+Exit fullscreen mode.
+
+### `addEventListener()`
+
+Start listening to an event. See the [Events](#events) section to see the function signature and the available events.
+
+### `removeEventListener()`
+
+Stop listening to an event. See the [Events](#events) section to see the function signature and the available events.
+
+## Events
+
+Using a [player ref](#playerref), you can bind event listeners to get notified of certain events of the player.
+
+```tsx twoslash
+import { useRef, useEffect } from "react";
+import { PlayerRef } from "@remotion/player";
+// ---cut---
+const playerRef = useRef<PlayerRef>(null);
+
+useEffect(() => {
+ if (!playerRef.current) {
+ return;
+ }
+ playerRef.current.addEventListener("play", () => {
+ console.log("playing");
+ });
+ playerRef.current.addEventListener("ratechange", () => {
+ console.log("ratechange");
+ });
+ playerRef.current.addEventListener("pause", () => {
+ console.log("pausing");
+ });
+
+ // See below for difference between `seeked` and `timeupdate`
+ playerRef.current.addEventListener("seeked", (e) => {
+ console.log("seeked to " + e.detail.frame);
+ });
+ playerRef.current.addEventListener("timeupdate", (e) => {
+ console.log("time has updated to " + e.detail.frame);
+ });
+ playerRef.current.addEventListener("ended", (e) => {
+ console.log("ended");
+ });
+ playerRef.current.addEventListener("error", (e) => {
+ console.log("error", e.detail.error);
+ });
+}, []);
+```
+
+### `seeked`
+
+Fired when the time position changes. You may get the current frame by reading it from `e.detail.frame`.
+
+```tsx twoslash
+import { useRef, useEffect } from "react";
+import { PlayerRef } from "@remotion/player";
+const playerRef = useRef<PlayerRef>(null);
+if (!playerRef.current) {
+ throw new Error();
+}
+// ---cut---
+playerRef.current.addEventListener("seeked", (e) => {
+ console.log("seeked to " + e.detail.frame); // seeked to 120
+});
+```
+
+This event fires on every single frame update. Don't update your UI based on this event as it will cause a lot of rerenders. Use the [`timeupdate`](#timeupdate) event instead.
+
+### `ended`
+
+Fires when the video has ended and looping is disabled.
+
+### `play`
+
+Fires when the video has started playing or has resumed from a pause.
+
+### `ratechange`
+
+Fires when the [`playbackRate`](#playbackrate) has changed.
+
+### `pause`
+
+Fires when the video has paused or ended.
+
+### `timeupdate`
+
+Fires periodically when the video is playing. Unlike the [`seeked`](#seeked) event, frames are skipped, and the event is throttled to only fire a few times a second.
+
+```tsx twoslash
+import { useRef, useEffect } from "react";
+import { PlayerRef } from "@remotion/player";
+const playerRef = useRef<PlayerRef>(null);
+if (!playerRef.current) {
+ throw new Error();
+}
+// ---cut---
+playerRef.current.addEventListener("timeupdate", (e) => {
+ console.log("current frame is " + e.detail.frame); // current frame is 120
+});
+```
+
+### `error`
+
+Fires when an error or uncaught exception has happened in the video.
+
+You may get the error by reading the `e.detail.error` value:
+
+```tsx twoslash
+import { useRef, useEffect } from "react";
+import { PlayerRef } from "@remotion/player";
+const ref = useRef<PlayerRef>(null);
+// ---cut---
+ref.current?.addEventListener("error", (e) => {
+ console.log("error ", e.detail.error); // error [Error: undefined is not a function]
+});
+```
+
+## Handling errors
+
+Since videos are written in React, they are prone to crashing.
+When a video throws an exception, you may handle the error using the [`error` event](#error).
+The video will unmount and show an error UI, but the host application (The React app which is embedding the player) will not crash.
+It is up to you to handle the error and to re-mount the video (for example by changing the `key` prop in React).
+
+This feature is implemented using an [error boundary](https://reactjs.org/docs/error-boundaries.html), so only errors in the render function will be caught. Errors in event handlers and asynchronous code will not be reported and will not cause the video to unmount.
+
+You can customize the error message that is shown if a video crashes:
+
+```tsx twoslash
+import { Player, ErrorFallback } from "@remotion/player";
+import { useCallback } from "react";
+import { AbsoluteFill } from "remotion";
+
+const Component: React.FC = () => null;
+
+// ---cut---
+
+const MyApp: React.FC = () => {
+ // `ErrorFallback` type can be imported from "@remotion/player"
+ const errorFallback: ErrorFallback = useCallback(({ error }) => {
+ return (
+ <AbsoluteFill
+ style={{
+ backgroundColor: "yellow",
+ justifyContent: "center",
+ alignItems: "center",
+ }}
+ >
+ Sorry about this! An error occurred: {error.message}
+ </AbsoluteFill>
+ );
+ }, []);
+
+ return (
+ <Player
+ fps={30}
+ component={Component}
+ durationInFrames={100}
+ compositionWidth={1080}
+ compositionHeight={1080}
+ errorFallback={errorFallback}
+ />
+ );
+};
+```
diff --git a/packages/docs/docs/player/autoplay.md b/packages/docs/docs/player/autoplay.md
index 355683317d..2980568343 100644
--- a/packages/docs/docs/player/autoplay.md
+++ b/packages/docs/docs/player/autoplay.md
@@ -9,7 +9,7 @@ Browsers place restrictions on websites that play audio without user interaction
## Trigger the play from a user event
-Don't autoplay audio on site load, or else browsers will block the audio. Instead, display a play button or use the [`controls`](/docs/player#controls) prop.
+Don't autoplay audio on site load, or else browsers will block the audio. Instead, display a play button or use the [`controls`](/docs/player/api#controls) prop.
## Pass the event to the `play()` or `toggle()` method
@@ -45,7 +45,7 @@ export const App: React.FC = () => {
## Use the `numberOfSharedAudioTags` property
-If your audio does not enter the video immediately (say the few seconds of the video are silent, but then the audio fades in), it technically doesn't start based on an user interation. To combat this issue, you can use the [`numberOfSharedAudioTags`](/docs/player#numberofsharedaudiotags) property. This will play some silent audio on the first play with user interaction, and then reuse that tag to play your deferred audio playback.
+If your audio does not enter the video immediately (say the few seconds of the video are silent, but then the audio fades in), it technically doesn't start based on an user interation. To combat this issue, you can use the [`numberOfSharedAudioTags`](/docs/player/api#numberofsharedaudiotags) property. This will play some silent audio on the first play with user interaction, and then reuse that tag to play your deferred audio playback.
You can have as many silent audio tags as you want. Set `numberOfSharedAudioTags={2}` to prepare two shared audio tags. Be mindful: If you set this props and you render more [`<Audio/>`](/docs/audio) than there are shared audio tags, an exception will be thrown.
diff --git a/packages/docs/docs/player/player-integration.md b/packages/docs/docs/player/player-integration.md
index 9cac6d180c..72b4891f84 100644
--- a/packages/docs/docs/player/player-integration.md
+++ b/packages/docs/docs/player/player-integration.md
@@ -1,7 +1,7 @@
---
id: integration
-sidebar_label: "Integrate into your app"
-title: "Integrating Player into your app"
+sidebar_label: "Code sharing"
+title: "Code sharing"
slug: /player/integration
---
diff --git a/packages/docs/docs/player/player.md b/packages/docs/docs/player/player.md
index 319c339c63..b2338534f5 100644
--- a/packages/docs/docs/player/player.md
+++ b/packages/docs/docs/player/player.md
@@ -45,469 +45,3 @@ yarn add @remotion/player
</TabItem>
</Tabs>
-
-## `<Player />`
-
-A React component which takes the following props
-
-### `component` or `lazyComponent`
-
-Pass a React component in directly **or** pass a function that returns a dynamic import. Passing neither or both of the props is an error.
-
-### `durationInFrames`
-
-The duration of the video in frames. Must be an integer and greater than 0.
-
-### `fps`
-
-The frame rate of the video. Must be a number.
-
-### `compositionWidth`
-
-The width of the composition in pixels.
-
-### `compositionHeight`
-
-The height of the composition in pixels.
-
-### `loop`
-
-_optional_
-
-Whether the video should restart when it ends. Default `false`.
-
-### `autoPlay`
-
-_optional_
-
-Whether the video should start immediately after loaded. Default `false`.
-
-### `controls`
-
-_optional_
-
-Whether the video should display a seek bar and a play/pause button. Default `false.`
-
-### `showVolumeControls`
-
-_optional_
-
-Whether the video should display a volume slider and a mute button. Only has an effect if `controls` is also set to true. Default `true`.
-
-### `allowFullscreen`
-
-_optional_
-
-Whether the video can go fullscreen. By default `true`.
-
-### `clickToPlay`
-
-_optional_
-
-A boolean property defining whether you can play, pause or resume the video with a single click into the player. Default `true` if `controls` are true, otherwise `false`.
-
-### `doubleClickToFullscreen`
-
-_optional_
-
-A boolean property defining whether you can go fullscreen and exit fullscreen in the video with double click into the player. If enabled, clicking on the video once will delay pausing the video for 200ms to wait for a possible second click. Default `false`.
-
-### `spaceKeyToPlayOrPause`
-
-_optional_
-
-A boolean property defining whether you can play or pause a video using space key. If enabled, playing the video and subsequently pressing the space key pauses and resumes the video. Only works if `controls` is true. Default `true`.
-
-### `inputProps`
-
-_optional_
-
-Pass props to the component that you have specified using the `component` prop. The Typescript definition takes the shape of the props that you have given to your `component`. Default `undefined`.
-
-### `style`
-
-_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.
-
-If you'd like to opt out of this behavior, you can pass `0` to mount native audio tags simultaneously as you mount Remotion's [`<Audio />`](/docs/audio) tags.
-
-Once you have set this prop, you cannot change it anymore or an error will be thrown.
-
-### `playbackRate`
-
-_optional_
-
-A number between -4 and 4 (excluding 0) for the speed that the Player will run the media.
-
-A `playbackRate` of `2` means the video plays twice as fast. A playbackRate of `0.5` means the video plays twice as slow. A playbackRate of `-1` means the video plays in reverse. Note that [`<Audio/>`](/docs/audio) and [`<Video/>`](/docs/video) tags cannot be played in reverse, this is a browser limitation.
-
-Default `1`.
-
-### `errorFallback`
-
-_optional_
-
-A callback for rendering a custom error message. See [Handling errors](#handling-errors) section for an example.
-
-### `renderLoading`
-
-_optional_
-
-A callback function that allows you to return a custom UI that gets displayed while the player is loading.
-
-The first parameter contains the `height` and `width` of the player as it gets rendered.
-
-```tsx twoslash
-import { Player, RenderLoading } from "@remotion/player";
-import { useCallback } from "react";
-import { AbsoluteFill } from "remotion";
-
-const Component: React.FC = () => null;
-
-// ---cut---
-
-const MyApp: React.FC = () => {
- // `RenderLoading` type can be imported from "@remotion/player"
- const renderLoading: RenderLoading = useCallback(({ height, width }) => {
- return (
- <AbsoluteFill style={{ backgroundColor: "gray" }}>
- Loading player ({height}x{width})
- </AbsoluteFill>
- );
- }, []);
-
- return (
- <Player
- fps={30}
- component={Component}
- durationInFrames={100}
- compositionWidth={1080}
- compositionHeight={1080}
- renderLoading={renderLoading}
- />
- );
-};
-```
-
-:::info
-A player needs to be loaded if it contains elements that use React Suspense, or if the `lazyComponent` props is being used.
-:::
-
-## `PlayerRef`
-
-You may attach a ref to the player and control it in an imperative manner.
-
-```tsx twoslash {15}
-// @allowUmdGlobalAccess
-
-// @filename: MyComposition.tsx
-export const MyComposition: React.FC = () => null;
-
-// @filename: index.tsx
-// ---cut---
-import { useEffect, useRef } from "react";
-import { Player, PlayerRef } from "@remotion/player";
-import { MyComposition } from "./MyComposition";
-
-const MyComp: React.FC = () => {
- const playerRef = useRef<PlayerRef>(null);
-
- useEffect(() => {
- if (playerRef.current) {
- console.log(playerRef.current.getCurrentFrame());
- }
- }, []);
-
- return (
- <Player
- ref={playerRef}
- durationInFrames={30}
- compositionWidth={1080}
- compositionHeight={1080}
- fps={30}
- component={MyComposition}
- // Many other optional props are available.
- />
- );
-};
-```
-
-The following methods are available on the player ref:
-
-### `pause()`
-
-Pause the video. Nothing happens if the video is already paused.
-
-### `play()`
-
-Play the video. Nothing happens if the video is already playing.
-
-If you play the video from a user gesture, pass the `SyntheticEvent` in as an argument so [browser autoplay restrictions do not apply](/docs/player/autoplay).
-
-### `toggle()`
-
-Pauses the video if it's playing. Plays the video if it's paused.
-
-If you play the video from a user gesture, pass the `SyntheticEvent` in as an argument so [browser autoplay restrictions do not apply](/docs/player/autoplay).
-
-### `getCurrentFrame()`
-
-Gets the current postition expressed as the current frame. Divide by the `fps` you passed to get the time in seconds.
-
-### `isPlaying()`
-
-_Available from v2.5.7_
-
-Returns a boolean indicating whether the video is playing.
-
-### `getContainerNode()`
-
-_Available from v2.4.2_
-
-Gets the container `HTMLDivElement` of the player. Useful if you'd like to manually attach listeners to the player element.
-
-```tsx twoslash
-import { useRef, useEffect } from "react";
-import { PlayerRef } from "@remotion/player";
-// ---cut---
-const playerRef = useRef<PlayerRef>(null);
-
-useEffect(() => {
- if (!playerRef.current) {
- return;
- }
- const container = playerRef.current.getContainerNode();
- if (!container) {
- return;
- }
-
- const onClick = () => {
- console.log("player got clicked");
- };
-
- container.addEventListener("click", onClick);
- return () => {
- container.removeEventListener("click", onClick);
- };
-}, []);
-```
-
-### `mute()`
-
-Mutes the video.
-
-### `unmute()`
-
-Unmutes the video.
-
-### `getVolume()`
-
-Gets the volume of the video. The volume is a value between 0 and 1 and is initially 1.
-
-### `setVolume()`
-
-#### Arguments
-
-- `volume`: `number`
-
-Set the volume of the video. Must be a value between 0 and 1, otherwise an exception will be thrown.
-
-### `isMuted()`
-
-Returns a boolean specifying whether the video is muted.
-
-### `seekTo()`
-
-#### Arguments
-
-- `frame`: `number`
-
-Move the position in the video to a specific frame. If the video is playing, it will pause for a brief moment, then start playing again after the seek is completed.
-
-### `isFullscreen()`
-
-Returns a boolean whether the video is currently playing in fullscreen.
-
-_To observe the fullscreen state and react to changes, listen to the [`fullscreenchange`](https://developer.mozilla.org/en-US/docs/Web/API/Document/fullscreenchange_event) event on the global document._
-
-### `requestFullscreen()`
-
-Requests the video to go to fullscreen. This method throws if the `allowFullscreen` prop is false or the browser doesn't support allow the player to go into fullscreen.
-
-### `exitFullscreen()`
-
-Exit fullscreen mode.
-
-### `addEventListener()`
-
-Start listening to an event. See the [Events](#events) section to see the function signature and the available events.
-
-### `removeEventListener()`
-
-Stop listening to an event. See the [Events](#events) section to see the function signature and the available events.
-
-## Events
-
-Using a [player ref](#playerref), you can bind event listeners to get notified of certain events of the player.
-
-```tsx twoslash
-import { useRef, useEffect } from "react";
-import { PlayerRef } from "@remotion/player";
-// ---cut---
-const playerRef = useRef<PlayerRef>(null);
-
-useEffect(() => {
- if (!playerRef.current) {
- return;
- }
- playerRef.current.addEventListener("play", () => {
- console.log("playing");
- });
- playerRef.current.addEventListener("ratechange", () => {
- console.log("ratechange");
- });
- playerRef.current.addEventListener("pause", () => {
- console.log("pausing");
- });
-
- // See below for difference between `seeked` and `timeupdate`
- playerRef.current.addEventListener("seeked", (e) => {
- console.log("seeked to " + e.detail.frame);
- });
- playerRef.current.addEventListener("timeupdate", (e) => {
- console.log("time has updated to " + e.detail.frame);
- });
- playerRef.current.addEventListener("ended", (e) => {
- console.log("ended");
- });
- playerRef.current.addEventListener("error", (e) => {
- console.log("error", e.detail.error);
- });
-}, []);
-```
-
-### `seeked`
-
-Fired when the time position changes. You may get the current frame by reading it from `e.detail.frame`.
-
-```tsx twoslash
-import { useRef, useEffect } from "react";
-import { PlayerRef } from "@remotion/player";
-const playerRef = useRef<PlayerRef>(null);
-if (!playerRef.current) {
- throw new Error();
-}
-// ---cut---
-playerRef.current.addEventListener("seeked", (e) => {
- console.log("seeked to " + e.detail.frame); // seeked to 120
-});
-```
-
-This event fires on every single frame update. Don't update your UI based on this event as it will cause a lot of rerenders. Use the [`timeupdate`](#timeupdate) event instead.
-
-### `ended`
-
-Fires when the video has ended and looping is disabled.
-
-### `play`
-
-Fires when the video has started playing or has resumed from a pause.
-
-### `ratechange`
-
-Fires when the [`playbackRate`](#playbackrate) has changed.
-
-### `pause`
-
-Fires when the video has paused or ended.
-
-### `timeupdate`
-
-Fires periodically when the video is playing. Unlike the [`seeked`](#seeked) event, frames are skipped, and the event is throttled to only fire a few times a second.
-
-```tsx twoslash
-import { useRef, useEffect } from "react";
-import { PlayerRef } from "@remotion/player";
-const playerRef = useRef<PlayerRef>(null);
-if (!playerRef.current) {
- throw new Error();
-}
-// ---cut---
-playerRef.current.addEventListener("timeupdate", (e) => {
- console.log("current frame is " + e.detail.frame); // current frame is 120
-});
-```
-
-### `error`
-
-Fires when an error or uncaught exception has happened in the video.
-
-You may get the error by reading the `e.detail.error` value:
-
-```tsx twoslash
-import { useRef, useEffect } from "react";
-import { PlayerRef } from "@remotion/player";
-const ref = useRef<PlayerRef>(null);
-// ---cut---
-ref.current?.addEventListener("error", (e) => {
- console.log("error ", e.detail.error); // error [Error: undefined is not a function]
-});
-```
-
-## Handling errors
-
-Since videos are written in React, they are prone to crashing.
-When a video throws an exception, you may handle the error using the [`error` event](#error).
-The video will unmount and show an error UI, but the host application (The React app which is embedding the player) will not crash.
-It is up to you to handle the error and to re-mount the video (for example by changing the `key` prop in React).
-
-This feature is implemented using an [error boundary](https://reactjs.org/docs/error-boundaries.html), so only errors in the render function will be caught. Errors in event handlers and asynchronous code will not be reported and will not cause the video to unmount.
-
-You can customize the error message that is shown if a video crashes:
-
-```tsx twoslash
-import { Player, ErrorFallback } from "@remotion/player";
-import { useCallback } from "react";
-import { AbsoluteFill } from "remotion";
-
-const Component: React.FC = () => null;
-
-// ---cut---
-
-const MyApp: React.FC = () => {
- // `ErrorFallback` type can be imported from "@remotion/player"
- const errorFallback: ErrorFallback = useCallback(({ error }) => {
- return (
- <AbsoluteFill
- style={{
- backgroundColor: "yellow",
- justifyContent: "center",
- alignItems: "center",
- }}
- >
- Sorry about this! An error occurred: {error.message}
- </AbsoluteFill>
- );
- }, []);
-
- return (
- <Player
- fps={30}
- component={Component}
- durationInFrames={100}
- compositionWidth={1080}
- compositionHeight={1080}
- errorFallback={errorFallback}
- />
- );
-};
-```
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 9aad01accd..fb4910f487 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -6,6 +6,7 @@ module.exports = {
href: "/docs",
},
"player/player",
+ "player/api",
"player/examples",
"player/integration",
"player/autoplay",