如何强制录制webrtc音频的时间限制

时间:2015-01-14 14:39:19

标签: javascript google-chrome firefox audio webrtc

如何在firefox / chrome中使用webrtc音频强制录制音频文件的时间限制?

示例:用户访问某个网站,我希望他们只录制7秒钟的语音,然后让他们通过电子邮件发送给我。

1 个答案:

答案 0 :(得分:0)

选项'timeSlice'是一种限时录制的方式。 这将有助于遵循示例:

function captureMicrophone(callback) {
    navigator.mediaDevices.getUserMedia({ audio: true, video: false 
    }).then(function(microphone) {
	    callback(microphone);
    }).catch(function(error) {
	    log(error);
    });
}

var blobs = [];
var audio = document.querySelector('audio');
captureMicrophone(function(microphone) {
    setSrcObject(microphone, audio);
	audio.play();				
				
	recorder = RecordRTC(microphone, {						
		recorderType: StereoAudioRecorder,
		mimeType: 'audio/wav',
		desiredSampRate: 16000,
		numberOfAudioChannels: 1,
		timeSlice: 50, // pass this parameter
		// getNativeBlob: true,
		ondataavailable: function(blob) {
			blobs.push(blob);
			var size = 0;
			blobs.forEach(function(b) {
				size += b.size;
			});	
			/////						
		}
	});				
				
	recorder.startRecording();
	recorder.microphone = microphone;
				
	document.getElementById("start").disabled = true;
	document.getElementById("stop").disabled = false;
});