🎥 Make videos programmatically with React
apply findings to audio
Jonny Burgerb4c09ac84a54
diff --git a/packages/core/src/audio/AudioForDevelopment.tsx b/packages/core/src/audio/AudioForDevelopment.tsxindex ba48a43656..aef8bd9404 100644--- a/packages/core/src/audio/AudioForDevelopment.tsx+++ b/packages/core/src/audio/AudioForDevelopment.tsx@@ -1,12 +1,14 @@import React, {useEffect, useRef} from 'react';import {usePlayingState} from '../timeline-position-state';-import {useCurrentFrame} from '../use-frame';+import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';import {useUnsafeVideoConfig} from '../use-unsafe-video-config';import {RemotionAudioProps} from './props';export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {const audioRef = useRef<HTMLAudioElement>(null);const currentFrame = useCurrentFrame();+ const absoluteFrame = useAbsoluteCurrentFrame();+const videoConfig = useUnsafeVideoConfig();const [playing] = usePlayingState();@@ -26,21 +28,28 @@ export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {throw new Error('No video config found');}const shouldBeTime = currentFrame / videoConfig.fps;+const isTime = audioRef.current.currentTime;const timeShift = Math.abs(shouldBeTime - isTime);if (timeShift > 0.5) {console.log('Time has shifted by', timeShift, 'sec. Fixing...');}- if (!playing /**1 */ || timeShift > 0.5 /**2 */) {+ if (timeShift > 0.5) {+ // If scrubbing around, adjust timing+ // or if time shift is bigger than 0.2sec+ audioRef.current.currentTime = shouldBeTime;+ }++ if (!playing || absoluteFrame === 0) {// If scrubbing around, adjust timing// or if time shift is bigger than 0.2sec- audioRef.current.currentTime = currentFrame / videoConfig.fps;+ audioRef.current.currentTime = shouldBeTime;}- if (currentFrame === 0 && playing) {- // If video ended, play it again+ if (audioRef.current.paused && !audioRef.current.ended && playing) {+ // Play videoaudioRef.current.play();}- }, [currentFrame, playing, videoConfig]);+ }, [absoluteFrame, currentFrame, playing, videoConfig]);return <audio ref={audioRef} {...props} />;};diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsxindex 88b4e3c0df..3e026c6166 100644--- a/packages/core/src/video/VideoForDevelopment.tsx+++ b/packages/core/src/video/VideoForDevelopment.tsx@@ -1,4 +1,5 @@import React, {useEffect, useRef} from 'react';+import {FEATURE_FLAG_V2_BREAKING_CHANGES} from '../feature-flags';import {usePlayingState} from '../timeline-position-state';import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';import {useUnsafeVideoConfig} from '../use-unsafe-video-config';@@ -27,14 +28,21 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {if (!videoConfig) {throw new Error('No video config found');}- const currentTime = (currentFrame + 1) / videoConfig.fps;+ const currentTime = (() => {+ if (FEATURE_FLAG_V2_BREAKING_CHANGES) {+ // In Chrome, if 30fps, the first frame is still displayed at 0.033333+ // even though after that it increases by 0.033333333 each.+ // So frame = 0 in Remotion is like frame = 1 for the browser+ return (currentFrame + 1) / videoConfig.fps;+ }+ return currentFrame / (1000 / videoConfig.fps);+ })();if (!playing || absoluteFrame === 0) {videoRef.current.currentTime = currentTime;- return;}- if (videoRef.current.paused && !videoRef.current.ended) {+ if (videoRef.current.paused && !videoRef.current.ended && playing) {videoRef.current.currentTime = currentTime;videoRef.current.play();}