🎥 Make videos programmatically with React
Custom player controls
Jonny Burger1b729d07c476
diff --git a/packages/docs/docs/player/TableOfContents.tsx b/packages/docs/docs/player/TableOfContents.tsxindex 6f2414d862..d9c0ce562e 100644--- a/packages/docs/docs/player/TableOfContents.tsx+++ b/packages/docs/docs/player/TableOfContents.tsx@@ -63,6 +63,10 @@ export const PlayerGuide: React.FC = () => {<strong>Premounting</strong><div>Mount components earlier to allow them to load</div></TOCItem>+ <TOCItem link="/docs/player/custom-controls">+ <strong>Custom controls</strong>+ <div>Recipes for custom Play buttons, volume sliders, etc.</div>+ </TOCItem></Grid></div>);diff --git a/packages/docs/docs/player/custom-controls.mdx b/packages/docs/docs/player/custom-controls.mdxnew file mode 100644index 0000000000..c2a9f82d2a--- /dev/null+++ b/packages/docs/docs/player/custom-controls.mdx@@ -0,0 +1,91 @@+---+image: /generated/articles-docs-player-custom-controls.png+id: custom-controls+sidebar_label: 'Custom controls'+title: 'Custom controls for the Player'+crumb: '@remotion/player'+---++You may want to implement custom controls for the [`<Player>`](/docs/player/player) component.++There are two approaches:++- Enable the [`controls`](/docs/player/player#controls) prop and granunarly override some or all of the controls inside the Player.+- Disable the [`controls`](/docs/player/player#controls) prop and implement your own controls anywhere on the page.++## Custom inline controls++Use this approach if you:++- Like the default controls but want to customize some of them+- Want the controls to overlay the Player.++Ensure the [`controls`](/docs/player/player#controls) prop is set in the [`<Player/>`](/docs/player/player).+Use the following APIs to customize the individual controls:++- [`renderPlayPauseButton()`](/docs/player/player#renderplaypausebutton)+- [`renderFullscreenButton()`](/docs/player/player#renderfullscreenbutton)+- [`renderMuteButton()`](/docs/player/player#rendermutebutton)+- [`renderVolumeSlider()`](/docs/player/player#rendervolumeslider)++## Controls outside the Player++Use this approach if you:++- Want to implement custom controls anywhere on the page+- Want full controls over the look and behavior of the controls++Ensure the [`controls`](/docs/player/player#controls) prop is not set in the [`<Player/>`](/docs/player/player).+Obtain a [`ref`](https://react.dev/learn/referencing-values-with-refs) of type `PlayerRef` of the [`<Player/>`](/docs/player/player) and use the following starting points to implement your own controls:++### Play / Pause button++```tsx twoslash+import type {PlayerRef} from '@remotion/player';+import React, {SVGProps, useCallback, useEffect} from 'react';++export const PlayPauseButton: React.FC<{+ playerRef: React.RefObject<PlayerRef>;+}> = ({playerRef}) => {+ const [playing, setPlaying] = React.useState(+ () => playerRef.current?.isPlaying() ?? false,+ );++ useEffect(() => {+ const {current} = playerRef;+ if (!current) {+ return;+ }++ const onPlay = () => {+ setPlaying(true);+ };++ const onPause = () => {+ setPlaying(false);+ };++ current.addEventListener('play', onPlay);+ current.addEventListener('pause', onPause);++ return () => {+ current.removeEventListener('play', onPlay);+ current.removeEventListener('pause', onPause);+ };+ }, [playerRef]);++ const onToggle = useCallback(() => {+ playerRef.current?.toggle();+ }, [playerRef]);++ return (+ <button onClick={onToggle} type="button">+ {playing ? 'Pause' : 'Play'}+ </button>+ );+};+```++:::note+The [buffering indicator](/docs/player/buffer-state) is not implemented in this snippet.+:::diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 59c23d57ae..6694a13ddf 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -738,6 +738,7 @@ module.exports = {'player/preloading','player/premounting','player/best-practices',+ 'player/custom-controls',],},diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 6f45dff3c8..7a10cdf664 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -3723,6 +3723,13 @@ export const articles = [compId: 'articles-docs-static-file-remote-urls',crumb: 'Troubleshooting',},+ {+ id: 'custom-controls',+ title: 'Custom controls for the Player',+ relativePath: 'docs/player/custom-controls.mdx',+ compId: 'articles-docs-player-custom-controls',+ crumb: '@remotion/player',+ },{id: 'player/index',title: '@remotion/player',diff --git a/packages/docs/static/generated/articles-docs-player-custom-controls.png b/packages/docs/static/generated/articles-docs-player-custom-controls.pngnew file mode 100644index 0000000000..e23b6630abBinary files /dev/null and b/packages/docs/static/generated/articles-docs-player-custom-controls.png differ