{"record":{"id":"3a91e7faf94acf1d","repo":"remotion-dev/remotion","slug":"changing-the-audiocontext-sample-rate-dynamically","errorCode":null,"errorMessage":"Changing the AudioContext sample rate dynamically is not supported. The sample rate was initialized with ${initialSampleRate.current} Hz, but ${sampleRate} Hz was passed later.","messagePattern":"Changing the AudioContext sample rate dynamically is not supported\\. The sample rate was initialized with (.+?) Hz, but (.+?) Hz was passed later\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/audio/use-audio-context.ts","lineNumber":47,"sourceCode":"\t}\n};\n\nexport const useSingletonAudioContext = ({\n\tlogLevel,\n\tlatencyHint,\n\taudioEnabled,\n\tsampleRate,\n}: {\n\tlogLevel: LogLevel;\n\tlatencyHint: AudioContextLatencyCategory;\n\taudioEnabled: boolean;\n\tsampleRate: number;\n}) => {\n\tconst env = useRemotionEnvironment();\n\tconst initialSampleRate = useRef(sampleRate);\n\n\tif (sampleRate !== initialSampleRate.current) {\n\t\tthrow new Error(\n\t\t\t`Changing the AudioContext sample rate dynamically is not supported. The sample rate was initialized with ${initialSampleRate.current} Hz, but ${sampleRate} Hz was passed later.`,\n\t\t);\n\t}\n\n\tconst context = useMemo(() => {\n\t\tif (env.isRendering) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (!audioEnabled) {\n\t\t\treturn null;\n\t\t}\n\n\t\tif (typeof AudioContext === 'undefined') {\n\t\t\twarnOnce(logLevel);\n\t\t\treturn null;\n\t\t}\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/remotion-dev/remotion/blob/78fe4bb3fdb5a2cd68724393d63cb223db333fa7/packages/core/src/audio/use-audio-context.ts#L29-L65","documentation":"The Web Audio AudioContext is constructed once with a fixed sampleRate (derived from the Player's previewSampleRate prop, default 48000). Browsers cannot re-sample an existing context, so Remotion captures the initial rate in a ref and throws if a later render passes a different value. Changing it would require tearing down and recreating the entire audio graph.","triggerScenarios":"Passing <Player previewSampleRate={rate} /> where rate is React state that changes (e.g. 44100 then 48000). Also triggered by a config-derived sample rate that flips during a session or via hot-reload.","commonSituations":"Exposing sample rate as a user setting; matching source media sample rate dynamically; switching between 44100 and 48000 based on the loaded clip.","solutions":["Pass a constant literal for previewSampleRate (48000 is recommended).","If the rate must change, remount the Player with a key tied to the value.","Leave previewSampleRate unset to use the default 48000."],"exampleFix":"// before\n<Player previewSampleRate={sampleRate} ... />\n\n// after\n<Player key={sampleRate} previewSampleRate={sampleRate} ... />","handlingStrategy":"validation","validationCode":"const SAMPLE_RATE = 48000; // constant\n// <Player previewSampleRate={SAMPLE_RATE} key={String(SAMPLE_RATE)} .../>","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass a constant for previewSampleRate (48000 recommended).","Do not derive it from the loaded media at runtime.","Remount via key if you truly need a different rate."],"tags":["audio","player","validation","react","lifecycle","web-audio"],"backgroundTag":null,"analyzedSha":"78fe4bb3fdb5a2cd68724393d63cb223db333fa7","analyzedAt":"2026-08-12T17:18:50.444Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}