Skip to main content

useWindowedAudioData()v4.0.240

@remotion/media-utils 工具函数包的一部分。

🌐 Part of the @remotion/media-utils package of helper functions.

这是 useAudioData() 的一种替代方案,它只加载当前帧周围的一部分音频。 它支持所有 Mediabunny 支持的格式

🌐 This is an alternative to useAudioData() that only loads a portion of the audio around the current frame.
It supports all Mediabunny-supported formats.

在 v4.0.383 之前,只支持 WAV 文件。

🌐 Before v4.0.383, only WAV files were supported.

useAudioData() 不同,后者将所有音频数据保存在内存中,此函数通过 HTTP Range 请求只加载当前帧周围的音频数据。
我们建议在可视化长时长音频时使用此函数。

🌐 Unlike useAudioData(), which keeps all of the audio data in memory, this function makes HTTP Range requests to only load the audio data around the current frame.
We recommend using this function for visualizing audio with a long duration.

info

远程音频文件需要支持 CORS

更多信息

示例

🌐 Example

import {useWindowedAudioData, visualizeAudio} from '@remotion/media-utils';
import {staticFile, useCurrentFrame, useVideoConfig} from 'remotion';

export const MyComponent: React.FC = () => {
  const {fps} = useVideoConfig();
  const frame = useCurrentFrame();
  const {audioData, dataOffsetInSeconds} = useWindowedAudioData({
    src: staticFile('podcast.wav'),
    frame,
    fps,
    windowInSeconds: 10,
  });

  if (!audioData) {
    return null;
  }

  const visualization = visualizeAudio({
    fps,
    frame,
    audioData,
    numberOfSamples: 16,
    dataOffsetInSeconds,
  });

  return null;
};

参数

🌐 Arguments

一个带有以下特性的对象:

🌐 An object with:

src

指向音频资源的字符串。

🌐 A string pointing to an audio asset.

frame

number

作品的当前画面。

🌐 The current frame of the composition.

fps

number

合成的每秒帧数。应从 useVideoConfig() 获取。

🌐 The frames per second of the composition. Should be taken from useVideoConfig().

windowInSeconds

number

音频将被分割成这种长度的窗口。 该函数将加载当前帧及前后窗口周围的音频数据。

🌐 The audio will be segmented into windows of this length.
The function will load the audio data around the current frame and the windows before and after.

在此示例中,窗口长度为10秒,因此该函数将加载当前窗口以及前一个和下一个窗口,最多一次可加载30秒的音频。

🌐 In this example, the window is 10 seconds long, so the function will load the current window as well as the previous and next one, leading to up to 30 seconds of audio being loaded at a time.

返回值

🌐 Return value

一个带有以下特性的对象:

🌐 An object with:

audioData

AudioData | null

一个包含音频数据的对象(参见 getAudioData() 的文档)或 null,如果数据尚未加载。

🌐 An object containing audio data (see documentation of getAudioData()) or null if the data has not been loaded.

dataOffsetInSeconds

number

当前加载的音频数据的偏移秒数。 你应该将其传递给 visualizeAudio()

🌐 The offset in seconds of the audio data that is currently loaded.
You should pass it through to visualizeAudio().

另请参阅

🌐 See also