--- title: MediaRecorder slug: Web/API/MediaRecorder translation_of: Web/API/MediaRecorder ---
MediaRecorder
是 MediaStream Recording API 提供的用来进行媒体轻松录制的接口, 他需要通过调用 {{domxref("MediaRecorder.MediaRecorder", "MediaRecorder()")}} 构造方法进行实例化.
MediaRecorder
对象,对指定的{{domxref("MediaStream")}} 对象进行录制,支持的配置项包括设置容器的MIME 类型 (例如"video/webm"
或者 "video/mp4"
)和音频及视频的码率或者二者同用一个码率MediaRecorder
对象创建时选择器选择的录制容器的 MIME typeMediaRecorder
的当前状态(闲置中,录制中或者暂停 ) (inactive
, recording
, or paused
.)MediaRecorder
创建时构造函数传入的stream对象MediaRecorder
是否录制无声的输入源. 如果这个属性是false. 录制器对象MediaRecorder
会录制无声的音频或者黑屏的视频, 默认值是falserequestData()
方法之后到现在的内容). 调用这个方法后,录制将会继续进行,但是会创建一个新的Blob
对象timeslice
参数设置一个毫秒值,如果设置这个毫秒值,那么录制的媒体会按照你设置的值进行分割成一个个单独的区块, 而不是以默认的方式录制一个非常大的整块内容.Blob
内容的录制数据.之后不再记录data
属性中会提供一个可用的 {{domxref("Blob")}} 对象.)Listen to these events using addEventListener()
or by assigning an event listener to the oneventname
property of this interface.
error
onerror
property.if (navigator.mediaDevices) { console.log('getUserMedia supported.'); var constraints = { audio: true }; var chunks = []; navigator.mediaDevices.getUserMedia(constraints) .then(function(stream) { var mediaRecorder = new MediaRecorder(stream); visualize(stream); record.onclick = function() { mediaRecorder.start(); console.log(mediaRecorder.state); console.log("recorder started"); record.style.background = "red"; record.style.color = "black"; } stop.onclick = function() { mediaRecorder.stop(); console.log(mediaRecorder.state); console.log("recorder stopped"); record.style.background = ""; record.style.color = ""; } mediaRecorder.onstop = function(e) { console.log("data available after MediaRecorder.stop() called."); var clipName = prompt('Enter a name for your sound clip'); var clipContainer = document.createElement('article'); var clipLabel = document.createElement('p'); var audio = document.createElement('audio'); var deleteButton = document.createElement('button'); clipContainer.classList.add('clip'); audio.setAttribute('controls', ''); deleteButton.innerHTML = "Delete"; clipLabel.innerHTML = clipName; clipContainer.appendChild(audio); clipContainer.appendChild(clipLabel); clipContainer.appendChild(deleteButton); soundClips.appendChild(clipContainer); audio.controls = true; var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' }); chunks = []; var audioURL = URL.createObjectURL(blob); audio.src = audioURL; console.log("recorder stopped"); deleteButton.onclick = function(e) { evtTgt = e.target; evtTgt.parentNode.parentNode.removeChild(evtTgt.parentNode); } } mediaRecorder.ondataavailable = function(e) { chunks.push(e.data); } }) .catch(function(err) { console.log('The following error occured: ' + err); }) }
This code sample is inspired by the Web Dictaphone demo. Some lines have been omitted for brevity; refer to the source for the complete code.
Specification | Status | Comment |
---|---|---|
{{SpecName("MediaStream Recording", "#MediaRecorderAPI")}} | {{Spec2("MediaStream Recording")}} | Initial definition |
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatChrome(47.0)}} | {{CompatGeckoDesktop("25.0")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | {{CompatChrome(47.0)}} | {{CompatChrome(47.0)}} | {{CompatGeckoDesktop("25.0")}} | 1.3[1] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |