在Safari上调用getUserMedia会使麦克风在Mac OS X Safari 11和12版本中静音一秒钟

时间:2019-05-02 14:01:30

标签: javascript getusermedia navigator mediadevices

我正在使用getusermedia录制音频,当我尝试在safari 11和12上录制第一首歌曲时,会静默1-2秒,然后开始录制音频。其他浏览器,音频效果完美,没有延迟。

我试图提高记录器所使用的其他功能的性能,但是没有运气。

navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
        console.log("getUserMedia() success, stream created, initializing WebAudioRecorder...");
        audioContext = new AudioContext();
        //assign to gumStream for later use
        gumStream = stream;
        /* use the stream */
        input = audioContext.createMediaStreamSource(stream);
        var analyser = audioContext.createAnalyser();
        input.connect(analyser);
        analyser.fftSize = 2048;
        var bufferLength = analyser.frequencyBinCount;
        var dataArray = new Uint8Array(bufferLength);
        var runProject = $('.project-page').filter(function(e){ 
            return $(this).css('display') === 'block'; 
        });
        var c = runProject.find('.level-bar');
        AudioRecordingMain.playSliderElement.hide();
        AudioRecordingMain.waveform.show();
        function draw1() {
            var canvasCtx1 = c[0].getContext("2d");
            var grid = new Grid(200, 1);
            canvasCtx1.clearRect(0, 0, WIDTH, HEIGHT);    
            analyser.getByteFrequencyData(dataArray);
            canvasCtx1.fillStyle = 'rgba(255, 255, 255, 0)';
            canvasCtx1.fillRect(0, 0, WIDTH, HEIGHT);
            var barWidth = (WIDTH / bufferLength) * 2.5;
            var barHeight;
            var x = 0;
            for (var i = 0; i < grid.length; i++) {
                var s = dataArray[i]/3;
                canvasCtx1.fillStyle = hsl(map(i, 0, grid.length, 0, 360), 80, 50);
                canvasCtx1.fillRect(grid.x[i], HEIGHT - s/2, grid.spacing_x-1, s);
                x += barWidth + 1;
            }
            drawVisual = requestAnimationFrame(draw1);
        }
        draw1();
        if(AudioRecordingMain.recordingPendingInitiation){
            return;
        }
        audioRecorder = new WebAudioRecorder(input, {
            workerDir: "js/", // must end with slash
            encoding: 'mp3',
            numChannels:2, //2 is the default, mp3 encoding supports only 2
            onEncoderLoading: function(recorder, encoding) {
                // show "loading encoder..." display
                console.log("Loading "+encoding+" encoder...");
            },
            onEncoderLoaded: function(recorder, encoding) {
                // hide "loading encoder..." display
                console.log(encoding+" encoder loaded");
            }
        });
        audioRecorder.onComplete = function(recorder, blob) { 
            console.log("Encoding complete");
            AudioRecordingMain.recordingStopped(blob, recorder);
            AudioRecordingMain.uploadData[AudioRecordingMain.setCurrentRecNo -1] = blob; 
        }
        audioRecorder.onTimeout = function (recorder) {
            AudioRecordingMain.stopPlayingOrRecording();
        }
        audioRecorder.setOptions({
            timeLimit:1200,
            encodeAfterRecord:encodeAfterRecord,
            bufferSize: 4096,
            mp3: {bitRate: 160}
        });  
        console.log("Recording started");
        AudioRecordingMain.countdownFinished();
        AudioRecordingMain.recordingBtn.hide();
        AudioRecordingMain.recordingStopBtn.show();
    }).catch(function(err) {
        console.log(err);
    });

麦克风需要立即连接,并且与其他浏览器类似,在Safari 11和12中录制时应没有延迟。

0 个答案:

没有答案