AudioBuffer 不可缓存/decodeAudioData 需要很长时间

AudioBuffer not cachable / decodeAudioData takes to long

我正在尝试使用网络音频 Api 为音乐编写自定义网络播放器 Api,但我 运行 遇到解码音频的问题。

我正在从后端获取 .mp3 作为 ArrayBuffer 并使用 AudioContext.decodeAudioData 函数对其进行解码。

  const resp = await fetch(url);
  const raw = await resp.arrayBuffer();
  const audioBuffer = context.decodeAudioData(raw);

我正在尝试将 AudioBuffer 缓存在 IndexedDB 中,但它不可克隆。在我的设备上使用普通 mp3 进行解码大约需要 10 秒,那是相当长的时间。我是否有任何其他选择来防止歌曲之间的长时间等待(除了预取和 "predecoding")?

AudioBuffer 不适用于大段音频

This interface represents a memory-resident audio asset (for one-shot sounds and other short audio clips). Its format is non-interleaved IEEE 32-bit linear PCM with a nominal range of -1 -> +1. It can contain one or more channels. Typically, it would be expected that the length of the PCM data would be fairly short (usually somewhat less than a minute). For longer sounds, such as music soundtracks, streaming should be used with the audio element and MediaElementAudioSourceNode.

(强调我的)

https://www.w3.org/TR/webaudio/#AudioBuffer

如果您确实需要即时操作音频,您可能需要查看 MediaElementAudioSourceNode。否则一个简单的音频元素就足够了。