How to Create an Audio Recorder with Pause and Download Functionality Using JavaScript

How to Create an Audio Recorder with Pause and Download Functionality Using JavaScript

Creating an audio recorder with pause and obtain performance utilizing JavaScript is an effective way so as to add interactive options to your net software. This tutorial will information you thru the steps to construct this characteristic utilizing fashionable JavaScript APIs.

Know More :- https://article.shade.cool/p/32

Prerequisites

Before we start, be sure you have a primary understanding of HTML, CSS, and JavaScript. You’ll additionally want a contemporary net browser that helps the Web Audio API and MediaRecorder API.

Step 1: Setting Up the HTML

First, let’s create the HTML construction for our audio recorder. We’ll want buttons to start out, pause, and obtain the recording, in addition to an audio ingredient to play again the recording.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta identify="viewport" content material="width=device-width, initial-scale=1.0">
  <title>Audio Recorder</title>
  <model>
    physique {
      font-family: Arial, sans-serif;
      show: flex;
      flex-direction: column;
      align-items: heart;
      margin-top: 50px;
    }
    button {
      margin: 10px;
    }
  </model>
</head>
<physique>
  <h1>Audio Recorder</h1>
  <button id="startButton">Start Recording</button>
  <button id="pauseButton" disabled>Pause Recording</button>
  <button id="downloadButton" disabled>Download Recording</button>
  <audio id="audioPlayback" controls></audio>

  <script src="recorder.js"></script>
</physique>
</html>

Step 2: Accessing the Microphone

Next, we’ll use the MediaDevices API to entry the consumer’s microphone. We’ll request permission to make use of the microphone and deal with the consumer’s response.

// recorder.js

let mediaRecorder;
let recordedChunks = [];

async perform getMicrophoneEntry() {
  strive {
    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
    handleSuccess(stream);
  } catch (err) {
    console.error('Error accessing microphone:', err);
  }
}

perform handleSuccess(stream) {
  mediaRecorder = new MediaRecorder(stream);

  mediaRecorder.ondataavailable = (occasion) => {
    if (occasion.knowledge.dimension > 0) {
      recordedChunks.push(occasion.knowledge);
    }
  };

  mediaRecorder.onstop = () => {
    const blob = new Blob(recordedChunks, {
      kind: 'audio/webm'
    });
    const url = URL.createObjectURL(blob);
    const audioPlayback = doc.getElementById('audioPlayback');
    audioPlayback.src = url;

    const downloadButton = doc.getElementById('downloadButton');
    downloadButton.href = url;
    downloadButton.obtain = 'recording.webm';
    downloadButton.disabled = false;
  };
}

getMicrophoneEntry();

Step 3: Implementing Start and Pause Functionality

We’ll add occasion listeners to the buttons to regulate the recording. The begin button will begin the recording, the pause button will pause or resume the recording relying on its state, and the cease button will cease the recording and allow the obtain button.

// recorder.js

doc.getElementById('startButton').addEventListener('click on', () => {
  if (mediaRecorder.state === 'inactive') {
    recordedChunks = [];
    mediaRecorder.begin();
    doc.getElementById('startButton').disabled = true;
    doc.getElementById('pauseButton').disabled = false;
    doc.getElementById('downloadButton').disabled = true;
  }
});

doc.getElementById('pauseButton').addEventListener('click on', () => {
  if (mediaRecorder.state === 'recording') {
    mediaRecorder.pause();
    doc.getElementById('pauseButton').textContent = 'Resume Recording';
  } else if (mediaRecorder.state === 'paused') {
    mediaRecorder.resume();
    doc.getElementById('pauseButton').textContent = 'Pause Recording';
  }
});

doc.getElementById('stopButton').addEventListener('click on', () => {
  if (mediaRecorder.state !== 'inactive') {
    mediaRecorder.cease();
    doc.getElementById('startButton').disabled = false;
    doc.getElementById('pauseButton').disabled = true;
    doc.getElementById('pauseButton').textContent = 'Pause Recording';
  }
});

Step 4: Implementing the Download Functionality

Finally, we’ll implement the obtain performance by making a Blob from the recorded audio knowledge and producing a obtain hyperlink.

// recorder.js

doc.getElementById('downloadButton').addEventListener('click on', () => {
  if (recordedChunks.size > 0) {
    const blob = new Blob(recordedChunks, { kind: 'audio/webm' });
    const url = URL.createObjectURL(blob);
    const a = doc.createElement('a');
    a.model.show = 'none';
    a.href = url;
    a.obtain = 'recording.webm';
    doc.physique.appendChild(a);
    a.click on();
    window.URL.revokeObjectURL(url);
  }
});

Conclusion

You’ve now created a primary audio recorder with pause and obtain performance utilizing JavaScript. This instance may be prolonged and customised to suit the wants of your software, corresponding to including extra controls, enhancing the UI, or supporting totally different audio codecs.

Feel free to discover the capabilities of the MediaRecorder API and the Web Audio API to boost your audio recording performance additional. Happy coding!

HI-FI News

through DEV Community https://dev.to

June 27, 2024 at 07:23PM