getUserMedia 在 Firefox 上给出错误,在 Chrome 上默默地失败

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

完整的Js代码

        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);
        }

加载此页面时,Firefox 执行 getUserMedia 的 onError 回调。错误告诉:

NotFoundError: The object can not be found here.

Chrome,相反,什么也不做。

FF 和 Chrome 都没有询问我使用麦克风的权限。为什么?

您需要 运行 来自 http(s) 协议的页面。如果你 运行 它来自 file:// 安全限制开始。

此外,在 Chrome AudioContext 中有前缀,因此您需要更改此行:

var audioContext = window.AudioContext;

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

另外,createJavaScriptNode is obsolete. Consider using createScriptProcessor代替。

(最终 ScriptProcessorNode 也将被替换为 Audio Workers - 虽然尚未在任何浏览器中实现,但需要注意的事项稍后)。