-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvoice-record.html
More file actions
179 lines (153 loc) · 6.18 KB
/
voice-record.html
File metadata and controls
179 lines (153 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Voice Memo Recorder</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
img, video, canvas, svg { max-width: 100%; height: auto; }
@import url('https://fonts.googleapis.com/css2?family=Oswald:wght@200..700&display=swap');
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; padding: 1.5rem; max-width: 32.5rem; margin: 0 auto; }
h2 {
font-family: "Oswald", serif;
font-size: 1.5em; line-height: 1.1; text-transform: uppercase; letter-spacing: .05em;
margin: 0 0 0.9375rem; padding: 0.3125rem 0.625rem; position: relative;
}
.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
button.record {
font-family: "Oswald", serif;
margin-top: 0.625rem; width: 100%;
background: #007bff; color: #fff; font-size: 1rem; padding: 0.625rem;
border: none; border-radius: 0.25rem; cursor: pointer; font-weight: bold;
transition: background .3s ease;
}
button.record:hover { background: #0056b3; }
button.record:disabled {
background: #7aaef0; cursor: not-allowed;
}
audio { margin-top: 1rem; width: 100%; display: block; }
#downloadButton {
font-family: "Oswald", serif;
display: none; margin-top: 0.625rem; padding: 0.625rem 0.75rem; border-radius: 0.25rem;
background-color: #0056b3; color: #fff; text-decoration: none; font-weight: 700;
text-align: center; display: inline-block;
}
#status { margin-top: 0.5rem; font-size: 0.875rem; opacity: .8; }
</style>
</head>
<body>
<h2>Voice Memo Recorder</h2>
<div class="controls">
<button id="startButton" class="record">RECORD</button>
<button id="stopButton" class="record" disabled>STOP</button>
</div>
<div id="status" aria-live="polite"></div>
<audio id="audioPlayback" controls></audio>
<a id="downloadButton" download="voice-memo.webm">DOWNLOAD</a>
<script>
const startButton = document.getElementById("startButton");
const stopButton = document.getElementById("stopButton");
const audioPlayback = document.getElementById("audioPlayback");
const downloadButton = document.getElementById("downloadButton");
const statusEl = document.getElementById("status");
let mediaRecorder;
let audioChunks = [];
let stream;
let chosenMimeType = null;
function pickMimeType() {
if (!window.MediaRecorder) return null;
const prefs = [
"audio/webm;codecs=opus",
"audio/webm",
"audio/ogg;codecs=opus",
"audio/ogg",
"audio/mp4" // Safari (iOS 14+)
];
for (const t of prefs) {
if (MediaRecorder.isTypeSupported && MediaRecorder.isTypeSupported(t)) return t;
}
return null; // Let the browser choose default
}
function updateStatus(msg) { statusEl.textContent = msg || ""; }
function setUIRecordingState(isRecording) {
startButton.disabled = isRecording;
stopButton.disabled = !isRecording;
if (isRecording) {
downloadButton.style.display = "none";
updateStatus("Recording… press STOP when you’re done.");
} else {
updateStatus("");
}
}
async function startRecording() {
if (!navigator.mediaDevices?.getUserMedia) {
alert("getUserMedia is not supported in this browser.");
return;
}
// Secure context check
if (location.protocol !== "https:" && location.hostname !== "localhost") {
updateStatus("Tip: Recording requires HTTPS (or http://localhost).");
}
try {
stream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
}
});
chosenMimeType = pickMimeType();
mediaRecorder = chosenMimeType
? new MediaRecorder(stream, { mimeType: chosenMimeType })
: new MediaRecorder(stream);
audioChunks = [];
mediaRecorder.addEventListener("dataavailable", (event) => {
if (event.data && event.data.size > 0) audioChunks.push(event.data);
});
mediaRecorder.addEventListener("stop", () => {
const type = chosenMimeType || "audio/webm";
const audioBlob = new Blob(audioChunks, { type });
const audioURL = URL.createObjectURL(audioBlob);
audioPlayback.src = audioURL;
// File extension based on mime
const ext = type.includes("ogg") ? "ogg"
: type.includes("mp4") ? "m4a"
: "webm";
downloadButton.download = `voice-memo.${ext}`;
downloadButton.href = audioURL;
downloadButton.style.display = "inline-block";
// Release mic
if (stream) {
stream.getTracks().forEach(t => t.stop());
stream = null;
}
setUIRecordingState(false);
updateStatus("Recording ready. You can play or download it.");
});
mediaRecorder.start(); // you can pass a timeslice (ms) if you want periodic dataavailable
setUIRecordingState(true);
} catch (error) {
console.error("Microphone access error:", error);
alert("Could not access your microphone. Please check site permissions and that you’re on HTTPS (or localhost).");
setUIRecordingState(false);
}
}
function stopRecording() {
try {
if (mediaRecorder && mediaRecorder.state === "recording") {
mediaRecorder.stop();
updateStatus("Finishing up…");
} else {
updateStatus("No active recording to stop.");
}
} catch (e) {
console.error("Stop error:", e);
updateStatus("Couldn’t stop recording. See console for details.");
}
}
startButton.addEventListener("click", startRecording);
stopButton.addEventListener("click", stopRecording);
</script>
</body>
</html>