Skip to content

Commit 50ba66a

Browse files
committed
privilege ogg, use extension in submitting file
1 parent d480521 commit 50ba66a

File tree

1 file changed

+58
-57
lines changed

1 file changed

+58
-57
lines changed

components/recorder.js

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
FaVolumeDown,
1515
FaVolumeUp,
1616
FaRegTrashAlt,
17-
FaDownload
17+
FaDownload,
1818
} from 'react-icons/fa';
1919
import { useDispatch, useSelector } from 'react-redux';
2020
import ListGroup from 'react-bootstrap/ListGroup';
@@ -89,7 +89,7 @@ function AudioViewer({ src }) {
8989
height: '1.05em',
9090
cursor: 'pointer',
9191
color: 'red',
92-
paddingLeft: '2px'
92+
paddingLeft: '2px',
9393
}}
9494
onClick={toggleVolume}
9595
/>
@@ -112,7 +112,7 @@ function AudioViewer({ src }) {
112112
width: '1.23em',
113113
height: '1.23em',
114114
cursor: 'pointer',
115-
paddingLeft: '3px'
115+
paddingLeft: '3px',
116116
}}
117117
onClick={toggleVolume}
118118
/>
@@ -131,7 +131,7 @@ function AudioViewer({ src }) {
131131
cursorWidth: 3,
132132
height: 200,
133133
barGap: 3,
134-
dragToSeek: true
134+
dragToSeek: true,
135135
// plugins:[
136136
// WaveSurferRegions.create({maxLength: 60}),
137137
// WaveSurferTimeLinePlugin.create({container: containerT.current})
@@ -161,7 +161,7 @@ function AudioViewer({ src }) {
161161
flexDirection: 'column',
162162
justifyContent: 'center',
163163
alignItems: 'center',
164-
margin: '0 1rem 0 1rem'
164+
margin: '0 1rem 0 1rem',
165165
}}
166166
>
167167
<div
@@ -173,7 +173,7 @@ function AudioViewer({ src }) {
173173
style={{
174174
display: 'flex',
175175
justifyContent: 'center',
176-
alignItems: 'center'
176+
alignItems: 'center',
177177
}}
178178
>
179179
<Button
@@ -185,7 +185,7 @@ function AudioViewer({ src }) {
185185
width: '40px',
186186
height: '40px',
187187
borderRadius: '50%',
188-
padding: '0'
188+
padding: '0',
189189
}}
190190
onClick={playPause}
191191
>
@@ -220,13 +220,13 @@ export default function Recorder({ submit, accompaniment }) {
220220

221221
const getSupportedMimeType = () => {
222222
const types = [
223+
'audio/ogg;codecs=opus',
223224
'audio/webm',
224225
'audio/webm;codecs=opus',
225-
'audio/ogg;codecs=opus',
226226
'audio/mp4',
227-
'audio/mpeg'
227+
'audio/mpeg',
228228
];
229-
return types.find(type => MediaRecorder.isTypeSupported(type)) || null;
229+
return types.find((type) => MediaRecorder.isTypeSupported(type)) || null;
230230
};
231231
const [min, setMinute] = useState(0);
232232
const [sec, setSecond] = useState(0);
@@ -236,14 +236,11 @@ export default function Recorder({ submit, accompaniment }) {
236236
const router = useRouter();
237237
const { slug, piece, actCategory, partType } = router.query;
238238

239-
useEffect(
240-
() => {
241-
setBlobInfo([]);
242-
setBlobURL('');
243-
setBlobData();
244-
},
245-
[partType]
246-
);
239+
useEffect(() => {
240+
setBlobInfo([]);
241+
setBlobURL('');
242+
setBlobData();
243+
}, [partType]);
247244

248245
const startRecording = () => {
249246
if (isBlocked) {
@@ -263,7 +260,7 @@ export default function Recorder({ submit, accompaniment }) {
263260
mediaRecorder.stop();
264261
};
265262

266-
const downloadRecording = i => {
263+
const downloadRecording = (i) => {
267264
const url = window.URL.createObjectURL(blobInfo[i].data);
268265
const a = document.createElement('a');
269266
a.style.display = 'none';
@@ -283,12 +280,19 @@ export default function Recorder({ submit, accompaniment }) {
283280
};
284281

285282
const submitRecording = (i, submissionId) => {
283+
const extension = mimeType.includes('webm')
284+
? 'webm'
285+
: mimeType.includes('ogg')
286+
? 'ogg'
287+
: mimeType.includes('mp4')
288+
? 'm4a'
289+
: 'wav';
286290
const formData = new FormData(); // TODO: make filename reflect assignment
287291
formData.append(
288292
'file',
289-
new File([blobInfo[i].data], 'student-recording', {
290-
type: mimeType
291-
})
293+
new File([blobInfo[i].data], `student-recording-${i}.${extension}`, {
294+
type: mimeType,
295+
}),
292296
);
293297
// dispatch(submit({ audio: formData }));
294298
submit({ audio: formData, submissionId });
@@ -314,10 +318,10 @@ export default function Recorder({ submit, accompaniment }) {
314318
autoGainControl: false,
315319
channelCount: 1,
316320
sampleRate: 48000,
317-
latency: 0
318-
}
321+
latency: 0,
322+
},
319323
})
320-
.then(stream => {
324+
.then((stream) => {
321325
const supportedType = getSupportedMimeType();
322326
if (!supportedType) {
323327
console.error('No supported audio MIME type found');
@@ -327,10 +331,10 @@ export default function Recorder({ submit, accompaniment }) {
327331
setMimeType(supportedType);
328332

329333
const recorder = new MediaRecorder(stream, {
330-
mimeType: supportedType
334+
mimeType: supportedType,
331335
});
332336

333-
recorder.ondataavailable = e => {
337+
recorder.ondataavailable = (e) => {
334338
if (e.data.size > 0) {
335339
chunksRef.current.push(e.data);
336340
}
@@ -341,12 +345,12 @@ export default function Recorder({ submit, accompaniment }) {
341345
setBlobData(blob);
342346
const url = URL.createObjectURL(blob);
343347
setBlobURL(url);
344-
setBlobInfo(prevInfo => [
348+
setBlobInfo((prevInfo) => [
345349
...prevInfo,
346350
{
347351
url,
348-
data: blob
349-
}
352+
data: blob,
353+
},
350354
]);
351355
setIsRecording(false);
352356
chunksRef.current = [];
@@ -355,39 +359,36 @@ export default function Recorder({ submit, accompaniment }) {
355359
setMediaRecorder(recorder);
356360
setIsBlocked(false);
357361
})
358-
.catch(err => {
362+
.catch((err) => {
359363
console.log('Permission Denied');
360364
setIsBlocked(true);
361365
});
362366
}
363367
}, []);
364368

365-
useEffect(
366-
() => {
367-
let interval = null;
368-
if (isRecording) {
369-
interval = setInterval(() => {
370-
setSecond(sec + 1);
371-
if (sec === 59) {
372-
setMinute(min + 1);
373-
setSecond(0);
374-
}
375-
if (min === 99) {
376-
setMinute(0);
377-
setSecond(0);
378-
}
379-
}, 1000);
380-
} else if (!isRecording && sec !== 0) {
381-
setMinute(0);
382-
setSecond(0);
383-
clearInterval(interval);
384-
}
385-
return () => {
386-
clearInterval(interval);
387-
};
388-
},
389-
[isRecording, sec]
390-
);
369+
useEffect(() => {
370+
let interval = null;
371+
if (isRecording) {
372+
interval = setInterval(() => {
373+
setSecond(sec + 1);
374+
if (sec === 59) {
375+
setMinute(min + 1);
376+
setSecond(0);
377+
}
378+
if (min === 99) {
379+
setMinute(0);
380+
setSecond(0);
381+
}
382+
}, 1000);
383+
} else if (!isRecording && sec !== 0) {
384+
setMinute(0);
385+
setSecond(0);
386+
clearInterval(interval);
387+
}
388+
return () => {
389+
clearInterval(interval);
390+
};
391+
}, [isRecording, sec]);
391392

392393
return (
393394
<>

0 commit comments

Comments
 (0)