Skip to content

🎥 Make videos programmatically with React

simplify usemediatagvolume

Jonny Burger4f7d570acac8
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsx
index 6bbee571a7..fc0d6cb3ef 100644
--- a/packages/core/src/audio/Audio.tsx
+++ b/packages/core/src/audio/Audio.tsx
@@ -12,6 +12,7 @@ export const Audio: React.FC<RemotionAudioProps & RemotionMainAudioProps> = (
const {startAt, endAt, ...otherProps} = props;
if (typeof startAt !== 'undefined' || typeof endAt !== 'undefined') {
validateStartFromProps(startAt, endAt);
+
const startAtFrameNo = startAt ?? 0;
const endAtFrameNo = endAt ?? Infinity;
return (
diff --git a/packages/core/src/audio/AudioForDevelopment.tsx b/packages/core/src/audio/AudioForDevelopment.tsx
index fa2a695edc..c25664457e 100644
--- a/packages/core/src/audio/AudioForDevelopment.tsx
+++ b/packages/core/src/audio/AudioForDevelopment.tsx
@@ -4,6 +4,7 @@ import {getAssetFileName} from '../get-asset-file-name';
import {isApproximatelyTheSame} from '../is-approximately-the-same';
import {useNonce} from '../nonce';
import {SequenceContext} from '../sequencing';
+import {useMediaTagVolume} from '../sync-actual-volume';
import {TimelineContext, usePlayingState} from '../timeline-position-state';
import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config';
@@ -15,7 +16,6 @@ export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {
const audioRef = useRef<HTMLAudioElement>(null);
const frame = useCurrentFrame();
const absoluteFrame = useAbsoluteCurrentFrame();
- const [actualVolume, setActualVolume] = useState(1);
const nonce = useNonce();
const audioFrame = useAudioFrame();
@@ -36,21 +36,7 @@ export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {
const {volume, ...nativeProps} = props;
- useEffect(() => {
- const ref = audioRef.current;
- if (!ref) {
- return;
- }
- if (ref.volume !== actualVolume) {
- setActualVolume(ref.volume);
- return;
- }
- const onChange = () => {
- setActualVolume(ref.volume);
- };
- ref.addEventListener('volumechange', onChange);
- return () => ref.removeEventListener('volumechange', onChange);
- }, [actualVolume]);
+ const actualVolume = useMediaTagVolume(audioRef);
useEffect(() => {
const userPreferredVolume = evaluateVolume({
diff --git a/packages/core/src/use-media-tag-volume.ts b/packages/core/src/use-media-tag-volume.ts
new file mode 100644
index 0000000000..805ef0eeed
--- /dev/null
+++ b/packages/core/src/use-media-tag-volume.ts
@@ -0,0 +1,27 @@
+import {RefObject, useEffect, useState} from 'react';
+
+// Returns the real volume of the audio or video while playing,
+// no matter what the supposed volume should be
+export const useMediaTagVolume = (
+ mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>
+) => {
+ const [actualVolume, setActualVolume] = useState(1);
+
+ useEffect(() => {
+ const ref = mediaRef.current;
+ if (!ref) {
+ return;
+ }
+ if (ref.volume !== actualVolume) {
+ setActualVolume(ref.volume);
+ return;
+ }
+ const onChange = () => {
+ setActualVolume(ref.volume);
+ };
+ ref.addEventListener('volumechange', onChange);
+ return () => ref.removeEventListener('volumechange', onChange);
+ }, [actualVolume, mediaRef]);
+
+ return actualVolume;
+};
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index aeafe4e867..b2d730615d 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -17,8 +17,8 @@ export const Video: React.FC<RemotionVideoProps & RemotionMainVideoProps> = (
const endAtFrameNo = endAt ?? Infinity;
return (
<Sequence
- showInTimeline={false}
from={0 - startAtFrameNo}
+ showInTimeline={false}
durationInFrames={endAtFrameNo}
>
<Video {...otherProps} />
diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsx
index 51459783af..adb5e73100 100644
--- a/packages/core/src/video/VideoForDevelopment.tsx
+++ b/packages/core/src/video/VideoForDevelopment.tsx
@@ -1,6 +1,7 @@
-import React, {useEffect, useRef, useState} from 'react';
+import React, {useEffect, useRef} from 'react';
import {FEATURE_FLAG_V2_BREAKING_CHANGES} from '../feature-flags';
import {isApproximatelyTheSame} from '../is-approximately-the-same';
+import {useMediaTagVolume} from '../sync-actual-volume';
import {usePlayingState} from '../timeline-position-state';
import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config';
@@ -10,11 +11,11 @@ import {RemotionVideoProps} from './props';
export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
const videoRef = useRef<HTMLVideoElement>(null);
- const currentFrame = useCurrentFrame();
+ const frame = useCurrentFrame();
const absoluteFrame = useAbsoluteCurrentFrame();
+
const videoConfig = useUnsafeVideoConfig();
const [playing] = usePlayingState();
- const [actualVolume, setActualVolume] = useState(1);
const {volume, ...nativeProps} = props;
@@ -27,25 +28,11 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
}
}, [playing]);
- useEffect(() => {
- const ref = videoRef.current;
- if (!ref) {
- return;
- }
- if (ref.volume !== actualVolume) {
- setActualVolume(ref.volume);
- return;
- }
- const onChange = () => {
- setActualVolume(ref.volume);
- };
- ref.addEventListener('volumechange', onChange);
- return () => ref.removeEventListener('volumechange', onChange);
- }, [actualVolume]);
+ const actualVolume = useMediaTagVolume(videoRef);
useEffect(() => {
const userPreferredVolume = evaluateVolume({
- frame: currentFrame,
+ frame,
volume,
});
if (
@@ -54,7 +41,7 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
) {
videoRef.current.volume = userPreferredVolume;
}
- }, [actualVolume, currentFrame, props.volume, volume]);
+ }, [actualVolume, frame, props.volume, volume]);
useEffect(() => {
if (!videoRef.current) {
@@ -70,11 +57,11 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
if (FEATURE_FLAG_V2_BREAKING_CHANGES) {
return getCurrentTime({
fps: videoConfig.fps,
- frame: currentFrame,
+ frame,
src: props.src as string,
});
}
- return currentFrame / (1000 / videoConfig.fps);
+ return frame / (1000 / videoConfig.fps);
})();
if (!playing || absoluteFrame === 0) {
@@ -85,7 +72,7 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
videoRef.current.currentTime = currentTime;
videoRef.current.play();
}
- }, [currentFrame, playing, videoConfig, absoluteFrame, props.src]);
+ }, [frame, playing, videoConfig, absoluteFrame, props.src]);
return <video ref={videoRef} {...nativeProps} />;
};