higuma / web-audio-recorder-js

.wav/.ogg/.mp3 recorder with Web Audio API
MIT License
775 stars 185 forks source link

Fix "TypeError: recBuffers.push is undefined" #37

Open MotasemAghbar opened 4 years ago

MotasemAghbar commented 4 years ago

The function 'record' might throw a TypeError when executing recBuffers.push.

This happens when stopping the recorder, there is a chance for a race condition in the last 'record' event call and clean method, the clean method assigns the recBuffers to null, record event might be executing after the clean method, and then throw type error because recBuffers isn't defined.

The fix is to check if the recBuffers is undefined before accessing that var.

andreszs commented 4 years ago

Thanks for this PR, it works perfectly. Too bad the original developer has vanished and this won't be ever merged.

maria-nedelkova commented 4 years ago

I think that a better fix for this issue is to remove onaudioprocess event handler before processor has been deleted in cancelRecording function and finishRecording function (WevAudioRecorder.js):

    cancelRecording: function () {
      if (this.isRecording()) {
        this.input.disconnect();
        this.processor.disconnect();
        this.processor.onaudioprocess = null;
        delete this.processor;
        this.worker.postMessage({ command: 'cancel' });
      } else this.error('cancelRecording: no recording is running');
    },

    finishRecording: function () {
      if (this.isRecording()) {
        this.input.disconnect();
        this.processor.disconnect();
        this.processor.onaudioprocess = null;
        delete this.processor;
        this.worker.postMessage({ command: 'finish' });
      } else this.error('finishRecording: no recording is running');
    },
a8568730 commented 2 years ago

@maria-nedelkova thank you for the fix, I add the line this.processor.onaudioprocess = null; before those functions and it fixes the error. :)