Audio Recorder Tool
Audio Recorder
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background-color: #ffffff;
border-radius: 10px;
padding: 40px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
width: 90%;
max-width: 400px;
}
.title {
font-size: 24px;
color: #333;
margin-bottom: 20px;
}
.controls {
margin-bottom: 20px;
}
.btn {
padding: 10px 20px;
margin: 10px;
font-size: 16px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.record-btn {
background-color: #ff5733;
color: white;
}
.stop-btn {
background-color: #33b5ff;
color: white;
}
.play-btn {
background-color: #75e300;
color: white;
}
.download-btn {
background-color: #d8b200;
color: white;
}
.record-btn:hover {
background-color: #e24e27;
}
.stop-btn:hover {
background-color: #2b9fd2;
}
.play-btn:hover {
background-color: #63c900;
}
.download-btn:hover {
background-color: #c59c00;
}
audio {
width: 100%;
margin-top: 20px;
}
let isRecording = false;
let mediaRecorder;
let audioChunks = [];
let audioUrl;
let audioBlob;
const recordBtn = document.getElementById("recordBtn");
const stopBtn = document.getElementById("stopBtn");
const playBtn = document.getElementById("playBtn");
const downloadBtn = document.getElementById("downloadBtn");
const audioPlayer = document.getElementById("audioPlayer");
const constraints = { audio: true };
// Start recording when the user clicks the "Record" button
recordBtn.addEventListener("click", async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.onstart = () => {
isRecording = true;
recordBtn.disabled = true;
stopBtn.disabled = false;
};
mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
isRecording = false;
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
audioUrl = URL.createObjectURL(audioBlob);
audioPlayer.src = audioUrl;
playBtn.disabled = false;
downloadBtn.disabled = false;
};
mediaRecorder.start();
} catch (err) {
console.error("Error accessing microphone", err);
}
});
// Stop recording when the user clicks the "Stop" button
stopBtn.addEventListener("click", () => {
if (isRecording) {
mediaRecorder.stop();
recordBtn.disabled = false;
stopBtn.disabled = true;
}
});
// Play the recorded audio when the user clicks the "Play" button
playBtn.addEventListener("click", () => {
audioPlayer.play();
});
// Download the recorded audio when the user clicks the "Download" button
downloadBtn.addEventListener("click", () => {
const a = document.createElement("a");
a.href = audioUrl;
a.download = "recorded-audio.wav";
a.click();
});