Skip to content
This repository was archived by the owner on Jun 1, 2024. It is now read-only.

Commit e5fd889

Browse files
author
MPatterson17
committed
Added a timer to the recorder component that ticks up when a user is recording
1 parent c84c99e commit e5fd889

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

components/recorder.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export default function Recorder({ submit }) {
1919
const [isBlocked, setIsBlocked] = useState(false);
2020
const [recorder, setRecorder] = useState(new MicRecorder());
2121
const dispatch = useDispatch();
22+
const [min, setMinute] = useState(0);
23+
const [sec, setSecond] = useState(0);
2224

2325
const startRecording = (ev) => {
2426
console.log('startRecording', ev);
@@ -85,6 +87,28 @@ export default function Recorder({ submit }) {
8587
}
8688
}, []);
8789

90+
useEffect(() => {
91+
let interval = null;
92+
if (isRecording) {
93+
interval = setInterval(() => {
94+
setSecond(sec => sec + 1);
95+
if (sec == 59) {
96+
setMinute(min => min + 1);
97+
setSecond(sec => 0);
98+
}
99+
if (min == 99) {
100+
setMinute(min => 0);
101+
setSecond(sec => 0);
102+
}
103+
}, 1000);
104+
} else if (!isRecording && sec !== 0) {
105+
setMinute(min => 0);
106+
setSecond(sec => 0);
107+
clearInterval(interval);
108+
}
109+
return () => { clearInterval(interval); };
110+
}, [isRecording, sec]);
111+
88112
return (
89113
<Row>
90114
<Col>
@@ -113,17 +137,18 @@ export default function Recorder({ submit }) {
113137
<Col>
114138
{/* eslint-disable-next-line jsx-a11y/media-has-caption */}
115139
<audio src={blobURL} />
116-
140+
117141
{isRecording ? (
118142
<Button onClick={stopRecording}>
119-
<FaStop /> Stop Recording
143+
<FaStop /> {String(min).padStart(2, '0')}:{String(sec).padStart(2, '0')}
120144
</Button>
121145
) : (
122146
<Button onClick={startRecording}>
123-
<FaMicrophone />
147+
<FaMicrophone />
124148
</Button>
125149
)}
126150
</Col>
127151
</Row>
128152
);
153+
129154
}

0 commit comments

Comments
 (0)