The MediaRecorder interface of the MediaStream Recording API provides functionality to easily record media. It is created by the invocation of the MediaRecorder() constructor.
Constructor
MediaRecorder.MediaRecorder()- Creates a new
MediaRecorderobject, given aMediaStreamto 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.
Properties
MediaRecorder.mimeTypeRead only- Returns the MIME type that was selected as the recording container for the
MediaRecorderobject when it was created. MediaRecorder.stateRead only- Returns the current state of the
MediaRecorderobject (inactive,recording, orpaused.) MediaRecorder.streamRead only- Returns the stream that was passed into the constructor when the
MediaRecorderwas created. MediaRecorder.ignoreMutedMedia- Indicates whether the
MediaRecorderinstance will record input when the inputMediaStreamTrackis muted. If this attribute isfalse,MediaRecorderwill record silence for audio and black frames for video. The default isfalse. MediaRecorder.videoBitsPerSecondRead only- Returns the video encoding bit rate in use. This may differ from the bit rate specified in the constructor (if it was provided).
MediaRecorder.audioBitsPerSecondRead only- Returns the audio encoding bit rate in use. This may differ from the bit rate specified in the constructor (if it was provided).
Methods
MediaRecorder.isTypeSupported()- Returns a
Booleanvalue indicating if the given MIME type is supported by the current user agent . MediaRecorder.pause()- Pauses the recording of media.
MediaRecorder.requestData()- Requests a
Blobcontaining the saved data received thus far (or since the last timerequestData()was called. After calling this method, recording continues, but in a newBlob. MediaRecorder.resume()- Resumes recording of media after having been paused.
MediaRecorder.start()- Begins recording media; this method can optionally be passed a
timesliceargument 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()- Stops recording, at which point a
dataavailableevent containing the finalBlobof saved data is fired. No more recording occurs.
Event Handlers
MediaRecorder.ondataavailable- Called to handle the
dataavailableevent, which can be used to grab the recorded media (which is made available as aBlobobject in the event'sdataattribute.) MediaRecorder.onerror- An
EventHandlercalled to handle therecordingerrorevent, including reporting errors that arise with media recording. These are fatal errors that stop recording. MediaRecorder.onpause- An
EventHandlercalled to handle thepauseevent, which occurs when media recording is paused. MediaRecorder.onresume- An
EventHandlercalled to handle theresumeevent, which occurs when media recording resumes after being paused. MediaRecorder.onstart- An
EventHandlercalled to handle thestartevent, which occurs when media recording starts. MediaRecorder.onstop- An
EventHandlercalled to handle thestopevent, which occurs when media recording ends, either when theMediaStreamends — or after theMediaRecorder.stop()method is called.
Example
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.
Specifications
| Specification | Status | Comment |
|---|---|---|
| MediaStream Recording | Working Draft | Initial definition |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 47.0 | 25.0 (25.0) | No support | No support | No support |
| Feature | Android Webview | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Basic support | 47.0 | 47.0 | 25.0 (25.0) | 1.3[1] | No support | No support | No support |
- [1] The initial Firefox OS implementation only supported audio recording.
See also
- Using the MediaRecorder API
- Web Dictaphone: MediaRecorder + getUserMedia + Web Audio API visualization demo, by Chris Mills (source on Github.)
- Recording a media element
- simpl.info MediaStream Recording demo, by Sam Dutton.
navigator.mediaDevices.getUserMedia- FingerSpell: Sign Language Fingerspelling practice using MediaDevices and the MediaStream Recording API to create and download recordings, MediaRecorder API supported desktop browsers only (source on GitHub)
Document Tags and Contributors
Tags:
Contributors to this page:
jpmedley,
chrisdavidmills,
lb091188,
miguelao,
nylki,
SOCSIELEARNING,
Jib,
Sheppy,
teoli,
omidfi,
Sebastianz,
padenotmoz,
eldog
Last updated by:
jpmedley,