Skip to content

🎥 Make videos programmatically with React

mute button

Jonny Burgerc0e0d5b76312
diff --git a/packages/docs/docs/player/custom-controls.mdx b/packages/docs/docs/player/custom-controls.mdx
index 243f146e83..bc16ab9191 100644
--- a/packages/docs/docs/player/custom-controls.mdx
+++ b/packages/docs/docs/player/custom-controls.mdx
@@ -238,7 +238,7 @@ The `Exit Fullscreen` label is hypothetical since if it is rendered outside of t
### Seek bar
-```tsx twoslash
+```tsx twoslash title="SeekBar.tsx"
import type {PlayerRef} from '@remotion/player';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {interpolate} from 'remotion';
@@ -747,4 +747,50 @@ TODO
### Mute button
-TODO
+Note that Remotion does not consider a volume of 0 "muted".
+Adapt the logic to also listen to the [`volumechange`](/docs/player/player#volumechange) event if necessary.
+
+```tsx twoslash title="MuteButton.tsx"
+import type {PlayerRef} from '@remotion/player';
+import React, {useEffect, useState} from 'react';
+
+export const MuteButton: React.FC<{
+ playerRef: React.RefObject<PlayerRef>;
+}> = ({playerRef}) => {
+ const [muted, setMuted] = useState(playerRef.current?.isMuted() ?? false);
+
+ const onClick = React.useCallback(() => {
+ if (!playerRef.current) {
+ return;
+ }
+
+ if (playerRef.current.isMuted()) {
+ playerRef.current.unmute();
+ } else {
+ playerRef.current.mute();
+ }
+ }, [playerRef]);
+
+ useEffect(() => {
+ const {current} = playerRef;
+ if (!current) {
+ return;
+ }
+
+ const onMuteChange = () => {
+ setMuted(current.isMuted());
+ };
+
+ current.addEventListener('mutechange', onMuteChange);
+ return () => {
+ current.removeEventListener('mutechange', onMuteChange);
+ };
+ }, [playerRef]);
+
+ return (
+ <button type="button" onClick={onClick}>
+ {muted ? 'Unmute' : 'Mute'}
+ </button>
+ );
+};
+```