Javascript - 从 mp3 文件录制音频

Javascript - Record audio from mp3 file

我想使用 MediaRecorder 来录制现有的 mp3 文件。在调用 captureStream() 之后,我尝试将 Audio 元素作为源传递给 MediaRecorder,但它不起作用。

new MediaRecorder(new Audio('./audio.mp3').captureStream(), {
    audioBitsPerSecond: 16000
});

错误:

Uncaught DOMException: Failed to execute 'start' on 'MediaRecorder': The MediaRecorder cannot start becausethere are no audio or video tracks available.

我该怎么做?

您需要 play() 那个音频...(等待它确实如此),否则您的流中没有任何内容可以录制。

const aud = new Audio('./audio.mp3');
aud.play().then( () => {
  const stream = aud.captureStream();
  const recorder = new MediaRecorder(stream);
  recorder.ondataavailable = ...

});