Open rbracco opened 3 years ago
import { Recorder } from "vmsg";
const recorder = new Recorder({
wasmURL: "https://unpkg.com/vmsg@0.3.0/vmsg.wasm",
});
export const RecordViewNew = () => {
const [loading, setLoading] = useState(false);
const [isRecording, setIsRecording] = useState(false);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const [recordings, setRecordings] = useState<any[]>([]);
const record = async () => {
setLoading(true);
if (isRecording) {
const blob = await recorder.stopRecording();
setIsRecording(false);
setLoading(false);
setRecordings(recordings.concat(URL.createObjectURL(blob)));
} else {
try {
await recorder.initAudio();
await recorder.initWorker();
recorder.startRecording();
setLoading(false);
setIsRecording(true);
} catch (e) {
console.error(e);
setLoading(false);
}
}
};
return (
<>
<button disabled={loading} onClick={record}>
{isRecording ? "Stop" : "Record"}
</button>
<ul style={{ listStyle: "none", padding: 0 }}>
{recordings.map((url) => (
<li key={url}>
<audio src={url} controls />
</li>
))}
</ul>
</>
);
};
If someone needs functional component.
Hi, thank you for your work on vmsg. I am trying to implement a functional component version of the React demo from the README. Everything seems to work, and it's almost the same code as the example, but the blob comes back null. I am using vmsg 0.3.0, and I traced the code in
vmsg.stopRecording
and it seems though the worker is null even though I'm callinginitWorker()
. I'm probably messing up something basic with async/await or promises but I've tried to track it down with no luck. Thank you.