Search code examples
javascriptgoogle-chromefirefoxhtml5-audio

getUserMedia give a error on Firefox, and silently fails on Chrome


Full Js code

        navigator.getUserMedia = ( navigator.getUserMedia ||
                   navigator.webkitGetUserMedia ||
                   navigator.mozGetUserMedia ||
                   navigator.msGetUserMedia);

        var session = {
          audio: true,
          video: false
        };
        var recordRTC = null;
        navigator.getUserMedia(session, initializeRecorder, onError);

        function initializeRecorder(stream) {
            console.log ("init Recorder");
            var audioContext = window.AudioContext;
            var context = new audioContext();
            var audioInput = context.createMediaStreamSource(stream);
            var bufferSize = 2048;
            // create a javascript node
            var recorder = context.createJavaScriptNode(bufferSize, 1, 1);
            // specify the processing function
            recorder.onaudioprocess = recorderProcess;
            // connect stream to our recorder
            audioInput.connect(recorder);
            // connect our recorder to the previous destination
            recorder.connect(context.destination);
        }

        function recorderProcess(e) {
            var left = e.inputBuffer.getChannelData(0);
            // window.Stream.write(convertFloat32ToInt16(left));
        }

        function onError(errorText)
        {
            console.log (errorText);
        }

When loading this page, Firefox executes the onError callback for getUserMedia. The error tells:

NotFoundError: The object can not be found here.

Chrome, instead, simply does nothing.

Both FF and Chrome doesn't ask me for permission to use microphone. Why?


Solution

  • You need to run the page from a http(s) protocol. If you run it from file:// security restrictions kicks in.

    Also, in Chrome AudioContext is prefixed so you need to change this line:

    var audioContext = window.AudioContext;
    

    to

    var audioContext = window.AudioContext || window.webkitAudioContext;
    

    In addition, createJavaScriptNode is obsolete. Consider using createScriptProcessor instead.

    (Eventually the ScriptProcessorNode will also be replaced, by Audio Workers - not yet implemented in any browsers though, but something to be aware of for later).