简体   繁体   中英

Javascript - Record audio from mp3 file

I want to use MediaRecorder in order to record an existing mp3 file. I tried to pass an Audio element as a source to MediaRecorder , after calling captureStream() but it doesn't works.

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

The error:

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

How can I do that?

You need to play() that audio... (and wait it actually does), otherwise there is nothing in your stream to get recorded.

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM