π₯ Make videos programmatically with React
better docs
Jonny Burger55dbb1893b82
diff --git a/packages/docs/docs/player/autoplay.mdx b/packages/docs/docs/player/autoplay.mdxindex 89926af077..1ce6d608dc 100644--- a/packages/docs/docs/player/autoplay.mdx+++ b/packages/docs/docs/player/autoplay.mdx@@ -1,21 +1,31 @@---image: /generated/articles-docs-player-autoplay.pngid: autoplay-sidebar_label: "Autoplay"-title: "Combatting autoplay issues"+sidebar_label: 'Autoplay'+title: 'Combatting autoplay issues'slug: /player/autoplay-crumb: "@remotion/player"+crumb: '@remotion/player'----Browsers place restrictions on websites that play audio without user interaction. Read on how to properly use the Remotion Player so you don't run into a browser policy.+Browsers place restrictions on websites that play audio without user interaction.+Read on how to properly use the Remotion Player so you don't run into a browser policy.++The most strict browser is **Mobile Safari**.+If your composition plays well there, you should have no problems elsewhere.## Trigger the play from a user event-Don't autoplay audio on site load, or else browsers will block the audio. Instead, display a play button or use the [`controls`](/docs/player/player#controls) prop.+If you autoplay audio (through an `<Audio>`, `<OffthreadVideo>` or `<Video>` tag) on site load, browsers will block the audio.++The [`autoPlay`](/docs/player/api#autoplay) prop of the [`<Player>`](/docs/api/player) is therefore discouraged if your video contains any audio.++Display a [play button](/docs/player/custom-controls) instead or use the [`controls`](/docs/player/player#controls) prop.## Pass the event to the `play()` or `toggle()` method-The play must be initialized from a user gesture, for example a mouse click. You should get a Javascript event like a `MouseEvent` from it. Pass this event to the [`play()`](/docs/player/player#play) or [`toggle()`](/docs/player/player#toggle) function so audio may play automatically.+If you don't use the default controls, make sure the call to [`.play()`](/docs/player/player#play) is initialized from a user gesture, for example a mouse click.+You should get a Javascript event like a `MouseEvent` from it.+Pass this event to the [`play()`](/docs/player/player#play) or [`toggle()`](/docs/player/player#toggle) function so audio may play automatically.```tsx twoslash// @allowUmdGlobalAccess@@ -24,8 +34,8 @@ export const MyComp = () => <></>;// @filename: index.tsx// ---cut----import { PlayerRef } from "@remotion/player";-import { useRef } from "react";+import {PlayerRef} from '@remotion/player';+import {useRef} from 'react';export const App: React.FC = () => {const ref = useRef<PlayerRef>(null);@@ -33,8 +43,8 @@ export const App: React.FC = () => {return (<buttononClickCapture={(e) => {- const { current } = ref;- // Pass the event to .play(), .pause() or .toggle()+ const {current} = ref;+ // Pass the event to .play() or .toggle()current?.play(e);}}>@@ -44,21 +54,92 @@ export const App: React.FC = () => {};```-:::info+:::notePrefer the `onClickCapture` event instead of the `onClick` event to make the event propagation work properly in Safari.:::-## Use the `numberOfSharedAudioTags` property+## Media that enters the video after the start-If your audio does not enter the video immediately (say the few seconds of the video are silent, but then the audio fades in), it technically doesn't start based on an user integration. To combat this issue, you can use the [`numberOfSharedAudioTags`](/docs/player/player#numberofsharedaudiotags) property. This will play some silent audio on the first play with user interaction, and then reuse that tag to play your deferred audio playback.+Media that contains audio and that plays without user interaction requires special attention.+Consider the following scenario:-You can have as many silent audio tags as you want. Set `numberOfSharedAudioTags={2}` to prepare two shared audio tags. Be mindful: If you set this props and you render more [`<Audio/>`](/docs/audio) than there are shared audio tags, an exception will be thrown.+```tsx twoslash+import {+ AbsoluteFill,+ Audio,+ OffthreadVideo,+ Sequence,+ staticFile,+} from 'remotion';-:::warning-Due to a bug on our side, this prop does not work well [before Remotion v3.1.5](https://github.com/remotion-dev/remotion/issues/723). Upgrade to receive the bugfix.-:::+// ---cut---+export const MyComp = () => {+ return (+ <AbsoluteFill>+ <Audio src={staticFile('audio.mp3')} />+ <Sequence from={120}>+ <Audio src={staticFile('audio2.mp3')} />+ </Sequence>+ <Sequence from={120}>+ <OffthreadVideo src={staticFile('video.mp4')} />+ </Sequence>+ </AbsoluteFill>+ );+};+```++There are three different cases here:++<Step>1</Step> An `<Audio>` tag that plays immediately. β This is fine because we have user interaction.+<div style={{marginTop: -14}} />+<Step>2</Step> An `<Audio>` tag that plays after 120 frames. β οΈ May be subject to browser autoplay policies, but there is a workaround+β [Using the `numberOfSharedAudioTags` prop](/docs/player/autoplay#using-the-numberofsharedaudiotags-prop)+<div style={{marginTop: -14}} />+<Step>3</Step> A `<OffthreadVideo>` tag that plays after 120 frames. β May be subject to browser autoplay policies, which might restrict the video from playing with audio β [Dealing with an autoplay failure](/docs/player/autoplay#dealing-with-an-autoplay-failure)++## Using the `numberOfSharedAudioTags` prop++By default, Remotion will mount 5 auxiliary `<audio>` tags that will "warm" up with the first user interaction.+If an `<Audio>` tag is played after the first user interaction, it will be mounted into one of the auxiliary tags and will be allowed to play.++If you have more than 5 `<Audio>` tags that play at the same time, you can increase the number of auxiliary tags by passing the [`numberOfSharedAudioTags`](/docs/player/player#numberofsharedaudiotags) prop to the `<Player>`, because otherwise an error will be thrown.++## Dealing with an autoplay failure<AvailableFrom v="4.0.187" />++If the browser blocks a video from being autoplayed due to it having sound and not being started from a user interaction:++**By default**: The video will be muted, and played without audio. A console message will be printed.++**Custom behavior**: You may handle an autoplay failure using the [`onAutoPlayError`](/docs/offthreadvideo#onautoplayerror) prop of `<OffthreadVideo>` and `<Video>`:++```tsx twoslash+import {OffthreadVideo, staticFile} from 'remotion';+import type {PlayerRef} from '@remotion/player';++export const MyComp: React.FC<{+ playerRef: React.RefObject<PlayerRef>;+}> = ({playerRef}) => {+ return (+ <OffthreadVideo+ src={staticFile('video.mp4')}+ onAutoPlayError={() => {+ playerRef.current?.pause();+ }}+ />+ );+};+```++In this example, the Player is simply paused.+If the user presses Play again, the video may then start playing with audio again due to it being started with user interaction.++You may show a [poster](/docs/player/player/#renderposter) with a hint - for example "Tap to resume".++## When will a browser restrict autoplay?++It is not entirely deterministic.-## Controlling the behavior of autoplay failures<AvailableFrom v="4.0.187" />+After playing a video with user gesture, Safari will allow more media to be played for some amount of time.+However, the WebKit code shows that it is also dependent on battery saver mode or thermal pressure on the device.-If the browser blocks a video from being autoplayed due to it having sound, the video will be muted and will be retried once.-If you want to handle the error yourself, you can pass a callback to the [`onAutoPlayError`](/docs/offthreadvideo#onautoplayerror) prop of `<OffthreadVideo>` and `<Video>`.+Therefore, it is better to attempt to play the video with audio and handle the failure gracefully.diff --git a/packages/docs/docs/videos/sequence.mdx b/packages/docs/docs/videos/sequence.mdxindex d089eec19d..e6d27ecb81 100644--- a/packages/docs/docs/videos/sequence.mdx+++ b/packages/docs/docs/videos/sequence.mdx@@ -237,6 +237,9 @@ export const Root: React.FC = () => {## Adding premounting+If you only care about the video looking smooth when rendered, you may skip this step.+If you also want smooth preview playback in the Player, consider this:+A video will only load when it is about to be played.To create a smoother preview playback, we should do two things to all videos:@@ -284,6 +287,12 @@ export const VideosInSequence: React.FC<Props> = ({videos}) => {};```+## Browser autoplay policies++Mobile browsers are more aggressive in blocking autoplaying videos that enter after the start of the composition.++If you want to ensure a smooth playback experience for all videos, also [read the notes about browser autoplay behavior](/docs/player/autoplay#media-that-enters-the-video-after-the-start) and customize the behavior if needed.+## See also- [OffthreadVideo](/docs/offthreadvideo)