react-use-audio-recorder
Version:
React component and hook for audio recording in your React applications
211 lines (207 loc) • 6.79 kB
JavaScript
// src/components/audio-recorder.tsx
import { useEffect as useEffect2 } from "react";
// src/hooks/audio-recorder.ts
import { useEffect, useRef, useState } from "react";
import RecordRTC, { StereoAudioRecorder } from "recordrtc";
var useAudioRecorder = () => {
const [recordingTime, setRecordingTime] = useState(0);
const [recordingStatus, setRecordingStatus] = useState();
const intervalRef = useRef(null);
const recorderRef = useRef(null);
const startRecording = () => {
navigator.mediaDevices.getUserMedia({ audio: true }).then((microphone) => {
recorderRef.current = new RecordRTC(microphone, {
type: "audio",
recorderType: StereoAudioRecorder,
desiredSampRate: 16e3,
disableLogs: true
});
recorderRef.current.startRecording();
recorderRef.current.microphone = microphone;
setRecordingStatus("recording");
}).catch((error) => {
alert("Unable to access your microphone.");
console.error(error);
});
};
const stopRecording = (callBack) => {
recorderRef.current?.stopRecording((blobURL) => {
recorderRef.current.microphone.stop();
setRecordingStatus("stopped");
setRecordingTime(0);
callBack?.(recorderRef.current?.getBlob(), blobURL);
});
};
const pauseRecording = () => {
recorderRef.current?.pauseRecording();
setRecordingStatus("paused");
};
const resumeRecording = () => {
recorderRef.current?.resumeRecording();
setRecordingStatus("recording");
};
const saveRecording = (fileName) => {
recorderRef.current?.save(fileName);
};
const getBlob = () => {
return recorderRef.current?.getBlob();
};
useEffect(() => {
if (recordingStatus == "recording") {
intervalRef.current = setInterval(() => {
setRecordingTime((prev) => prev + 1);
}, 1e3);
}
if (!recordingStatus || recordingStatus == "stopped") {
clearInterval(intervalRef.current);
}
return () => clearInterval(intervalRef.current);
}, [recordingStatus]);
return {
recordingStatus,
recordingTime,
startRecording,
stopRecording,
pauseRecording,
resumeRecording,
saveRecording,
getBlob
};
};
var audio_recorder_default = useAudioRecorder;
// src/components/audio-recorder.tsx
import { jsx, jsxs } from "react/jsx-runtime";
function AudioRecorder({ onStop }) {
const {
recordingStatus,
recordingTime,
startRecording,
stopRecording,
pauseRecording,
resumeRecording,
getBlob,
saveRecording
} = audio_recorder_default();
useEffect2(() => {
if (recordingStatus === "stopped") {
onStop?.(getBlob());
}
}, [recordingStatus]);
return /* @__PURE__ */ jsxs("div", { className: "audio-recorder", children: [
(!recordingStatus || recordingStatus === "stopped") && /* @__PURE__ */ jsx("button", { className: "record-button", onClick: startRecording, children: /* @__PURE__ */ jsxs(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "80",
height: "80",
viewBox: "0 0 48 48",
children: [
/* @__PURE__ */ jsx(
"path",
{
fill: "none",
stroke: "white",
strokeLinecap: "round",
strokeLinejoin: "round",
strokeWidth: "2",
d: "M31.03 25.59a7.03 7.03 0 0 1-14.06 0V11.53a7.03 7.03 0 0 1 14.06 0z"
}
),
/* @__PURE__ */ jsx(
"path",
{
fill: "none",
stroke: "white",
strokeLinecap: "round",
strokeLinejoin: "round",
strokeWidth: "2",
d: "M35.919 25.59c0 6.582-5.336 11.919-11.919 11.919s-11.919-5.337-11.919-11.92M24 37.509V43.5"
}
)
]
}
) }),
recordingStatus === "recording" && /* @__PURE__ */ jsx("button", { className: "record-button", onClick: pauseRecording, children: /* @__PURE__ */ jsx(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "80",
height: "80",
viewBox: "0 0 24 24",
children: /* @__PURE__ */ jsx(
"path",
{
fill: "white",
d: "M16 19q-.825 0-1.412-.587T14 17V7q0-.825.588-1.412T16 5t1.413.588T18 7v10q0 .825-.587 1.413T16 19m-8 0q-.825 0-1.412-.587T6 17V7q0-.825.588-1.412T8 5t1.413.588T10 7v10q0 .825-.587 1.413T8 19"
}
)
}
) }),
recordingStatus === "paused" && /* @__PURE__ */ jsx("button", { className: "record-button", onClick: resumeRecording, children: /* @__PURE__ */ jsx(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "80",
height: "80",
viewBox: "0 0 24 24",
children: /* @__PURE__ */ jsx(
"path",
{
fill: "white",
d: "M8 17.175V6.825q0-.425.3-.713t.7-.287q.125 0 .263.037t.262.113l8.15 5.175q.225.15.338.375t.112.475t-.112.475t-.338.375l-8.15 5.175q-.125.075-.262.113T9 18.175q-.4 0-.7-.288t-.3-.712"
}
)
}
) }),
/* @__PURE__ */ jsx(
"div",
{
className: "recording-status",
style: recordingStatus === "recording" || recordingStatus === "paused" ? { opacity: 1 } : { opacity: 0 },
children: /* @__PURE__ */ jsxs("span", { children: [
"Recording ",
`${recordingTime} s`
] })
}
),
/* @__PURE__ */ jsx(
"button",
{
className: "pause-resume-button",
onClick: () => saveRecording(),
disabled: recordingStatus !== "stopped",
children: /* @__PURE__ */ jsx(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "24",
height: "24",
viewBox: "0 0 24 24",
children: /* @__PURE__ */ jsx(
"path",
{
fill: "white",
d: "M12 15.575q-.2 0-.375-.062T11.3 15.3l-3.6-3.6q-.3-.3-.288-.7t.288-.7q.3-.3.713-.312t.712.287L11 12.15V5q0-.425.288-.712T12 4t.713.288T13 5v7.15l1.875-1.875q.3-.3.713-.288t.712.313q.275.3.288.7t-.288.7l-3.6 3.6q-.15.15-.325.213t-.375.062M6 20q-.825 0-1.412-.587T4 18v-2q0-.425.288-.712T5 15t.713.288T6 16v2h12v-2q0-.425.288-.712T19 15t.713.288T20 16v2q0 .825-.587 1.413T18 20z"
}
)
}
)
}
),
/* @__PURE__ */ jsx(
"button",
{
className: "stop-button",
onClick: () => stopRecording(),
disabled: !recordingStatus || recordingStatus === "stopped",
children: "Stop"
}
)
] });
}
// src/index.ts
var src_default = AudioRecorder;
export {
src_default as default,
audio_recorder_default as useAudioRecorder
};