我正在使用 getusermedia 录制音频,当我尝试在 safari 11 和 12 上录制第一首歌曲时,会有 1-2 秒的静音,然后音频开始录制。O 其他浏览器音频工作完美,没有延迟。
我试图提高与录音机一起使用的其他东西的性能,但没有运气。
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 上录制应该没有延迟。