MediaRecorder

The MediaRecorder interface of the MediaStream Recording API provides functionality to easily record media. It is created using the MediaRecorder() constructor.

Constructor

MediaRecorder()
Creates a new 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.

Properties

MediaRecorder.mimeType Read only
Returns the MIME type that was selected as the recording container for the MediaRecorder object when it was created.
MediaRecorder.state Read only
Returns the current state of the MediaRecorder object (inactive, recording, or paused.)
MediaRecorder.stream Read only
Returns the stream that was passed into the constructor when the MediaRecorder was created.
MediaRecorder.ignoreMutedMedia
Indicates whether the MediaRecorder 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
Returns the video encoding bit rate in use. This may differ from the bit rate specified in the constructor (if it was provided).
MediaRecorder.audioBitsPerSecond Read 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.pause()
Pauses the recording of media.
MediaRecorder.requestData()
Requests a 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()
Resumes recording of media after having been paused.
MediaRecorder.start()
Begins recording media; this method can optionally be passed a 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()
Stops recording, at which point a dataavailable event containing the final Blob of saved data is fired. No more recording occurs.

Static methods

MediaRecorder.isTypeSupported()
A static method which returns a Boolean value indicating if the given MIME media type is supported by the current user agent.

Event handlers

MediaRecorder.ondataavailable
Called to handle the dataavailable 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.onerror
An EventHandler 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.onpause
An EventHandler called to handle the pause event, which occurs when media recording is paused.
MediaRecorder.onresume
An EventHandler called to handle the resume event, which occurs when media recording resumes after being paused.
MediaRecorder.onstart
An EventHandler called to handle the start event, which occurs when media recording starts.
MediaRecorder.onstop
An EventHandler 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.

Events

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

error
Fired when an error occurs: for example because recording wasn't allowed or was attempted using an unsupported codec.
Also available via the onerror property.

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 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.

Specifications

Specification Status Comment
MediaStream Recording Working Draft Initial definition

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
MediaRecorderChrome Full support 47Edge Full support 79Firefox Full support 25
Notes
Full support 25
Notes
Notes Prior to Firefox 58, using MediaStream.addTrack() on a stream obtained using getUserMedia(), then attempting to record the resulting stream would result in only recording the original stream without the added tracks (severe bug).
IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 25
Notes
Full support 25
Notes
Notes Prior to Firefox 58, using MediaStream.addTrack() on a stream obtained using getUserMedia(), then attempting to record the resulting stream would result in only recording the original stream without the added tracks (severe bug).
Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
MediaRecorder() constructorChrome Full support 47Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
audioBitsPerSecond
Experimental
Chrome Full support 49Edge Full support 79Firefox Full support 71IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android ? Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
error eventChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
ignoreMutedMedia
DeprecatedNon-standard
Chrome No support 49 — 57Edge No support NoFirefox ? IE No support NoOpera No support 36 — 44Safari No support NoWebView Android No support 49 — 57Chrome Android No support 49 — 57Firefox Android ? Opera Android No support 36 — 44Safari iOS No support NoSamsung Internet Android No support 5.0 — 7.0
isTypeSupportedChrome Full support 47Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
mimeTypeChrome Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Edge Full support 79Firefox Full support 25
Notes
Full support 25
Notes
Notes Starting with Firefox 71, the behavior of mimeType is more consistent. For example, it now returns the media type even after recording has stopped.
IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Chrome Android Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
ondataavailableChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
onerrorChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
onpauseChrome Full support 49Edge Full support 79Firefox Full support 65IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 65Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
onresumeChrome Full support 49Edge Full support 79Firefox Full support 65IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 65Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
onstartChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
onstopChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
onwarning
Deprecated
Chrome Full support 49Edge Full support 79Firefox No support 25 — 71IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
pauseChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
requestDataChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
resumeChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
startChrome Full support 47Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 47Chrome Android Full support 47Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
stateChrome Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Chrome Android Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
stopChrome Full support 49Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
streamChrome Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Edge Full support 79Firefox Full support 25IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49
Full support 49
No support 47 — 49
Notes
Notes Prior to Chrome 49, only video is supported, not audio.
Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
videoBitsPerSecond
Experimental
Chrome Full support 49Edge Full support 79Firefox Full support 71IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android ? Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0
warning event
Deprecated
Chrome Full support 49Edge Full support 79Firefox No support 25 — 71IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 25Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support 5.0

Legend

Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

See also