Skip to content

Commit

Permalink
Merge pull request #108 from TEAM-Hearus/develop
Browse files Browse the repository at this point in the history
Refactor: useRecord ์„ฑ๋Šฅ ๊ฐœ์„ 
  • Loading branch information
Nangniya authored Nov 11, 2024
2 parents cec4923 + 1091011 commit a6622cc
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions src/hooks/useRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,38 @@ import { useAlertStore } from '../store/useAlertStore';
export const useRecorder = (onAudioData: (data: string) => void) => {
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
const streamRef = useRef<MediaStream | null>(null);
const intervalRef = useRef<number | null>(null);

const addAlert = useAlertStore((state) => state.addAlert);

const initMediaRecorder = (stream: MediaStream) => {
const options = { mimeType: 'audio/webm;codecs=opus' };
mediaRecorderRef.current = new MediaRecorder(stream, options);

mediaRecorderRef.current.ondataavailable = (event) => {
if (event.data.size > 0 && event.data != null) {
const reader = new FileReader();
reader.onload = (e) => {
const arrayBuffer = e.target?.result as ArrayBuffer;
if (arrayBuffer) {
const base64EncodedData = btoa(
String.fromCharCode(...new Uint8Array(arrayBuffer)),
);
onAudioData(base64EncodedData);
} else {
console.error(
'[mediaRecorder]-[ondataavailable] ArrayBuffer is null',
);
}
};
reader.readAsArrayBuffer(event.data);
}
};

mediaRecorderRef.current.start();
const handleDataAvailable = (event: BlobEvent) => {
if (event.data.size > 0) {
const reader = new FileReader();
reader.onload = (e) => {
const arrayBuffer = e.target?.result as ArrayBuffer;
if (arrayBuffer) {
const base64EncodedData = btoa(
String.fromCharCode(...new Uint8Array(arrayBuffer)),
);
onAudioData(base64EncodedData);
} else {
console.error(
'[mediaRecorder]-[ondataavailable] ArrayBuffer is null',
);
}
};
reader.readAsArrayBuffer(event.data);
}
};

const startRecording = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
streamRef.current = stream;
initMediaRecorder(stream);

intervalRef.current = setInterval(() => {
if (mediaRecorderRef.current?.state === 'recording') {
mediaRecorderRef.current.stop();
}
initMediaRecorder(stream);
}, 3000);
const options = { mimeType: 'audio/webm;codecs=opus' };
mediaRecorderRef.current = new MediaRecorder(stream, options);
mediaRecorderRef.current.ondataavailable = handleDataAvailable;
mediaRecorderRef.current.start(3000);
} catch (error) {
addAlert(
'๋งˆ์ดํฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ € ์„ค์ •์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.',
Expand All @@ -60,12 +49,8 @@ export const useRecorder = (onAudioData: (data: string) => void) => {
mediaRecorderRef.current.stop();
}
streamRef.current?.getTracks().forEach((track) => track.stop());
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
mediaRecorderRef.current = null;
streamRef.current = null;
intervalRef.current = null;
};

return { startRecording, stopRecording };
Expand Down

0 comments on commit a6622cc

Please sign in to comment.