Skip to content

Commit 7b68f50

Browse files
authored
Merge pull request #6249 from remotion-dev/dont-create-audio-context
2 parents 9c3f55f + 0503032 commit 7b68f50

File tree

7 files changed

+30
-15
lines changed

7 files changed

+30
-15
lines changed

packages/core/src/RemotionRoot.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export const RemotionRootContexts: React.FC<{
2424
readonly numberOfAudioTags: number;
2525
readonly logLevel: LogLevel;
2626
readonly audioLatencyHint: AudioContextLatencyCategory;
27-
readonly videoEnabled: boolean | null;
28-
readonly audioEnabled: boolean | null;
27+
readonly videoEnabled: boolean;
28+
readonly audioEnabled: boolean;
2929
readonly frameState: Record<string, number> | null;
3030
}> = ({
3131
children,
@@ -87,6 +87,7 @@ export const RemotionRootContexts: React.FC<{
8787
<SharedAudioContextProvider
8888
numberOfAudioTags={numberOfAudioTags}
8989
audioLatencyHint={audioLatencyHint}
90+
audioEnabled={audioEnabled}
9091
>
9192
<DurationsContextProvider>
9293
<BufferingProvider>{children}</BufferingProvider>

packages/core/src/audio/AudioForPreview.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import {evaluateVolume} from '../volume-prop.js';
2626
import {warnAboutTooHighVolume} from '../volume-safeguard.js';
2727
import type {IsExact, NativeAudioProps, RemotionAudioProps} from './props.js';
28-
import {SharedAudioContext, useSharedAudio} from './shared-audio-tags.js';
28+
import {useSharedAudio} from './shared-audio-tags.js';
2929
import {useFrameForVolumeProp} from './use-audio-frame.js';
3030

3131
type AudioForPreviewProps = RemotionAudioProps & {
@@ -165,11 +165,6 @@ const AudioForDevelopmentForwardRefFunction: React.ForwardRefRenderFunction<
165165
],
166166
);
167167

168-
const context = useContext(SharedAudioContext);
169-
if (!context) {
170-
throw new Error('SharedAudioContext not found');
171-
}
172-
173168
const {el: audioRef, mediaElementSourceNode} = useSharedAudio({
174169
aud: propsToPass,
175170
audioId: id,

packages/core/src/audio/shared-audio-tags.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ export const SharedAudioContextProvider: React.FC<{
118118
readonly numberOfAudioTags: number;
119119
readonly children: React.ReactNode;
120120
readonly audioLatencyHint: AudioContextLatencyCategory;
121-
}> = ({children, numberOfAudioTags, audioLatencyHint}) => {
121+
readonly audioEnabled: boolean;
122+
}> = ({children, numberOfAudioTags, audioLatencyHint, audioEnabled}) => {
122123
const audios = useRef<AudioElem[]>([]);
123124
const [initialNumberOfAudioTags] = useState(numberOfAudioTags);
124125

@@ -129,7 +130,11 @@ export const SharedAudioContextProvider: React.FC<{
129130
}
130131

131132
const logLevel = useLogLevel();
132-
const audioContext = useSingletonAudioContext(logLevel, audioLatencyHint);
133+
const audioContext = useSingletonAudioContext({
134+
logLevel,
135+
latencyHint: audioLatencyHint,
136+
audioEnabled,
137+
});
133138
const refs = useMemo(() => {
134139
return new Array(numberOfAudioTags).fill(true).map((): Ref => {
135140
const ref = createRef<HTMLAudioElement>();

packages/core/src/audio/use-audio-context.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,26 @@ const warnOnce = (logLevel: LogLevel) => {
2121
}
2222
};
2323

24-
export const useSingletonAudioContext = (
25-
logLevel: LogLevel,
26-
latencyHint: AudioContextLatencyCategory,
27-
) => {
24+
export const useSingletonAudioContext = ({
25+
logLevel,
26+
latencyHint,
27+
audioEnabled,
28+
}: {
29+
logLevel: LogLevel;
30+
latencyHint: AudioContextLatencyCategory;
31+
audioEnabled: boolean;
32+
}) => {
2833
const env = useRemotionEnvironment();
2934

3035
const audioContext = useMemo(() => {
3136
if (env.isRendering) {
3237
return null;
3338
}
3439

40+
if (!audioEnabled) {
41+
return null;
42+
}
43+
3544
if (typeof AudioContext === 'undefined') {
3645
warnOnce(logLevel);
3746
return null;
@@ -44,7 +53,7 @@ export const useSingletonAudioContext = (
4453
// we observe some issues that seem to go away when we set the sample rate to 48000 with Sony LinkBuds Bluetooth headphones.
4554
sampleRate: 48000,
4655
});
47-
}, [logLevel, latencyHint, env.isRendering]);
56+
}, [logLevel, latencyHint, env.isRendering, audioEnabled]);
4857

4958
return audioContext;
5059
};

packages/player/src/Player.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ const PlayerFn = <
408408
audioLatencyHint={audioLatencyHint}
409409
volumePersistenceKey={volumePersistenceKey}
410410
inputProps={actualInputProps}
411+
audioEnabled
411412
>
412413
<Internals.SetTimelineContext.Provider value={setTimelineContextValue}>
413414
<PlayerEmitterProvider currentPlaybackRate={currentPlaybackRate}>

packages/player/src/SharedPlayerContext.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const SharedPlayerContexts: React.FC<{
3030
readonly audioLatencyHint: AudioContextLatencyCategory;
3131
readonly volumePersistenceKey?: string;
3232
readonly inputProps: Record<string, unknown>;
33+
readonly audioEnabled: boolean;
3334
}> = ({
3435
children,
3536
timelineContext,
@@ -44,6 +45,7 @@ export const SharedPlayerContexts: React.FC<{
4445
audioLatencyHint,
4546
volumePersistenceKey,
4647
inputProps,
48+
audioEnabled,
4749
}) => {
4850
const compositionManagerContext: CompositionManagerContext = useMemo(() => {
4951
const context: CompositionManagerContext = {
@@ -152,6 +154,7 @@ export const SharedPlayerContexts: React.FC<{
152154
<Internals.SharedAudioContextProvider
153155
numberOfAudioTags={numberOfSharedAudioTags}
154156
audioLatencyHint={audioLatencyHint}
157+
audioEnabled={audioEnabled}
155158
>
156159
<Internals.BufferingProvider>
157160
{children}

packages/player/src/Thumbnail.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ const ThumbnailFn = <
127127
logLevel={logLevel}
128128
audioLatencyHint="playback"
129129
inputProps={passedInputProps}
130+
audioEnabled={false}
130131
>
131132
<ThumbnailEmitterContext.Provider value={emitter}>
132133
<ThumbnailUI

0 commit comments

Comments
 (0)