Skip to content Skip to sidebar Skip to footer

How To Cut An Audio Recording In Javascript

Say I record audio from my microphone, using Recorder.js. How can I then trim/crop/slice/cut the recording? Meaning, say I record 3.5 seconds, and I want only the first 3 seconds.

Solution 1:

You can stop the original recording at 3 seconds or use <audio> element, HTMLMediaElement.captureStream() and MediaRecorder to record 3 seconds of playback of the original recording.

const audio = new Audio(/* Blob URL or URL of recording */);
const chunks = [];
audio.oncanplay = e => {
  audio.play();
  const stream = audio.captureStream();
  const recorder = new MediaRecorder(stream);
  recorder.ondataavailable = e => {
    if (recorder.state === "recording") {
      recorder.stop()
    };
    chunks.push(e.data)
  }
  recorder.onstop = e => {
    console.log(chunks)
  }
  recorder.start(3000);
}

Post a Comment for "How To Cut An Audio Recording In Javascript"