The MediaRecorder interface of the MediaStream Recording API provides functionality to easily record media. It is created using the MediaRecorder() constructor.
MediaRecorder()MediaRecorder object, given a MediaStream to record. Options are available to do things like set the container's MIME type (such as "video/webm" or "video/mp4") and the bit rates of the audio and video tracks or a single overall bit rate.MediaRecorder.mimeType Read only
MediaRecorder object when it was created.MediaRecorder.state Read only
MediaRecorder object (inactive, recording, or paused.)MediaRecorder.stream Read only
MediaRecorder was created.MediaRecorder.ignoreMutedMediaMediaRecorder instance will record input when the input MediaStreamTrack is muted. If this attribute is false, MediaRecorder will record silence for audio and black frames for video. The default is false.MediaRecorder.videoBitsPerSecond Read only
MediaRecorder.audioBitsPerSecond Read only
MediaRecorder.isTypeSupported()Boolean value indicating if the given MIME type is supported by the current user agent . MediaRecorder.pause()MediaRecorder.requestData()Blob containing the saved data received thus far (or since the last time requestData() was called. After calling this method, recording continues, but in a new Blob.MediaRecorder.resume()MediaRecorder.start()timeslice argument with a value in milliseconds. If this is specified, the media will be captured in separate chunks of that duration, rather than the default behavior of recording the media in a single large chunk.MediaRecorder.stop()dataavailable event containing the final Blob of saved data is fired. No more recording occurs.MediaRecorder.ondataavailabledataavailable event, which is periodically triggered each time timeslice milliseconds of media have been recorded (or when the entire media has been recorded, if timeslice wasn't specified). The event, of type BlobEvent, contains the recorded media in its data property. You can then collect and act upon that recorded media data using this event handler.MediaRecorder.onerrorEventHandler called to handle the error event, including reporting errors that arise with media recording. These are fatal errors that stop recording. The received event is based on the MediaRecorderErrorEvent interface, whose error property contains a DOMException that describes the actual error that occurred.MediaRecorder.onpauseEventHandler called to handle the pause event, which occurs when media recording is paused.MediaRecorder.onresumeEventHandler called to handle the resume event, which occurs when media recording resumes after being paused.MediaRecorder.onstartEventHandler called to handle the start event, which occurs when media recording starts.MediaRecorder.onstopEventHandler called to handle the stop event, which occurs when media recording ends, either when the MediaStream ends — or after the MediaRecorder.stop() method is called.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 occurred: ' + 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 |
|---|---|---|
| MediaStream Recording | Working Draft | Initial definition |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 47 | ? | 25
|
? | ? | ? |
MediaRecorder() constructor |
47 | ? | 25 | ? | ? | ? |
mimeType |
47
|
? | 25 | ? | ? | ? |
state |
47
|
? | 25 | ? | ? | ? |
stream |
47
|
? | 25 | ? | ? | ? |
ignoreMutedMedia
|
49 | ? | ? | ? | ? | ? |
videoBitsPerSecond
|
49 | ? | ? | ? | ? | ? |
audioBitsPerSecond
|
49 | ? | ? | ? | ? | ? |
isTypeSupported |
47 | ? | 25 | ? | ? | ? |
pause |
? | ? | 25 | ? | ? | ? |
requestData |
47
|
? | 25 | ? | ? | ? |
resume |
47
|
? | 25 | ? | ? | ? |
start |
47 | ? | 25 | ? | ? | ? |
stop |
47
|
? | 25 | ? | ? | ? |
ondataavailable |
47
|
? | 25 | ? | ? | ? |
onerror |
47
|
? | 25 | ? | ? | ? |
onpause |
47
|
? | 25 | ? | ? | ? |
onresume |
47
|
? | 25 | ? | ? | ? |
onstart |
47
|
? | 25 | ? | ? | ? |
onstop |
47
|
? | 25 | ? | ? | ? |
onwarning
|
47
|
? | 25 | ? | ? | ? |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | 47 | 47 | ? | 25
|
? | ? | 5.0 |
MediaRecorder() constructor |
47 | 47 | ? | 25 | ? | ? | 5.0 |
mimeType |
No | No | ? | 25 | ? | ? | No |
state |
No | No | ? | 25 | ? | ? | No |
stream |
No | No | ? | 25 | ? | ? | No |
ignoreMutedMedia
|
49 | 49 | ? | ? | ? | ? | 5.0 |
videoBitsPerSecond
|
49 | 49 | ? | ? | ? | ? | 5.0 |
audioBitsPerSecond
|
49 | 49 | ? | ? | ? | ? | 5.0 |
isTypeSupported |
47 | 47 | ? | 25 | ? | ? | 5.0 |
pause |
? | ? | ? | 25 | ? | ? | ? |
requestData |
? | ? | ? | 25 | ? | ? | ? |
resume |
? | ? | ? | 25 | ? | ? | ? |
start |
47 | 47 | ? | 25 | ? | ? | 5.0 |
stop |
? | ? | ? | 25 | ? | ? | ? |
ondataavailable |
? | ? | ? | 25 | ? | ? | ? |
onerror |
? | ? | ? | 25 | ? | ? | ? |
onpause |
? | ? | ? | 25 | ? | ? | ? |
onresume |
? | ? | ? | 25 | ? | ? | ? |
onstart |
? | ? | ? | 25 | ? | ? | ? |
onstop |
? | ? | ? | 25 | ? | ? | ? |
onwarning
|
? | ? | ? | 25 | ? | ? | ? |
navigator.mediaDevices.getUserMedia
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder