Skip to content

🎥 Make videos programmatically with React

mute/unmute working

umungobungoa194a3050a16
diff --git a/packages/docs/src/components/Demo/Demo.tsx b/packages/docs/src/components/Demo/Demo.tsx
index dd14818559..60a1ca0b8b 100644
--- a/packages/docs/src/components/Demo/Demo.tsx
+++ b/packages/docs/src/components/Demo/Demo.tsx
@@ -1,12 +1,19 @@
import {useColorMode} from '@docusaurus/theme-common';
import type {PlayerRef} from '@remotion/player';
import {Player} from '@remotion/player';
-import React, {type CSSProperties, useEffect, useRef, useState} from 'react';
+import React, {
+ type CSSProperties,
+ useCallback,
+ useEffect,
+ useRef,
+ useState,
+} from 'react';
import type {LocationAndTrending} from '../../remotion/HomepageVideo/Comp';
import {
HomepageVideoComp,
getDataAndProps,
} from '../../remotion/HomepageVideo/Comp';
+import type {EmojiPosition} from '../../remotion/HomepageVideo/emoji/EmojiCard';
import {ActionRow} from './ActionRow';
import {PlayerControls} from './PlayerControls';
import styles from './player.module.css';
@@ -27,6 +34,22 @@ export const Demo: React.FC = () => {
const [isFullscreen, setIsFullscreen] = useState(false);
const [cardOrder, setCardOrder] = useState([0, 1, 2, 3]);
+ const activeTranslationStyle =
+ 'transform 0.2s ease-in, opacity 0.2s ease-in-out';
+
+ const [emojiPositions, setEmojiPositions] = useState<EmojiPosition>({
+ prev: 'melting',
+ current: 'partying-face',
+ next: 'fire',
+ translation: 0,
+ translationStyle: activeTranslationStyle,
+ });
+
+ const [audioState, setAudioState] = useState({
+ volume: 0.75,
+ isMuted: true,
+ });
+
const playerWrapper: CSSProperties = {
border: '2px solid ' + strokeColor,
borderBottom: '4px solid ' + strokeColor,
@@ -55,6 +78,63 @@ export const Demo: React.FC = () => {
setCardOrder(newCardOrder);
};
+ const onClickLeft = useCallback(() => {
+ setEmojiPositions((c) => {
+ return {
+ ...c,
+ translation: -33.3,
+ translationStyle: activeTranslationStyle,
+ };
+ });
+ // after the animation is done, we need to update the emoji contents
+ setTimeout(() => {
+ setEmojiPositions((c) => {
+ return {
+ prev: c.next,
+ current: c.prev,
+ next: c.current,
+ translation: 0,
+ translationStyle: undefined,
+ };
+ });
+ }, 200);
+ }, []);
+
+ const onClickRight = useCallback(() => {
+ setEmojiPositions((c) => {
+ return {
+ ...c,
+ translation: 33.3,
+ translationStyle: activeTranslationStyle,
+ };
+ });
+ setTimeout(() => {
+ setEmojiPositions((c) => {
+ return {
+ prev: c.current,
+ current: c.next,
+ next: c.prev,
+ translation: 0,
+ translationStyle: '',
+ };
+ });
+ }, 200);
+ }, []);
+
+ const updateAudioVolume = (volume: number) => {
+ setAudioState({
+ volume,
+ isMuted: false,
+ });
+ };
+
+ const updateAudioMute = (isMuted: boolean) => {
+ setAudioState((v) => ({
+ volume: v.volume === 0 ? 0.5 : v.volume, // if volume was previously 0, set it to 0.75
+ isMuted,
+ }));
+ };
+
return (
<div>
<br />
@@ -77,6 +157,7 @@ export const Demo: React.FC = () => {
...playerWrapper,
touchAction: 'none', // prevent page from scrolling when dragging children on mobile
}}
+ initiallyMuted
inputProps={{
theme: colorMode,
onToggle: () => {
@@ -84,11 +165,22 @@ export const Demo: React.FC = () => {
},
cardOrder,
updateCardOrder,
+ emojiPositions,
+ onClickLeft,
+ onClickRight,
+ audioVolume: audioState,
...data,
}}
loop
/>
- <PlayerControls playerRef={ref} durationInFrames={120} fps={30} />
+ <PlayerControls
+ playerRef={ref}
+ durationInFrames={120}
+ fps={30}
+ updateAudioVolume={updateAudioVolume}
+ updateAudioMute={updateAudioMute}
+ audioState={audioState}
+ />
</>
) : (
<>
diff --git a/packages/docs/src/components/Demo/PlayerControls.tsx b/packages/docs/src/components/Demo/PlayerControls.tsx
index bb963722ec..f33e17c527 100644
--- a/packages/docs/src/components/Demo/PlayerControls.tsx
+++ b/packages/docs/src/components/Demo/PlayerControls.tsx
@@ -11,7 +11,20 @@ export const PlayerControls: React.FC<{
playerRef: React.RefObject<PlayerRef>;
durationInFrames: number;
fps: number;
-}> = ({playerRef, durationInFrames, fps}) => {
+ updateAudioVolume: (volume: number) => void;
+ updateAudioMute: (isMuted: boolean) => void;
+ audioState: {
+ volume: number;
+ isMuted: boolean;
+ };
+}> = ({
+ playerRef,
+ durationInFrames,
+ fps,
+ updateAudioVolume,
+ updateAudioMute,
+ audioState,
+}) => {
return (
<div className={styles['controls-wrapper']}>
<div className={styles['start-controls']}>
@@ -31,7 +44,12 @@ export const PlayerControls: React.FC<{
/>
</div>
<div className={styles['end-controls']}>
- <PlayerVolume playerRef={playerRef} />
+ <PlayerVolume
+ playerRef={playerRef}
+ updateAudioVolume={updateAudioVolume}
+ updateAudioMute={updateAudioMute}
+ audioState={audioState}
+ />
<PlayerFullscreen playerRef={playerRef} />
</div>
</div>
diff --git a/packages/docs/src/components/Demo/PlayerVolume.tsx b/packages/docs/src/components/Demo/PlayerVolume.tsx
index 7281b16e79..e78250b420 100644
--- a/packages/docs/src/components/Demo/PlayerVolume.tsx
+++ b/packages/docs/src/components/Demo/PlayerVolume.tsx
@@ -1,12 +1,16 @@
import type {PlayerRef} from '@remotion/player';
-import React, {useCallback, useEffect, useState} from 'react';
+import React, {useCallback, useEffect} from 'react';
import {IsMutedIcon, NotMutedIcon} from '../../icons/arrows';
export const PlayerVolume: React.FC<{
playerRef: React.RefObject<PlayerRef>;
-}> = ({playerRef}) => {
- const [isMuted, setIsMuted] = useState(false);
-
+ updateAudioVolume: (volume: number) => void;
+ updateAudioMute: (isMuted: boolean) => void;
+ audioState: {
+ volume: number;
+ isMuted: boolean;
+ };
+}> = ({playerRef, updateAudioMute, audioState}) => {
useEffect(() => {
const {current} = playerRef;
@@ -15,7 +19,7 @@ export const PlayerVolume: React.FC<{
}
const onMutedChange = (e) => {
- setIsMuted(e.detail.isMuted);
+ updateAudioMute(e.detail.isMuted);
};
current.addEventListener('mutechange', onMutedChange);
@@ -23,15 +27,15 @@ export const PlayerVolume: React.FC<{
return () => {
current.removeEventListener('mutechange', onMutedChange);
};
- }, [playerRef]);
+ }, [playerRef, updateAudioMute]);
const onClick = useCallback(() => {
- if (isMuted) {
+ if (audioState.isMuted) {
playerRef.current.unmute();
} else {
playerRef.current.mute();
}
- }, [isMuted, playerRef]);
+ }, [audioState, playerRef]);
return (
<button
type="button"
@@ -43,7 +47,7 @@ export const PlayerVolume: React.FC<{
padding: 0,
}}
>
- {isMuted ? (
+ {audioState.isMuted ? (
<IsMutedIcon style={{width: 20, opacity: 0.3}} />
) : (
<NotMutedIcon style={{width: 20}} />
diff --git a/packages/docs/src/remotion/HomepageVideo/Comp.tsx b/packages/docs/src/remotion/HomepageVideo/Comp.tsx
index 124c10f4d5..e8745dab86 100644
--- a/packages/docs/src/remotion/HomepageVideo/Comp.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/Comp.tsx
@@ -1,8 +1,15 @@
import React, {useCallback, useState} from 'react';
-import type {CalculateMetadataFunction} from 'remotion';
-import {AbsoluteFill} from 'remotion';
+import {
+ AbsoluteFill,
+ Audio,
+ interpolate,
+ staticFile,
+ useVideoConfig,
+ type CalculateMetadataFunction,
+} from 'remotion';
import {z} from 'zod';
import {Cards} from './cards/Cards';
+import type {EmojiPosition} from './emoji/EmojiCard';
import type {Location} from './types';
export type Trending = {
@@ -63,6 +70,10 @@ type Props = {
readonly onToggle: () => void;
readonly cardOrder: number[];
readonly updateCardOrder: (newCardOrder: number[]) => void;
+ readonly emojiPositions: EmojiPosition;
+ readonly onClickLeft: () => void;
+ readonly onClickRight: () => void;
+ readonly audioVolume: {volume: number; isMuted: boolean};
};
export const schema = z.object({
@@ -76,8 +87,13 @@ export const HomepageVideoComp: React.FC<z.infer<typeof schema> & Props> = ({
onToggle,
cardOrder,
updateCardOrder,
+ emojiPositions,
+ onClickLeft,
+ onClickRight,
+ audioVolume,
}) => {
const [rerenders, setRerenders] = useState(0);
+ const {durationInFrames} = useVideoConfig();
const onUpdate = useCallback(
(newIndices: number[]) => {
@@ -95,6 +111,9 @@ export const HomepageVideoComp: React.FC<z.infer<typeof schema> & Props> = ({
return null;
}
+ const loweredVolume = audioVolume.volume * 0.5; // this track is too loud by default
+ const audioFadeFrame = durationInFrames - 30;
+
return (
<AbsoluteFill
style={{
@@ -110,6 +129,23 @@ export const HomepageVideoComp: React.FC<z.infer<typeof schema> & Props> = ({
trending={trending}
onToggle={onToggle}
temperatureInCelsius={trending.temperatureInCelsius}
+ onClickLeft={onClickLeft}
+ onClickRight={onClickRight}
+ emojiPositions={emojiPositions}
+ />
+ <Audio
+ src={staticFile('Utope-nature.mp3')}
+ muted={audioVolume.isMuted || audioVolume.volume === 0}
+ volume={(f) =>
+ interpolate(
+ f,
+ [0, 10, audioFadeFrame, durationInFrames - 5],
+ [0, loweredVolume, loweredVolume, 0],
+ {
+ extrapolateLeft: 'clamp',
+ },
+ )
+ }
/>
</AbsoluteFill>
);
diff --git a/packages/docs/src/remotion/HomepageVideo/cards/Card.tsx b/packages/docs/src/remotion/HomepageVideo/cards/Card.tsx
index 386b28dbc2..99b18ed1a2 100644
--- a/packages/docs/src/remotion/HomepageVideo/cards/Card.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/cards/Card.tsx
@@ -27,8 +27,8 @@ export const Card: React.FC<{
readonly indices: number[];
readonly theme: 'dark' | 'light';
readonly withSwitcher: boolean;
- readonly onLeft: () => void;
- readonly onRight: () => void;
+ readonly onClickLeft: () => void;
+ readonly onClickRight: () => void;
}> = ({
positions,
shouldBePositions,
@@ -39,8 +39,8 @@ export const Card: React.FC<{
indices,
theme,
withSwitcher,
- onLeft,
- onRight,
+ onClickLeft,
+ onClickRight,
}) => {
const refToUse = refsToUse[index];
const stopPrevious = useRef<(() => void)[]>([]);
@@ -261,8 +261,8 @@ export const Card: React.FC<{
{content}
{withSwitcher ? (
<>
- <Switcher onTap={onLeft} type="left" theme={theme} />
- <Switcher onTap={onRight} type="right" theme={theme} />
+ <Switcher onTap={onClickLeft} type="left" theme={theme} />
+ <Switcher onTap={onClickRight} type="right" theme={theme} />
</>
) : null}
</AbsoluteFill>
diff --git a/packages/docs/src/remotion/HomepageVideo/cards/Cards.tsx b/packages/docs/src/remotion/HomepageVideo/cards/Cards.tsx
index 5f39c6de8e..347ef11df8 100644
--- a/packages/docs/src/remotion/HomepageVideo/cards/Cards.tsx
+++ b/packages/docs/src/remotion/HomepageVideo/cards/Cards.tsx
@@ -1,10 +1,4 @@
-import React, {
- createRef,
- useCallback,
- useEffect,
- useRef,
- useState,
-} from 'react';
+import React, {createRef, useEffect, useRef, useState} from 'react';
import {AbsoluteFill, getRemotionEnvironment} from 'remotion';
import type {Trending} from '../Comp';
import {CurrentCountry} from '../CurrentCountry';
@@ -23,6 +17,9 @@ export const Cards: React.FC<{
readonly trending: Trending;
readonly temperatureInCelsius: number;
onToggle: () => void;
+ onClickLeft: () => void;
+ onClickRight: () => void;
+ emojiPositions: EmojiPosition;
}> = ({
onUpdate,
indices,
@@ -31,20 +28,12 @@ export const Cards: React.FC<{
trending,
onToggle,
temperatureInCelsius,
+ onClickLeft,
+ onClickRight,
+ emojiPositions,
}) => {
const container = useRef<HTMLDivElement>(null);
- const activeTranslationStyle =
- 'transform 0.2s ease-in, opacity 0.2s ease-in-out';
-
- const [emojiPositions, setEmojiPositions] = useState<EmojiPosition>({
- prev: 'melting',
- current: 'partying-face',
- next: 'fire',
- translation: 0,
- translationStyle: activeTranslationStyle,
- });
-
const [refs] = useState(() => {
return new Array(4).fill(true).map(() => {
return createRef<HTMLDivElement>();
@@ -76,49 +65,6 @@ export const Cards: React.FC<{
};
}, [onToggle]);
- const onLeft = useCallback(() => {
- setEmojiPositions((c) => {
- return {
- ...c,
- translation: -33.3,
- translationStyle: activeTranslationStyle,
- };
- });
- // after the animation is done, we need to update the emoji contents
- setTimeout(() => {
- setEmojiPositions((c) => {
- return {
- prev: c.next,
- current: c.prev,
- next: c.current,
- translation: 0,
- translationStyle: undefined,
- };
- });
- }, 200);
- }, []);
-
- const onRight = useCallback(() => {
- setEmojiPositions((c) => {
- return {
- ...c,
- translation: 33.3,
- translationStyle: 'transform 0.2s ease-in, opacity 0.2s ease-in-out',
- };
- });
- setTimeout(() => {
- setEmojiPositions((c) => {
- return {
- prev: c.current,
- current: c.next,
- next: c.prev,
- translation: 0,
- translationStyle: '',
- };
- });
- }, 200);
- }, []);
-
return (
<AbsoluteFill ref={container}>
{new Array(4).fill(true).map((_, i) => {
@@ -151,8 +97,8 @@ export const Cards: React.FC<{
indices={indices}
theme={theme}
withSwitcher={index === 3 && !isRendering}
- onLeft={onLeft}
- onRight={onRight}
+ onClickLeft={onClickLeft}
+ onClickRight={onClickRight}
/>
);
})}
diff --git a/packages/docs/static/Utope-nature.mp3 b/packages/docs/static/Utope-nature.mp3
new file mode 100644
index 0000000000..f476e7b2d2
Binary files /dev/null and b/packages/docs/static/Utope-nature.mp3 differ