Skip to content

🎥 Make videos programmatically with React

useAudioData

Jonny Burgerd5c43466fe55
diff --git a/packages/cli/src/editor/components/AudioWaveform.tsx b/packages/cli/src/editor/components/AudioWaveform.tsx
index fdfc5fcaf3..b742cccbe6 100644
--- a/packages/cli/src/editor/components/AudioWaveform.tsx
+++ b/packages/cli/src/editor/components/AudioWaveform.tsx
@@ -92,7 +92,7 @@ export const AudioWaveform: React.FC<{
getAudioData(src)
.then((data) => {
if (mountState.current.isMounted) {
- setMaxMediaDuration(Math.floor(data.duration * fps));
+ setMaxMediaDuration(Math.floor(data.durationInSeconds * fps));
setMetadata(data);
}
})
diff --git a/packages/docs/docs/use-audio-data.md b/packages/docs/docs/use-audio-data.md
index f3baa6ccd2..bc581f755f 100644
--- a/packages/docs/docs/use-audio-data.md
+++ b/packages/docs/docs/use-audio-data.md
@@ -9,4 +9,46 @@ This function is part of Remotion 2.0 which is not yet released. The information
_Part of the `@remotion/media-utils`_ package of helper functions.
-TODO
+This convienience function wraps the [`getAudioData()`](get-audio-data) function into a hook and does 3 things:
+
+- Keeps the audio data in a state
+- Wraps the function in a [`delayRender()` / `continueRender()`](data-fetching) pattern.
+- Handles the case where the component gets unmounted while the fetching is in progress and a React error is thrown.
+
+Using this function, you can elegantly render a component based on audio properties, for example together with the [`visualizeAudio()`](visalize-audio) function.
+
+## Arguments
+
+### `src`
+
+A string pointing to an audio asset.
+
+## Return value
+
+`AudioData | null` - An object containing audio data (see documentation of [`getAudioData()`](get-audio-data)) or `null` if the data has not been loaded.
+
+## Example
+
+```tsx
+import {useAudioData} from '@remotion/media-utils';
+import music from './music.mp3'
+
+
+export const MyComponent: React.FC = () => {
+ const audioData = useAudioData(music);
+
+ if (!audioData) {
+ return null;
+ }
+
+ return (
+ <div>This file has a {audioData.sampleRate} sampleRate.</div>
+ );
+}
+```
+
+## See also
+
+- [`getAudioData()`](get-audio-data)
+- [`visualizeAudio()`](visualize-audio)
+- [Audio visualization](audio-visualization)