Recorder.js recording is not working as expected

2.1k Views Asked by At

I implemented a recorder in JavaScript using recorder.js

Basically, I am creating an app for Mozilla OS. When I install the app, the recorder works fine for the first time. When I try for the second time, it's not working. When I opened the wav file in Audacity, It's showing completely blank.

I am sharing my recorder. I kept the createDownloadLink() empty because it is something which I can't share.

Any clues on why the recorder is working fine for the first time well and not second time onwards? Any hacks to overcome this problem?

function startUserMedia(stream) {

            var input = audio_context.createMediaStreamSource(stream);
            __log('Media stream created.');
            // Uncomment if you want the audio to feedback directly
            //input.connect(audio_context.destination);
            //__log('Input connected to audio context destination.');
            recorder = new Recorder(input);
            __log('Recorder initialised.');
        }

function startRecording(button) {

            recorder && recorder.clear();
            recorder && recorder.record();
            button.disabled = true;
            button.nextElementSibling.disabled = false;
            __log('Recording...');
        }

function stopRecording(button) {

            recorder && recorder.stop();
            button.disabled = true;
            button.previousElementSibling.disabled = false;
            __log('Stopped recording.');
            // create WAV download link using audio data blob
            createDownloadLink();
            recorder.clear();
            recorder && recorder.clear();

        }

function createDownloadLink() {

            recorder && recorder.exportWAV(function(blob) {
    }
}

 window.onload = function init() {

            try {

                request = new XMLHttpRequest();
                request.mozSystem = true;

                // webkit shim
                window.AudioContext = window.AudioContext || window.webkitAudioContext;
                navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia|| navigator.mozGetUserMedia;
                window.URL = window.URL || window.webkitURL;
                audio_context = new AudioContext;
                __log('Audio context set up.');
                __log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
            } catch (e) {
                alert('No web audio support in this browser!');
            }
            navigator.getUserMedia({
                audio: true
            }, startUserMedia, function(e) {
                __log('No live audio input: ' + e);
            });
        };
1

There are 1 best solutions below

0
On

You may be hitting this bug: https://bugzilla.mozilla.org/show_bug.cgi?id=934512 The workaround is to place a reference to the result of createMediaStreamSource somewhere that will never be garbage collected, such as:

window.savedReferenceWorkaroundFor934512 = input;

If that isn't the cause, then I would look into creating a new audioContext for each start/stop cycle (and initiating a new getUserMedia call at each new start).

It's hard to tell without looking at all of your code, but you might also have a race condition between saving the blob and clearing the recorder. One fix would be to call recorder.clear() only after the blob is made.