Skip to content

🎥 Make videos programmatically with React

buffer video and audio

Jonny Burgerd5e5a42552eb
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsx
index 0195355a0b..dafc036eec 100644
--- a/packages/core/src/audio/Audio.tsx
+++ b/packages/core/src/audio/Audio.tsx
@@ -28,7 +28,8 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
}
> = (props, ref) => {
const audioContext = useContext(SharedAudioContext);
- const {startFrom, endAt, name, stack, ...otherProps} = props;
+ const {startFrom, endAt, name, stack, pauseWhenBuffering, ...otherProps} =
+ props;
const {loop, ...propsOtherThanLoop} = props;
const {fps} = useVideoConfig();
const environment = getRemotionEnvironment();
@@ -145,6 +146,8 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
ref={ref}
onError={onError}
onDuration={onDuration}
+ // Proposal: Make this default to true in v5
+ pauseWhenBuffering={pauseWhenBuffering ?? false}
_remotionInternalNeedsDurationCalculation={Boolean(loop)}
/>
);
diff --git a/packages/core/src/audio/AudioForPreview.tsx b/packages/core/src/audio/AudioForPreview.tsx
index 5fb4eb9457..7c595d810e 100644
--- a/packages/core/src/audio/AudioForPreview.tsx
+++ b/packages/core/src/audio/AudioForPreview.tsx
@@ -11,6 +11,7 @@ import {usePreload} from '../prefetch.js';
import {random} from '../random.js';
import {SequenceContext} from '../SequenceContext.jsx';
import {SequenceVisibilityToggleContext} from '../SequenceManager.jsx';
+import {useMediaBuffering} from '../use-media-buffering.js';
import {useMediaInTimeline} from '../use-media-in-timeline.js';
import {
DEFAULT_ACCEPTABLE_TIMESHIFT,
@@ -29,6 +30,7 @@ import {useFrameForVolumeProp} from './use-audio-frame.js';
type AudioForPreviewProps = RemotionAudioProps & {
shouldPreMountAudioTags: boolean;
onDuration: (src: string, durationInSeconds: number) => void;
+ pauseWhenBuffering: boolean;
_remotionInternalNativeLoopPassed: boolean;
_remotionInternalStack: string | null;
};
@@ -64,6 +66,7 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
_remotionInternalStack,
allowAmplificationDuringRender,
name,
+ pauseWhenBuffering,
...nativeProps
} = props;
const {hidden} = useContext(SequenceVisibilityToggleContext);
@@ -148,6 +151,8 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
acceptableTimeShiftInSeconds ?? DEFAULT_ACCEPTABLE_TIMESHIFT,
});
+ useMediaBuffering(audioRef, pauseWhenBuffering);
+
useImperativeHandle(
ref,
() => {
diff --git a/packages/core/src/audio/props.ts b/packages/core/src/audio/props.ts
index 1eea7b328b..52458d78c9 100644
--- a/packages/core/src/audio/props.ts
+++ b/packages/core/src/audio/props.ts
@@ -20,4 +20,5 @@ export type RemotionAudioProps = Omit<
_remotionInternalNeedsDurationCalculation?: boolean;
_remotionInternalNativeLoopPassed?: boolean;
toneFrequency?: number;
+ pauseWhenBuffering?: boolean;
};
diff --git a/packages/core/src/use-media-buffering.ts b/packages/core/src/use-media-buffering.ts
index b7ecf19e32..c5b760d7c7 100644
--- a/packages/core/src/use-media-buffering.ts
+++ b/packages/core/src/use-media-buffering.ts
@@ -1,33 +1,44 @@
+import type React from 'react';
import {useEffect} from 'react';
import {useBuffer} from './use-buffer';
export const useMediaBuffering = (
- element: HTMLVideoElement | HTMLAudioElement,
+ element: React.RefObject<HTMLVideoElement | HTMLAudioElement>,
+ shouldBuffer: boolean,
) => {
const buffer = useBuffer();
useEffect(() => {
let cleanup: () => void;
+ const {current} = element;
+ if (!current) {
+ return;
+ }
+
+ if (!shouldBuffer) {
+ return;
+ }
+
const onWaiting = () => {
const {unblock} = buffer.delayPlayback();
const onCanPlay = () => {
unblock();
};
- element.addEventListener('canplay', onCanPlay, {
+ current.addEventListener('canplay', onCanPlay, {
once: true,
});
cleanup = () => {
- element.removeEventListener('canplay', onCanPlay);
+ current.removeEventListener('canplay', onCanPlay);
};
};
- element.addEventListener('waiting', onWaiting);
+ current.addEventListener('waiting', onWaiting);
return () => {
cleanup();
};
- }, [buffer, element]);
+ }, [buffer, element, shouldBuffer]);
};
diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsx
index c2f1a5d54b..8df8ab6610 100644
--- a/packages/core/src/video/OffthreadVideo.tsx
+++ b/packages/core/src/video/OffthreadVideo.tsx
@@ -14,7 +14,8 @@ import {VideoForPreview} from './VideoForPreview.js';
export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
// Should only destruct `startFrom` and `endAt` from props,
// rest gets drilled down
- const {startFrom, endAt, name, stack, ...otherProps} = props;
+ const {startFrom, endAt, name, pauseWhenBuffering, stack, ...otherProps} =
+ props;
const environment = getRemotionEnvironment();
const onDuration = useCallback(() => undefined, []);
@@ -65,6 +66,7 @@ export const OffthreadVideo: React.FC<OffthreadVideoProps> = (props) => {
_remotionInternalNativeLoopPassed={false}
onDuration={onDuration}
onlyWarnForMediaSeekingError
+ pauseWhenBuffering={pauseWhenBuffering ?? false}
{...withoutTransparent}
/>
);
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index 7c8f3d24da..31c3b0ef0f 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -29,6 +29,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
startFrom,
endAt,
name,
+ pauseWhenBuffering,
stack,
_remotionInternalNativeLoopPassed,
...otherProps
@@ -118,6 +119,8 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
onlyWarnForMediaSeekingError={false}
{...otherProps}
ref={ref}
+ // Proposal: Make this default to true in v5
+ pauseWhenBuffering={pauseWhenBuffering ?? false}
onDuration={onDuration}
_remotionInternalStack={stack ?? null}
_remotionInternalNativeLoopPassed={
diff --git a/packages/core/src/video/VideoForPreview.tsx b/packages/core/src/video/VideoForPreview.tsx
index c99d98a2ec..59b6ec1f98 100644
--- a/packages/core/src/video/VideoForPreview.tsx
+++ b/packages/core/src/video/VideoForPreview.tsx
@@ -11,6 +11,7 @@ import {useFrameForVolumeProp} from '../audio/use-audio-frame.js';
import {usePreload} from '../prefetch.js';
import {SequenceContext} from '../SequenceContext.jsx';
import {SequenceVisibilityToggleContext} from '../SequenceManager.jsx';
+import {useMediaBuffering} from '../use-media-buffering.js';
import {useMediaInTimeline} from '../use-media-in-timeline.js';
import {
DEFAULT_ACCEPTABLE_TIMESHIFT,
@@ -29,6 +30,7 @@ import {isIosSafari, useAppendVideoFragment} from './video-fragment.js';
type VideoForPreviewProps = RemotionVideoProps & {
onlyWarnForMediaSeekingError: boolean;
onDuration: (src: string, durationInSeconds: number) => void;
+ pauseWhenBuffering: boolean;
_remotionInternalNativeLoopPassed: boolean;
_remotionInternalStack: string | null;
};
@@ -62,6 +64,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
_remotionInternalNativeLoopPassed,
_remotionInternalStack,
style,
+ pauseWhenBuffering,
...nativeProps
} = props;
if (typeof acceptableTimeShift !== 'undefined') {
@@ -105,6 +108,8 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
acceptableTimeShiftInSeconds ?? DEFAULT_ACCEPTABLE_TIMESHIFT,
});
+ useMediaBuffering(videoRef, pauseWhenBuffering);
+
const actualFrom = parentSequence
? parentSequence.relativeFrom + parentSequence.cumulatedFrom
: 0;
diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.ts
index a50b2741f3..153c0dc2cd 100644
--- a/packages/core/src/video/props.ts
+++ b/packages/core/src/video/props.ts
@@ -23,6 +23,7 @@ export type RemotionVideoProps = Omit<
acceptableTimeShiftInSeconds?: number;
allowAmplificationDuringRender?: boolean;
toneFrequency?: number;
+ pauseWhenBuffering?: boolean;
};
type DeprecatedOffthreadVideoProps = {
@@ -46,6 +47,7 @@ export type OffthreadVideoProps = {
allowAmplificationDuringRender?: boolean;
toneFrequency?: number;
transparent?: boolean;
+ pauseWhenBuffering?: boolean;
/**
* @deprecated For internal use only
*/