Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement pause functionality? #17

Open
318097 opened this issue Mar 24, 2020 · 5 comments
Open

How to implement pause functionality? #17

318097 opened this issue Mar 24, 2020 · 5 comments

Comments

@318097
Copy link

318097 commented Mar 24, 2020

No description provided.

@buzinas
Copy link
Contributor

buzinas commented Jun 19, 2020

Currently there is no way to pause a recording. That said, we would be glad to accept PRs for that, it shouldn't be very difficult.

@picasocro1
Copy link

picasocro1 commented Aug 8, 2020

I'm also interested in that too!

picasocro1 pushed a commit to picasocro1/mic-recorder-to-mp3 that referenced this issue Aug 8, 2020
Added pause recording functionality (closeio#17)
Updated sample to show this feature too
@picasocro1
Copy link

I've prepared PR for that feature :)

@anirket
Copy link

anirket commented Nov 1, 2021

Hi, when will this PR be merged?

@martelman
Copy link

martelman commented Oct 6, 2024

@anirket
just implement this.

    function MicRecorder(config) {
      classCallCheck(this, MicRecorder);
  
      this.config = {
        bitRate: 128,
        startRecordingAt: 300,
        deviceId: null,
        ...config
      };
  
      this.activeStream = null;
      this.context = null;
      this.microphone = null;
      this.processor = null;
      this.startTime = 0;
      this.isPaused = false; // Flag to track pause state
      this.isRecording = false; // Flag to track recording state
  
      this.lameEncoder = new Encoder(this.config);
    }
  
    createClass(MicRecorder, [{
      key: 'start',
      value: function start() {
        const AudioContext = window.AudioContext || window.webkitAudioContext;
        this.context = new AudioContext();
        this.config.sampleRate = this.context.sampleRate;
  
        const audioConstraints = this.config.deviceId
          ? { deviceId: { exact: this.config.deviceId } }
          : { audio: true };
  
        return navigator.mediaDevices.getUserMedia(audioConstraints)
          .then(stream => {
            this.activeStream = stream;
            this.isPaused = false;
            this.isRecording = true;
            this.addMicrophoneListener(stream);
            return stream;
          })
          .catch(err => {
            console.error('Error accessing microphone:', err);
            throw err;
          });
      }
    }, {
      key: 'addMicrophoneListener',
      value: function addMicrophoneListener(stream) {
        const _this = this;
  
        this.timerToStart = setTimeout(() => {
          delete _this.timerToStart;
        }, this.config.startRecordingAt);
  
        this.microphone = this.context.createMediaStreamSource(stream);
        this.processor = this.context.createScriptProcessor(0, 1, 1);
  
        this.processor.onaudioprocess = function (event) {
          if (_this.isPaused || !_this.isRecording) {
            return;
          }
  
          if (_this.timerToStart) {
            return;
          }
  
          const inputBuffer = event.inputBuffer.getChannelData(0);
          _this.lameEncoder.encode(inputBuffer);
        };
  
        this.microphone.connect(this.processor);
        this.processor.connect(this.context.destination);
      }
    }, {
      key: 'pause',
      value: function pause() {
        if (!this.isRecording) return;
  
        this.isPaused = true;
        console.log('Recording paused.');
      }
    }, {
      key: 'resume',
      value: function resume() {
        if (!this.isRecording) return;
  
        this.isPaused = false;
        console.log('Recording resumed.');
      }
    }, {
      key: 'stop',
      value: function stop() {
        if (this.processor && this.microphone) {
          this.microphone.disconnect();
          this.processor.disconnect();
  
          if (this.context && this.context.state !== 'closed') {
            this.context.close();
          }
  
          this.processor.onaudioprocess = null;
        }
  
        if (this.activeStream) {
          this.activeStream.getAudioTracks().forEach(track => track.stop());
          this.activeStream = null;
        }
  
        this.isRecording = false;
        this.isPaused = false;
  
        return this;
      }
    }, {
      key: 'getMp3',
      value: function getMp3() {
        return new Promise((resolve, reject) => {
          if (!this.lameEncoder) {
            reject(new Error('Encoder not initialized.'));
            return;
          }
  
          const finalBuffer = this.lameEncoder.finish();
  
          if (finalBuffer.length === 0) {
            reject(new Error('No audio data captured.'));
          } else {
            const mp3Blob = new Blob(finalBuffer, { type: 'audio/mp3' });
            resolve([finalBuffer, mp3Blob]);
            this.lameEncoder.clearBuffer();
          }
        });
      }
    }]);
  
    return MicRecorder;
  }();```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants