π₯ Make videos programmatically with React
enhance docs
Jonny Burger8f150a9a83f2
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 6a521474a0..063e128c1e 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -132,7 +132,7 @@ export * from './spring/index.js';export {staticFile} from './static-file.js';export * from './Still.js';export type {PlayableMediaTag} from './timeline-position-state.js';-export {useBufferState} from './use-buffer';+export {useBufferState} from './use-buffer-state';export {useCurrentFrame} from './use-current-frame.js';export * from './use-video-config.js';export * from './version.js';diff --git a/packages/core/src/use-buffer.ts b/packages/core/src/use-buffer-state.tssimilarity index 100%rename from packages/core/src/use-buffer.tsrename to packages/core/src/use-buffer-state.tsdiff --git a/packages/core/src/use-media-buffering.ts b/packages/core/src/use-media-buffering.tsindex efebdd1eb2..d1b86ee6bf 100644--- a/packages/core/src/use-media-buffering.ts+++ b/packages/core/src/use-media-buffering.ts@@ -1,6 +1,6 @@import type React from 'react';import {useEffect} from 'react';-import {useBufferState} from './use-buffer';+import {useBufferState} from './use-buffer-state';export const useMediaBuffering = (element: React.RefObject<HTMLVideoElement | HTMLAudioElement>,diff --git a/packages/docs/docs/delay-render.md b/packages/docs/docs/delay-render.mdindex f748d1cff1..e49222f831 100644--- a/packages/docs/docs/delay-render.md+++ b/packages/docs/docs/delay-render.md@@ -124,7 +124,7 @@ const MyComp: React.FC = () => {## Failing with an error<AvailableFrom v="3.3.44" />-If your code fails to do an asynchronous operation and you want to cancel the render, you can call `cancelRender()` with an error message. This will automatically cancel all `delayRender()` calls to not further delay the render.+If your code fails to do an asynchronous operation and you want to cancel the render, you can call [`cancelRender()`](/docs/cancel-render) with an error message. This will automatically cancel all `delayRender()` calls to not further delay the render.```tsx twoslash title="MyComposition.tsx"import React, { useEffect, useState } from "react";@@ -145,6 +145,37 @@ export const MyComp: React.FC = () => {};```+## Difference to `useBufferState().delayPlayback()`++[`useBufferState()`](/docs/use-buffer-state) is a different API that comes into play during playback in the [Studio](/docs/terminology/studio) and in the [Player](/docs/terminology/player).++If you are loading data, you might want to both delay the screenshotting of your component during rendering and start a buffering state during Preview, in which case you need to use both APIs together.++```tsx twoslash title="Using delayRender() and delayPlayback() together"+import React from "react";+import { useBufferState, delayRender, continueRender } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBufferState();+ const [handle] = React.useState(() => delayRender());++ React.useEffect(() => {+ const delayHandle = buffer.delayPlayback();++ setTimeout(() => {+ delayHandle.unblock();+ continueRender(handle);+ }, 5000);++ return () => {+ delayHandle.unblock();+ };+ }, []);++ return <></>;+};+```+## See also- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/core/src/delay-render.ts)diff --git a/packages/docs/docs/player/buffer-state.mdx b/packages/docs/docs/player/buffer-state.mdxindex 027186931f..20edb28c30 100644--- a/packages/docs/docs/player/buffer-state.mdx+++ b/packages/docs/docs/player/buffer-state.mdx@@ -47,7 +47,8 @@ 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.+The user might seek to a different portion of the video which is immediately available.+Use the cleanup function of <code>useEffect()</code> to clear the handle when your component is unmounted.```tsx twoslash title="β Causes problems with React strict mode"import React, { useState } from "react";@@ -98,7 +99,35 @@ const MyComp: React.FC = () => {</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.+<a href="/docs/delay-render">delayRender()</a> is a different API which controls when a screenshot is taken during rendering.<br/><br/>++If you are loading data, you might want to delay the screenshotting of your component during rendering and delay the playback of the video in preview, in which case you need to use both APIs together.++```tsx twoslash title="Using delayRender() and delayPlayback() together"+import React from "react";+import { useBufferState, delayRender, continueRender } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBufferState();+ const [handle] = React.useState(() => delayRender());++ React.useEffect(() => {+ const delayHandle = buffer.delayPlayback();++ setTimeout(() => {+ delayHandle.unblock();+ continueRender(handle);+ }, 5000);++ return () => {+ delayHandle.unblock();+ };+ }, []);++ return <></>;+};+```+</details>### Components with built-in bufferingdiff --git a/packages/docs/docs/use-buffer-state.md b/packages/docs/docs/use-buffer-state.mdindex 522ba96cb5..20df53c421 100644--- a/packages/docs/docs/use-buffer-state.md+++ b/packages/docs/docs/use-buffer-state.md@@ -4,6 +4,123 @@ id: use-buffer-statecrumb: "API"---+_available from 4.1 // FIXME_++You can use this hook inside your [composition](/docs/terminology/composition) to retrieve functions that can inform the Player that your component is currently loading data.++```tsx twoslash title="MyComp.tsx"+import React from "react";+import { useBufferState } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBufferState();++ React.useEffect(() => {+ const delayHandle = buffer.delayPlayback();++ setTimeout(() => {+ delayHandle.unblock();+ }, 5000);++ return () => {+ delayHandle.unblock();+ };+ }, []);++ return <></>;+};+```++## API++Returns an object with one method:++### `delayPlayback()`++Tells the Player to delay playback until you call `unblock()`.++## Usage notes++### Handle unmounting++The user might seek to a different portion of the video which is immediately available.+Use the cleanup function of <code>useEffect()</code> to clear the handle when your component is unmounted.++```tsx twoslash title="β Causes problems with React strict mode"+import React, { useState } from "react";+import { useBufferState } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBufferState();+ const [delayHandle] = useState(() => buffer.delayPlayback()); // π₯++ React.useEffect(() => {+ setTimeout(() => {+ delayHandle.unblock();+ }, 5000);+ }, []);++ return <></>;+};+```++### Avoid calling inside `useState()`++While the following implementation works in production, it fails in development if you are inside React Strict mode, because the `useState()` hook is called twice, which causes the first invocation of the buffer to never be cleared and the video to buffer forever.++```tsx twoslash title="β Doesn't clear the buffer handle when seeking to a different portion of a video"+import React, { useState } from "react";+import { useBufferState } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBufferState();+ const [delayHandle] = useState(() => buffer.delayPlayback()); // π₯++ React.useEffect(() => {+ setTimeout(() => {+ delayHandle.unblock();+ }, 5000);++ return () => {+ delayHandle.unblock();+ };+ }, []);++ return <></>;+};+```++### Together with `delayRender()`++[`delayRender()`](/docs/delay-render) is a different API that comes into play during rendering.++If you are loading data, you might want to both delay the screenshotting of your component during rendering and start a buffering state during Preview, in which case you need to use both APIs together.++```tsx twoslash title="Using delayRender() and delayPlayback() together"+import React from "react";+import { useBufferState, delayRender, continueRender } from "remotion";++const MyComp: React.FC = () => {+ const buffer = useBufferState();+ const [handle] = React.useState(() => delayRender());++ React.useEffect(() => {+ const delayHandle = buffer.delayPlayback();++ setTimeout(() => {+ delayHandle.unblock();+ continueRender(handle);+ }, 5000);++ return () => {+ delayHandle.unblock();+ };+ }, []);++ return <></>;+};+```+## See also- [Buffer state](/docs/player/buffer-state)