Creating an Audio Recorder with JavaScript: A Step-by-Step Guide

0
555

Welcome to this step-by-step guide on creating your very own audio recorder using JavaScript. In this tutorial, we’ll walk through the process of harnessing the power of the web browser’s capabilities to capture and play back audio. Whether you’re building a voice memo app or simply exploring the world of web audio, this guide has you covered.

Getting Started

Prerequisites

Before we dive in, make sure you have a basic understanding of HTML, CSS, and JavaScript. Additionally, you’ll need a modern web browser that supports the MediaRecorder API.

Setting Up the Project

Create a new HTML file and include the necessary elements, such as buttons for recording, stopping, and playing, and an <audio> element for playback. Style your interface with CSS to make it visually appealing.

Capturing Audio with the MediaRecorder API

We’ll leverage the MediaRecorder API to capture audio from the user’s microphone. Learn how to request microphone access and handle the recording process.

Handling Recording Controls

Implement functionality for starting, stopping, and playing back the recorded audio. We’ll use event listeners and the <audio> element to provide a seamless user experience.

Putting It All Together

Combine the HTML, CSS, and JavaScript to create a cohesive audio recorder. Test your application and make any necessary adjustments.

Follow this video for complete guidance :

Complete Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audio Recorder</title>
    <style>
      body {
          font-family: 'Arial', sans-serif;
          display: flex;
          align-items: center;
          justify-content: center;
          height: 100vh;
          margin: 0;
      }

      .container {
          text-align: center;
      }

      button {
          font-size: 16px;
          margin: 5px;
      }

    </style>
</head>
<body>
    <div class="container">
        <button id="startRecording">Start Recording</button>
        <button id="stopRecording" disabled>Stop Recording</button>
        <button id="playRecording" disabled>Play Recording</button>
        <audio id="audioPlayer" controls></audio>
    </div>

    <script>
    document.addEventListener('DOMContentLoaded', () => {
    const startRecordingButton = document.getElementById('startRecording');
    const stopRecordingButton = document.getElementById('stopRecording');
    const playRecordingButton = document.getElementById('playRecording');
    const audioPlayer = document.getElementById('audioPlayer');

    let mediaRecorder;
    let chunks = [];

    navigator.mediaDevices.getUserMedia({ audio: true })
        .then((stream) => {
            mediaRecorder = new MediaRecorder(stream);

            mediaRecorder.ondataavailable = (event) => {
                if (event.data.size > 0) {
                    chunks.push(event.data);
                }
            };

            mediaRecorder.onstop = () => {
                const audioBlob = new Blob(chunks, { type: 'audio/wav' });
                const audioUrl = URL.createObjectURL(audioBlob);
                audioPlayer.src = audioUrl;
                playRecordingButton.disabled = false;
            };

            startRecordingButton.addEventListener('click', () => {
                mediaRecorder.start();
                startRecordingButton.disabled = true;
                stopRecordingButton.disabled = false;
            });

            stopRecordingButton.addEventListener('click', () => {
                mediaRecorder.stop();
                startRecordingButton.disabled = false;
                stopRecordingButton.disabled = true;
            });

            playRecordingButton.addEventListener('click', () => {
                audioPlayer.play();
            });
        })
        .catch((error) => {
            console.error('Error accessing microphone:', error);
        });
});

</script>
</body>
</html>

 

Comments are closed.