This article is in need of a technical review.
The MediaRecorder interface of the MediaRecorder API provides functionality to easily capture media. It is created by the invocation of the MediaRecorder() constructor.
Properties
-
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.
Methods
-
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 -
Called to handle the
DOMErrorevent, including reporting errors that arise with media recording. These are fatal errors that stop recording. -
MediaRecorder.onpause -
Called to handle the
pauseevent, which occurs when media recording is paused. -
MediaRecorder.onresume -
Called to handle the
resumeevent, which occurs when media recording resumes after being paused. -
MediaRecorder.onstart -
Called to handle the
startevent, which occurs when media recording starts. -
MediaRecorder.onstop -
Called to handle the
stopevent, which occurs when media recording ends, either when theMediaStreamends — or after theMediaRecorder.stop()method is called. -
MediaRecorder.onwarning -
Called to handle the
warningevent, which occurs when non-fatal errors occur that do not stop the recording.
Example
if (navigator.getUserMedia) {
console.log('getUserMedia supported.');
navigator.getUserMedia (
// constraints - only audio needed for this app
{
audio: true
},
// Success callback
function(stream) {
var mediaRecorder = new MediaRecorder(stream);
record.onclick = function() {
mediaRecorder.start();
console.log("recorder started");
}
stop.onclick = function() {
mediaRecorder.stop();
console.log("recorder stopped");
}
mediaRecorder.ondataavailable = function(e) {
console.log("data available after MediaRecorder.stop() called.");
var audio = document.createElement('audio');
audio.setAttribute('controls', '');
var audioURL = window.URL.createObjectURL(e.data);
audio.src = audioURL;
}
},
// Error callback
function(err) {
console.log('The following gUM error occured: ' + err);
}
);
} else {
console.log('getUserMedia not supported on your browser!');
}
Note: This code sample comes from the Web Dictaphone demo. Some lines have been omitted for brevity; refer to the source for the complete code.
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support |
Not supported |
25.0 (25.0) | Not supported | Not supported | Not supported |
| Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | Not supported | 25.0 (25.0) | 1.3 | Not supported | Not supported | Not supported |
Note: The current Gecko implementation currently only supports audio recording.
Specifications
| Specification | Status | Comment |
|---|---|---|
| MediaStream Recording | Editor's Draft |
See also
- Using the MediaRecorder API
- Web Dictaphone: MediaRecorder + getUserMedia + Web Audio API visualization demo, by Chris Mills (source on Github.)
- simpl.info MediaStream Recording demo, by Sam Dutton.
Navigator.getUserMedia
Document Tags and Contributors
Contributors to this page: Sheppy, chrisdavidmills
Last updated by:
chrisdavidmills,