Skip to content

🎥 Make videos programmatically with React

allow to return null in renderposter

Jonny Burger6a5df6374a3e
diff --git a/packages/docs/docs/player/api.md b/packages/docs/docs/player/api.md
index d614f0e78b..18bbb3405b 100644
--- a/packages/docs/docs/player/api.md
+++ b/packages/docs/docs/player/api.md
@@ -239,7 +239,6 @@ import type { RenderPoster } from "@remotion/player";
import { Player } from "@remotion/player";
const MyApp: React.FC = () => {
- // `RenderPoster` type can be imported from "@remotion/player"
const renderPoster: RenderPoster = useCallback(
({ height, width, isBuffering }) => {
if (isBuffering) {
@@ -371,8 +370,9 @@ export const App: React.FC = () => {
};
```
-Since v4.0.XXX // TODO, a `isBuffering` variable is being passed in the callback which is `true` if the Player is in a [buffer state](/docs/player/buffer-state).
-Read about [state management](/docs/player/buffer-state#state-management) to understand how to treat the different states a Player can be in.
+Since v4.0.XXX // TODO, a `isBuffering` variable is being passed in the callback which is `true` if the Player is in a [buffer state](/docs/player/buffer-state). Read about [state management](/docs/player/buffer-state#state-management) to understand how to treat the different states a Player can be in.
+
+Since v4.0.XXX // TODO, You can return `null` in the callback to fall back to the default UI.
### `renderFullscreenButton`<AvailableFrom v="3.2.32" />
diff --git a/packages/docs/docs/player/buffer-state.mdx b/packages/docs/docs/player/buffer-state.mdx
index 85322d8cd5..6b714d8a48 100644
--- a/packages/docs/docs/player/buffer-state.mdx
+++ b/packages/docs/docs/player/buffer-state.mdx
@@ -220,11 +220,11 @@ You can enable buffering on the following components:
## Indicating buffering in the UI
When the Player is buffering, by default the Play button will be replaced with a spinner.
-To prevent a janky UI, this spinner will only be shown after the Player has been in a buffering state in 300ms.
+To prevent a janky UI, this spinner will only be shown after the Player has been in a buffering state for 300ms.
-You may customize the timeout of `300` milliseconds by passing the [`bufferStateDelayInMilliseconds`](/docs/player/api#bufferstatedelayinmilliseconds) prop to the `<Player />` component.
+You may customize the timeout of `300` milliseconds by passing the [`bufferStateDelayInMilliseconds`](/docs/player/player#bufferstatedelayinmilliseconds) prop to the `<Player />` component.
-```tsx twoslash title="Listening to waiting and resume events"
+```tsx twoslash title="Setting the delay until the spinner is shown"
// @allowUmdGlobalAccess
// @filename: ./remotion/MyVideo.tsx
export const MyVideo = () => <></>;
@@ -243,7 +243,86 @@ export const App: React.FC = () => {
compositionWidth={1920}
compositionHeight={1080}
fps={30}
- bufferStateDelayInMilliseconds={1000}
+ bufferStateDelayInMilliseconds={1000} // Or set to `0` to immediately show the spinner
+ />
+ );
+};
+```
+
+To customize the spinner that is shown in place of the Play button, you can pass a [`renderPlayPauseButton()`](/docs/player/player#renderplaypausebutton) prop:
+
+```tsx twoslash title="Rendering a custom spinner inside the Play button"
+const MyPlayButton: React.FC = () => null;
+const MyPauseButton: React.FC = () => null;
+const MySpinner: React.FC = () => null;
+const MyVideo: React.FC = () => null;
+// ---cut---
+import { Player, RenderPlayPauseButton } from "@remotion/player";
+import { useCallback } from "react";
+
+export const App: React.FC = () => {
+ const renderPlayPauseButton: RenderPlayPauseButton = useCallback(
+ ({ playing, isBuffering }) => {
+ if (playing && isBuffering) {
+ return <MySpinner />;
+ }
+
+ return null;
+ },
+ [],
+ );
+
+ return (
+ <Player
+ component={MyVideo}
+ durationInFrames={120}
+ compositionWidth={1920}
+ compositionHeight={1080}
+ fps={30}
+ renderPlayPauseButton={renderPlayPauseButton}
+ />
+ );
+};
+```
+
+To display a loading UI layered on top of the Player (e.g. a spinner), you can set [`showPosterWhenBuffering`](/docs/player/player#showposterwhenbuffering) to `true` and pass a [`renderPoster()`](/docs/player/player#renderposter) prop:
+
+```tsx twoslash title="Rendering a custom spinner on top of the Player"
+import { useCallback } from "react";
+import { AbsoluteFill } from "remotion";
+
+const Component: React.FC = () => null;
+const Spinner: React.FC = () => null;
+
+// ---cut---
+
+import type { RenderPoster } from "@remotion/player";
+import { Player } from "@remotion/player";
+
+const MyApp: React.FC = () => {
+ const renderPoster: RenderPoster = useCallback(({ isBuffering }) => {
+ if (isBuffering) {
+ return (
+ <AbsoluteFill
+ style={{ justifyContent: "center", alignItems: "center" }}
+ >
+ <Spinner />
+ </AbsoluteFill>
+ );
+ }
+
+ return null;
+ }, []);
+
+ return (
+ <Player
+ fps={30}
+ component={Component}
+ durationInFrames={100}
+ compositionWidth={1080}
+ compositionHeight={1080}
+ renderPoster={renderPoster}
+ showPosterWhenBuffering
/>
);
};
diff --git a/packages/player/src/PlayerControls.tsx b/packages/player/src/PlayerControls.tsx
index 311375d152..b4140c6df6 100644
--- a/packages/player/src/PlayerControls.tsx
+++ b/packages/player/src/PlayerControls.tsx
@@ -18,7 +18,7 @@ import type {Size} from './utils/use-element-size.js';
export type RenderPlayPauseButton = (props: {
playing: boolean;
isBuffering: boolean;
-}) => ReactNode;
+}) => ReactNode | null;
export type RenderFullscreenButton = (props: {
isFullscreen: boolean;
@@ -287,7 +287,12 @@ export const Controls: React.FC<{
renderPlayPauseButton({
playing: player.playing,
isBuffering: buffering,
- })
+ }) ?? (
+ <DefaultPlayPauseButton
+ buffering={buffering}
+ playing={player.playing}
+ />
+ )
)}
</button>
{showVolumeControls ? (