|
1 |
| -import { FC, useState } from "react"; |
| 1 | +import { FC, useState, useEffect } from "react"; |
2 | 2 |
|
3 | 3 | const RecordAudio: FC<{ onCreated: (blob: Blob) => void }> = ({
|
4 | 4 | onCreated,
|
5 | 5 | }) => {
|
6 | 6 | const [isRecording, setIsRecording] = useState(false);
|
7 | 7 |
|
8 |
| - const startRecording = async () => { |
9 |
| - const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
| 8 | + // stop after 10s |
| 9 | + useEffect(() => { |
| 10 | + if (!isRecording) return; |
10 | 11 |
|
11 |
| - const recorder = new MediaRecorder(stream); |
12 |
| - const chunks: Blob[] = []; |
13 |
| - recorder.ondataavailable = (event) => { |
14 |
| - chunks.push(event.data); |
15 |
| - }; |
16 |
| - recorder.onstop = () => { |
17 |
| - const audioBlob = new Blob(chunks, { type: recorder.mimeType }); |
18 |
| - onCreated(audioBlob); |
19 |
| - }; |
| 12 | + const timer = setTimeout(() => setIsRecording(false), 10000); |
| 13 | + |
| 14 | + return () => clearTimeout(timer); |
| 15 | + }, [isRecording]); |
| 16 | + |
| 17 | + // |
| 18 | + useEffect(() => { |
| 19 | + if (!isRecording) return; |
| 20 | + |
| 21 | + const stream = navigator.mediaDevices.getUserMedia({ audio: true }); |
| 22 | + |
| 23 | + const recorder = stream.then((stream) => { |
| 24 | + const recorder = new MediaRecorder(stream); |
| 25 | + const chunks: Blob[] = []; |
| 26 | + recorder.ondataavailable = (event) => { |
| 27 | + chunks.push(event.data); |
| 28 | + }; |
| 29 | + recorder.onstop = () => { |
| 30 | + const audioBlob = new Blob(chunks, { type: recorder.mimeType }); |
20 | 31 |
|
21 |
| - recorder.start(); |
22 |
| - setIsRecording(true); |
23 |
| - setTimeout(() => { |
24 |
| - recorder?.stop(); |
25 |
| - setIsRecording(false); |
| 32 | + if (audioBlob.size < 1500) { |
| 33 | + console.warn("Skipping small blob", audioBlob); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + onCreated(audioBlob); |
| 38 | + }; |
| 39 | + |
| 40 | + recorder.start(); |
| 41 | + |
| 42 | + return recorder; |
| 43 | + }); |
| 44 | + |
| 45 | + return () => { |
| 46 | + recorder.then((recorder) => recorder.stop()); |
| 47 | + stream.then((stream) => |
| 48 | + stream.getTracks().forEach((track) => track.stop()) |
| 49 | + ); |
| 50 | + }; |
26 | 51 |
|
27 |
| - for (const track of stream.getTracks()) { |
28 |
| - track.stop(); |
29 |
| - } |
30 |
| - }, 5000); // Stop recording after 5 seconds |
31 |
| - }; |
| 52 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 53 | + }, [isRecording]); |
32 | 54 |
|
33 | 55 | return (
|
34 | 56 | <div>
|
35 |
| - <button onClick={startRecording} disabled={isRecording}> |
36 |
| - {isRecording ? "Recording..." : "Add Recording"} |
| 57 | + <button |
| 58 | + onClick={() => setIsRecording((prev) => !prev)} |
| 59 | + style={{ background: isRecording ? "red" : "" }} |
| 60 | + > |
| 61 | + {isRecording ? "Stop Recording" : "Start Recording"} |
37 | 62 | </button>
|
38 | 63 | </div>
|
39 | 64 | );
|
|
0 commit comments