Last active
March 14, 2024 10:23
-
-
Save li-jia-nan/899ce219b4ceb1808357ee98a7a87f97 to your computer and use it in GitHub Desktop.
MicrophoneRecorder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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