Skip to content

Instantly share code, notes, and snippets.

@li-jia-nan
Last active March 14, 2024 10:23
Show Gist options
  • Save li-jia-nan/899ce219b4ceb1808357ee98a7a87f97 to your computer and use it in GitHub Desktop.
Save li-jia-nan/899ce219b4ceb1808357ee98a7a87f97 to your computer and use it in GitHub Desktop.
MicrophoneRecorder
import RecordRTC from "recordrtc";
const options: RecordRTC.Options = {
type: "audio",
mimeType: "audio/wav",
recorderType: RecordRTC.StereoAudioRecorder,
};
class MicrophoneRecorder {
recorder: RecordRTC | undefined;
gumStream: MediaStream | undefined;
startRecording = () => {
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
this.gumStream = stream;
this.recorder = new RecordRTC(stream, options);
this.recorder?.startRecording();
});
};
stopRecording = () => {
return new Promise<Blob>((resolve, reject) => {
this.recorder?.stopRecording(() => {
this.gumStream?.getAudioTracks().forEach(track => track.stop());
const blob = this.recorder?.getBlob();
if (blob) {
resolve(blob);
} else {
reject(new Error("No blob"));
}
});
});
};
}
export default MicrophoneRecorder;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment