-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_voice_recorder_starter.html
More file actions
152 lines (125 loc) · 4.67 KB
/
Copy path08_voice_recorder_starter.html
File metadata and controls
152 lines (125 loc) · 4.67 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voice Recorder to n8n</title>
<style>
body { font-family: Arial, sans-serif; max-width: 720px; margin: 40px auto; padding: 0 16px; }
button { margin-right: 8px; margin-bottom: 12px; padding: 10px 16px; }
#status, #result { margin-top: 12px; white-space: pre-wrap; }
audio { display: block; margin-top: 12px; width: 100%; }
</style>
</head>
<body>
<h1>Voice Recorder → n8n Webhook</h1>
<button id="startBtn">Start Recording</button>
<button id="stopBtn" disabled>Stop Recording</button>
<button id="uploadBtn" disabled>Upload to n8n</button>
<button id="goTestBtn">Go Testing</button>
<audio id="player" controls></audio>
<div id="status"></div>
<div id="result"></div>
<script>
let webhookUrl = "https://stdio2026.app.n8n.cloud/webhook/02334597-01bd-41dd-a7c9-f49b8889cf83";
const startBtn = document.getElementById("startBtn");
const stopBtn = document.getElementById("stopBtn");
const uploadBtn = document.getElementById("uploadBtn");
const player = document.getElementById("player");
const statusEl = document.getElementById("status");
const resultEl = document.getElementById("result");
let mediaRecorder = null;
let audioChunks = [];
let audioBlob = null;
let stream = null;
function setStatus(text) {
statusEl.textContent = text;
}
startBtn.addEventListener("click", async () => {
try {
resultEl.textContent = "";
audioChunks = [];
audioBlob = null;
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
let mimeType = "";
if (MediaRecorder.isTypeSupported("audio/webm")) {
mimeType = "audio/webm";
} else if (MediaRecorder.isTypeSupported("audio/mp4")) {
mimeType = "audio/mp4";
}
mediaRecorder = mimeType
? new MediaRecorder(stream, { mimeType })
: new MediaRecorder(stream);
mediaRecorder.ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
audioChunks.push(event.data);
}
};
mediaRecorder.onstop = () => {
const finalType = mimeType || audioChunks[0]?.type || "audio/webm";
const extension = finalType.includes("mp4") ? "m4a" : "webm";
audioBlob = new Blob(audioChunks, { type: finalType });
const audioUrl = URL.createObjectURL(audioBlob);
player.src = audioUrl;
uploadBtn.disabled = false;
setStatus(`Recording stopped. File ready: recording.${extension}`);
};
mediaRecorder.start();
setStatus("Recording...");
startBtn.disabled = true;
stopBtn.disabled = false;
uploadBtn.disabled = true;
} catch (error) {
console.error(error);
setStatus("Cannot access microphone: " + error.message);
}
});
stopBtn.addEventListener("click", () => {
if (mediaRecorder && mediaRecorder.state !== "inactive") {
mediaRecorder.stop();
}
if (stream) {
stream.getTracks().forEach(track => track.stop());
}
startBtn.disabled = false;
stopBtn.disabled = true;
});
uploadBtn.addEventListener("click", async () => {
if (!audioBlob) {
setStatus("No audio to upload.");
return;
}
try {
setStatus("Uploading to n8n...");
const mimeType = audioBlob.type || "audio/webm";
const extension = mimeType.includes("mp4") ? "m4a" : "webm";
const file = new File([audioBlob], `recording.${extension}`, { type: mimeType });
const formData = new FormData();
formData.append("audio", file);
formData.append("userId", "demo-user");
formData.append("note", "Recorded from browser");
const response = await fetch(webhookUrl, {
method: "POST",
body: formData
});
const contentType = response.headers.get("content-type") || "";
let data;
if (contentType.includes("application/json")) {
data = await response.json();
resultEl.textContent = JSON.stringify(data, null, 2);
} else {
data = await response.text();
resultEl.textContent = data;
}
setStatus(`Upload complete. HTTP ${response.status}`);
} catch (error) {
console.error(error);
setStatus("Upload failed: " + error.message);
}
});
goTestBtn.onclick = function () {
webhookUrl = "https://stdio2026.app.n8n.cloud/webhook-test/02334597-01bd-41dd-a7c9-f49b8889cf83";
};
</script>
</body>
</html>