Skip to content

🎥 Make videos programmatically with React

add muted state + respect remotion studio

Igor Samokhovetse8ce68c26388
diff --git a/packages/media/src/video/media-player.ts b/packages/media/src/video/media-player.ts
index 57e28a46ca..f13f0acac8 100644
--- a/packages/media/src/video/media-player.ts
+++ b/packages/media/src/video/media-player.ts
@@ -44,6 +44,7 @@ export class MediaPlayer {
private audioSyncAnchor: number = 0;
private playing = false;
+ private muted = false;
private animationFrameId: number | null = null;
private videoAsyncId = 0;
@@ -219,6 +220,13 @@ export class MediaPlayer {
this.stopRenderLoop();
}
+ public setMuted(muted: boolean): void {
+ this.muted = muted;
+ if (muted) {
+ this.cleanupAudioQueue();
+ }
+ }
+
public dispose(): void {
this.input?.dispose();
this.stopRenderLoop();
@@ -497,7 +505,7 @@ export class MediaPlayer {
this.maybeResumeFromBuffering(totalBufferDuration);
- if (this.playing) {
+ if (this.playing && !this.muted) {
if (isFirstBuffer) {
this.audioSyncAnchor =
this.sharedAudioContext.currentTime - timestamp;
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 43c7e1c74c..a5c4622f24 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -3,7 +3,8 @@ import type {LogLevel} from 'remotion';
import {Internals, useBufferState, useCurrentFrame} from 'remotion';
import {MediaPlayer} from './media-player';
-const {useUnsafeVideoConfig, Timeline, SharedAudioContext} = Internals;
+const {useUnsafeVideoConfig, Timeline, SharedAudioContext, useMediaMutedState} =
+ Internals;
type NewVideoForPreviewProps = {
readonly src: string;
@@ -11,6 +12,7 @@ type NewVideoForPreviewProps = {
readonly playbackRate?: number;
readonly logLevel?: LogLevel;
readonly className?: string;
+ readonly muted?: boolean;
};
export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
@@ -19,6 +21,7 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
playbackRate = 1,
logLevel = 'info',
className,
+ muted = false,
}) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const videoConfig = useUnsafeVideoConfig();
@@ -32,6 +35,8 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
const buffer = useBufferState();
const delayHandleRef = useRef<{unblock: () => void} | null>(null);
+ const [mediaMuted] = useMediaMutedState();
+
if (!videoConfig) {
throw new Error('No video config found');
}
@@ -163,6 +168,16 @@ export const NewVideoForPreview: React.FC<NewVideoForPreviewProps> = ({
});
}, [mediaPlayerReady, buffer, logLevel]);
+ const effectiveMuted = muted || mediaMuted;
+
+ // sync muted state with MediaPlayer
+ useEffect(() => {
+ const mediaPlayer = mediaPlayerRef.current;
+ if (!mediaPlayer || !mediaPlayerReady) return;
+
+ mediaPlayer.setMuted(effectiveMuted);
+ }, [effectiveMuted, mediaPlayerReady]);
+
return (
<canvas
ref={canvasRef}
diff --git a/packages/media/src/video/video.tsx b/packages/media/src/video/video.tsx
index d12838b626..156cd6951d 100644
--- a/packages/media/src/video/video.tsx
+++ b/packages/media/src/video/video.tsx
@@ -76,6 +76,7 @@ export const Video: React.FC<VideoProps> = (props) => {
style={otherProps.style}
playbackRate={otherProps.playbackRate}
logLevel={otherProps.logLevel}
+ muted={otherProps.muted}
/>
);
};