UNPKG

react-mic-record

Version:
116 lines (93 loc) 4.12 kB
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var constraints = { audio: true, video: false }; // constraints - only audio needed var MicrophoneRecorder = function () { function MicrophoneRecorder(onStart, onStop, onData, options, audioContext) { _classCallCheck(this, MicrophoneRecorder); navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; this.audioCtx = audioContext.getAudioContext(); this.analyser = audioContext.getAnalyser(); this.stream = null; this.onStartCb = onStart; this.onStopCb = onStop; this.mediaOptions = options; this.onData = onData; this.chunks = []; this.startTime = null; } MicrophoneRecorder.prototype.startRecording = function startRecording() { var _this = this; this.startTime = Date.now(); if (this.mediaRecorder) { if (this.audioCtx && this.audioCtx.state === 'suspended') { this.audioCtx.resume(); } if (this.mediaRecorder && this.mediaRecorder.state === 'paused') { this.mediaRecorder.resume(); return; } if (this.audioCtx && this.mediaRecorder && this.mediaRecorder.state === 'inactive') { this.mediaRecorder.start(10); var source = this.audioCtx.createMediaStreamSource(this.stream); source.connect(this.analyser); if (this.onStartCb) { this.onStartCb(); } } } else { if (navigator.mediaDevices) { console.log('getUserMedia supported.'); navigator.mediaDevices.getUserMedia(constraints).then(function (str) { _this.stream = str; if (MediaRecorder.isTypeSupported(_this.mediaOptions.mimeType)) { _this.mediaRecorder = new MediaRecorder(_this.stream, _this.mediaOptions); } else { _this.mediaRecorder = new MediaRecorder(_this.stream); } if (_this.onStartCb) { _this.onStartCb(); } _this.mediaRecorder.onstop = function () { return _this.onStop(); }; _this.mediaRecorder.ondataavailable = function (e) { _this.chunks.push(e.data); if (_this.onData) { _this.onData(e.data); } }; _this.mediaRecorder.start(10); var source = _this.audioCtx.createMediaStreamSource(_this.stream); source.connect(_this.analyser); }); } else { alert('Your browser does not support audio recording'); } } }; MicrophoneRecorder.prototype.stopRecording = function stopRecording() { if (this.mediaRecorder && this.mediaRecorder.state !== 'inactive') { this.mediaRecorder.stop(); this.audioCtx.suspend(); } }; MicrophoneRecorder.prototype.unMount = function unMount() { this.stream && this.stream.getTracks()[0].stop(); this.mediaRecorder = null; }; MicrophoneRecorder.prototype.onStop = function onStop() { var blob = new Blob(this.chunks, { 'type': this.mediaOptions.mimeType }); this.chunks = []; var blobObject = { blob: blob, startTime: this.startTime, stopTime: Date.now(), options: this.mediaOptions, blobURL: window.URL.createObjectURL(blob) }; if (this.onStopCb) { this.onStopCb(blobObject); } }; return MicrophoneRecorder; }(); export { MicrophoneRecorder as default };