🎥 Make videos programmatically with React
document behavior
Jonny Burgere3ad6c356594
diff --git a/packages/docs/docs/player/TableOfContents.tsx b/packages/docs/docs/player/TableOfContents.tsxindex d9c0ce562e..01ead97883 100644--- a/packages/docs/docs/player/TableOfContents.tsx+++ b/packages/docs/docs/player/TableOfContents.tsx@@ -67,6 +67,10 @@ export const PlayerGuide: React.FC = () => {<strong>Custom controls</strong><div>Recipes for custom Play buttons, volume sliders, etc.</div></TOCItem>+ <TOCItem link="/docs/player/media-keys">+ <strong>Media Keys</strong>+ <div>Control what happens when users presses ⏯️</div>+ </TOCItem></Grid></div>);diff --git a/packages/docs/docs/player/api.mdx b/packages/docs/docs/player/api.mdxindex b9080a0685..b9ebbc4e53 100644--- a/packages/docs/docs/player/api.mdx+++ b/packages/docs/docs/player/api.mdx@@ -534,6 +534,11 @@ This allows you to flexibly implement custom UI for the buffer state.Makes the Player render things outside of the canvas. Useful if you have interactive elements in the video such as draggable elements.+### `browserMediaControlsBehavior`<AvailableFrom v="4.0.221" />++Controls what happens when the user presses the Play/Pause button on their keyboard or uses other controls such as Chromes built-in controls.+See [Media Keys Behavior](/docs/player/media-keys) for more information.+## `PlayerRef`You may attach a ref to the player and control it in an imperative manner.diff --git a/packages/docs/docs/player/media-keys.mdx b/packages/docs/docs/player/media-keys.mdxnew file mode 100644index 0000000000..e26ee64681--- /dev/null+++ b/packages/docs/docs/player/media-keys.mdx@@ -0,0 +1,129 @@+---+image: /generated/articles-docs-player-media-keys.png+id: media-keys+sidebar_label: 'Media Keys'+title: 'Media Key Behavior (Web MediaSession API)'+crumb: '@remotion/player'+---++# Media Keys Behavior<AvailableFrom v="4.0.221" />++This document is about the behavior when a user:++- Presses the Play/Pause (⏯️) or Previous track (⏪) button on their keyboard+- Uses other controls such as Chromes built-in controls next to the user avatar to control the playback++These behaviors are controlled by the [`browserMediaControlsBehavior`](/docs/player/player#browsermediacontrolsbehavior) prop.+The underlying Web API used is the [Media Session API](https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API).++## Modes++### `prevent-media-session` (default)++```tsx twoslash+const otherProps = {+ compositionHeight: 720,+ compositionWidth: 1280,+ inputProps: {},+ durationInFrames: 120,+ fps: 30,+ component: () => null,+};+// ---cut---++import {Player} from '@remotion/player';++export const MyComp: React.FC = () => {+ return (+ <Player+ browserMediaControlsBehavior={{+ mode: 'prevent-media-session',+ }}+ {...otherProps}+ />+ );+};+```++In this mode, Remotion will not act on the user's media keys, but map any keybord action to a no-op.+This prevents that the Player is paused but the audio tags get resumed by pressing the Play/Pause button on the keyboard, which was a problem prior to Remotion v4.0.221.++### `register-media-session`++```tsx twoslash+const otherProps = {+ compositionHeight: 720,+ compositionWidth: 1280,+ inputProps: {},+ durationInFrames: 120,+ fps: 30,+ component: () => null,+};+// ---cut---++import {Player} from '@remotion/player';++export const MyComp: React.FC = () => {+ return (+ <Player+ browserMediaControlsBehavior={{+ mode: 'register-media-session',+ }}+ {...otherProps}+ />+ );+};+```++In this mode, Remotion will use the Media Session API to register handlers:++- When the user presses the Play/Pause button on the keyboard+ - Toggle the Remotion Player's state+- When the user presses the Previous track button on the keyboard+ - Seek to the beginning of the video+- When the user presses the Fast Forward button on the keyboard+ - Seek 10 seconds forward+- When the user presses the Rewind button on the keyboard+ - Seek 10 seconds backward++Also, Remotion will react to seeking events and inform the device about the current playback position and duration.++### `do-nothing`++```tsx twoslash+const otherProps = {+ compositionHeight: 720,+ compositionWidth: 1280,+ inputProps: {},+ durationInFrames: 120,+ fps: 30,+ component: () => null,+};+// ---cut---++import {Player} from '@remotion/player';++export const MyComp: React.FC = () => {+ return (+ <Player+ browserMediaControlsBehavior={{+ mode: 'do-nothing',+ }}+ {...otherProps}+ />+ );+};+```++Reverts to the behavior prior to Remotion v4.0.221.+Remotion will not react to any media keys, leaving the browser to handle the media keys.+This leads to the problem that the user can resume any media tag by pressing the Play/Pause button on the keyboard, without the Remotion Player also resuming.++## When using multiple `<Player>`'s++Remotion's `register-media-session` handler is supposed to only work with 1 Player mounted.+It is not defined which Player reacts to the media keys.++Whe working with multiple Players, set one to `do-nothing` mode and the other to `register-media-session` mode to explicitly set the Media Keys for only 1 Player.++If you want all Players to react to the media keys, you need to use `do-nothing` mode and implement this behavior yourself with the Player API.diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 60a72c1ccc..c5109ed144 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -739,6 +739,7 @@ module.exports = {'player/premounting','player/best-practices','player/custom-controls',+ 'player/media-keys',],},diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex e2437f4255..9bacb41c09 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -3737,6 +3737,13 @@ export const articles = [compId: 'articles-docs-player-custom-controls',crumb: '@remotion/player',},+ {+ id: 'media-keys',+ title: 'Media Key Behavior (Web MediaSession API)',+ relativePath: 'docs/player/media-keys.mdx',+ compId: 'articles-docs-player-media-keys',+ crumb: '@remotion/player',+ },{id: 'player/index',title: '@remotion/player',diff --git a/packages/docs/static/generated/articles-docs-player-media-keys.png b/packages/docs/static/generated/articles-docs-player-media-keys.pngnew file mode 100644index 0000000000..7eb9d81ea7Binary files /dev/null and b/packages/docs/static/generated/articles-docs-player-media-keys.png differdiff --git a/packages/player/src/browser-mediasession.ts b/packages/player/src/browser-mediasession.tsindex 0e1128ee2c..304e87a4e1 100644--- a/packages/player/src/browser-mediasession.ts+++ b/packages/player/src/browser-mediasession.ts@@ -25,14 +25,30 @@ export const useBrowserMediaSession = ({const {playing, pause, play, emitter, getCurrentFrame, seek} = usePlayer();useEffect(() => {+ if (!navigator.mediaSession) {+ return;+ }++ if (browserMediaControlsBehavior.mode === 'do-nothing') {+ return;+ }+if (playing) {navigator.mediaSession.playbackState = 'playing';} else {navigator.mediaSession.playbackState = 'paused';}- }, [playing]);+ }, [browserMediaControlsBehavior.mode, playing]);useEffect(() => {+ if (!navigator.mediaSession) {+ return;+ }++ if (browserMediaControlsBehavior.mode === 'do-nothing') {+ return;+ }+const onTimeUpdate = () => {if (!videoConfig) {return;@@ -52,7 +68,13 @@ export const useBrowserMediaSession = ({return () => {emitter.removeEventListener('timeupdate', onTimeUpdate);};- }, [emitter, getCurrentFrame, playbackRate, videoConfig]);+ }, [+ browserMediaControlsBehavior.mode,+ emitter,+ getCurrentFrame,+ playbackRate,+ videoConfig,+ ]);useEffect(() => {if (!navigator.mediaSession) {