π₯ Make videos programmatically with React
cool
Jonny Burgera381c9045cd5
diff --git a/packages/docs/docs/player/best-practices.md b/packages/docs/docs/player/best-practices.mdindex e5a04f82ae..4d2754ed9b 100644--- a/packages/docs/docs/player/best-practices.md+++ b/packages/docs/docs/player/best-practices.md@@ -111,6 +111,42 @@ Pass it on to [`.play()`](/docs/player/player#play) and [`.toggle()`](/docs/play[Read here for more details.](/docs/player/autoplay#pass-the-event-to-the-play-or-toggle-method)+## Memoize the `inputProps`++Not memoizing the `inputProps` can cause the whole tree to re-render too many times and create a bottleneck.++```tsx twoslash title="Player.tsx"+// @allowUmdGlobalAccess+// @filename: ./remotion/MyVideo.tsx+export const MyVideo = () => <></>;++// @filename: index.tsx+// ---cut---+import { Player } from "@remotion/player";+import { useState, useMemo } from "react";+import { MyVideo } from "./remotion/MyVideo";++export const App: React.FC = () => {+ const [text, setText] = useState("world");+ const inputProps = useMemo(() => {+ return {+ text,+ };+ }, [text]);++ return (+ <Player+ component={MyVideo}+ durationInFrames={120}+ compositionWidth={1920}+ compositionHeight={1080}+ fps={30}+ inputProps={inputProps}+ />+ );+};+```+## See also- [Handling autoplay in the Player](/docs/player/autoplay)diff --git a/packages/docs/docs/player/buffer-state.mdx b/packages/docs/docs/player/buffer-state.mdxnew file mode 100644index 0000000000..373b25d995--- /dev/null+++ b/packages/docs/docs/player/buffer-state.mdx@@ -0,0 +1,124 @@+---+title: "The Player buffer state"+sidebar_label: Buffer state+crumb: Best practices+---++_available from 4.1 // FIXME_++Just like regular video players, it is possible that the content being displayed in the Player is not yet fully loaded.+In this case, the best practice is to briefly pause the video, let the content load and then resume playback.++Remotion has a native buffer state, which can be used to pause the video when the buffer is empty.++## Mechanic++### Activating the buffer state++A component can tell the player to switch into a buffer state by first using the `useBuffer()` hook and then calling `buffer.delayPlayback()`:++```tsx twoslash title="MyComp.tsx"+import React from "react";+import { useBuffer } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBuffer();++ React.useEffect(() => {+ const delayHandle = buffer.delayPlayback();++ setTimeout(() => {+ delayHandle.unblock();+ }, 5000);++ return () => {+ delayHandle.unblock();+ };+ }, []);++ return <></>;+};+```++To clear the handle, call `.unblock()` on the return value of `delayPlayback()`.++When activating the buffer state, pay attention to the following:++<details>+<summary>Clear the handle when the component unmounts</summary>++Use the cleanup function of <code>useEffect()</code> to clear the handle when your component is unmounted. The user might seek to a different portion of the video which is immediately available.++```tsx twoslash title="β Causes problems with React strict mode"+import React, { useState } from "react";+import { useBuffer } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBuffer();+ const [delayHandle] = useState(() => buffer.delayPlayback()); // π₯++ React.useEffect(() => {+ setTimeout(() => {+ delayHandle.unblock();+ }, 5000);+ }, []);++ return <></>;+};+```++</details>+<details>+<summary>Don't use <code>delayPlayback()</code> inside a <code>useState()</code></summary>++While the following implementation works in production, it fails in React Strict mode, because the `useState()` hook is called twice, which causes the first invocation of the buffer to never be cleared.++```tsx twoslash title="β Doesn't clear the buffer handle when seeking to a different portion of a video"+import React, { useState } from "react";+import { useBuffer } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBuffer();+ const [delayHandle] = useState(() => buffer.delayPlayback()); // π₯++ React.useEffect(() => {+ setTimeout(() => {+ delayHandle.unblock();+ }, 5000);++ return () => {+ delayHandle.unblock();+ };+ }, []);++ return <></>;+};+```++</details>+<details>+<summary>It doesn't replace <code>delayRender()</code></summary>+<a href="/docs/delay-render">delayRender()</a> is a different API which controls when a screenshot is taken during rendering.+</details>++### Components with built-in buffering++You can enable buffering on the following components:++- [`Audio`](/docs/audio)+- [`Video`](/docs/video)+- [`OffthreadVideo`](/docs/offthreadvideo)++// FIXME: How?++### State management++Whether a player is buffering does not internally change the `playing` / `paused` state. Therefore, a player can be in four playback states:++<Step>1</Step> <code>playing</code> and not buffering <br />+<Step>2</Step> <code>playing</code> and buffering <br />+<Step>3</Step> <code>paused</code> and not buffering <br />+<Step>4</Step> <code>paused</code> and buffering <br />+<br />++Only <InlineStep>1</InlineStep> will move the timeline forward.diff --git a/packages/docs/docs/player/player-examples.md b/packages/docs/docs/player/player-examples.mdindex 3531476c07..84dbe8bb9e 100644--- a/packages/docs/docs/player/player-examples.md+++ b/packages/docs/docs/player/player-examples.md@@ -246,12 +246,17 @@ export const MyVideo = () => <></>;// @filename: index.tsx// ---cut---import { Player } from "@remotion/player";-import { useState } from "react";+import { useState, useMemo } from "react";import { MyVideo } from "./remotion/MyVideo";export const App: React.FC = () => {// Connect the state to a text fieldconst [text, setText] = useState("world");+ const inputProps = useMemo(() => {+ return {+ text,+ };+ }, [text]);return (<Player@@ -260,9 +265,7 @@ export const App: React.FC = () => {compositionWidth={1920}compositionHeight={1080}fps={30}- inputProps={{- text,- }}+ inputProps={inputProps}/>);};diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 179b28e404..5386aec1ec 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -568,6 +568,7 @@ module.exports = {"player/current-time","player/preloading","player/best-practices",+ "player/buffer-state","troubleshooting/player-flicker",],},