react-use-audio-recorder
Version:
React component and hook for audio recording in your React applications
248 lines (242 loc) • 8.93 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => src_default,
useAudioRecorder: () => audio_recorder_default
});
module.exports = __toCommonJS(src_exports);
// src/components/audio-recorder.tsx
var import_react2 = require("react");
// src/hooks/audio-recorder.ts
var import_react = require("react");
var import_recordrtc = __toESM(require("recordrtc"), 1);
var useAudioRecorder = () => {
const [recordingTime, setRecordingTime] = (0, import_react.useState)(0);
const [recordingStatus, setRecordingStatus] = (0, import_react.useState)();
const intervalRef = (0, import_react.useRef)(null);
const recorderRef = (0, import_react.useRef)(null);
const startRecording = () => {
navigator.mediaDevices.getUserMedia({ audio: true }).then((microphone) => {
recorderRef.current = new import_recordrtc.default(microphone, {
type: "audio",
recorderType: import_recordrtc.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();
};
(0, import_react.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
var import_jsx_runtime = require("react/jsx-runtime");
function AudioRecorder({ onStop }) {
const {
recordingStatus,
recordingTime,
startRecording,
stopRecording,
pauseRecording,
resumeRecording,
getBlob,
saveRecording
} = audio_recorder_default();
(0, import_react2.useEffect)(() => {
if (recordingStatus === "stopped") {
onStop?.(getBlob());
}
}, [recordingStatus]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "audio-recorder", children: [
(!recordingStatus || recordingStatus === "stopped") && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { className: "record-button", onClick: startRecording, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "80",
height: "80",
viewBox: "0 0 48 48",
children: [
/* @__PURE__ */ (0, import_jsx_runtime.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__ */ (0, import_jsx_runtime.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__ */ (0, import_jsx_runtime.jsx)("button", { className: "record-button", onClick: pauseRecording, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "80",
height: "80",
viewBox: "0 0 24 24",
children: /* @__PURE__ */ (0, import_jsx_runtime.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__ */ (0, import_jsx_runtime.jsx)("button", { className: "record-button", onClick: resumeRecording, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "80",
height: "80",
viewBox: "0 0 24 24",
children: /* @__PURE__ */ (0, import_jsx_runtime.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__ */ (0, import_jsx_runtime.jsx)(
"div",
{
className: "recording-status",
style: recordingStatus === "recording" || recordingStatus === "paused" ? { opacity: 1 } : { opacity: 0 },
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { children: [
"Recording ",
`${recordingTime} s`
] })
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"button",
{
className: "pause-resume-button",
onClick: () => saveRecording(),
disabled: recordingStatus !== "stopped",
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
width: "24",
height: "24",
viewBox: "0 0 24 24",
children: /* @__PURE__ */ (0, import_jsx_runtime.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__ */ (0, import_jsx_runtime.jsx)(
"button",
{
className: "stop-button",
onClick: () => stopRecording(),
disabled: !recordingStatus || recordingStatus === "stopped",
children: "Stop"
}
)
] });
}
// src/index.ts
var src_default = AudioRecorder;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useAudioRecorder
});