The MediaStream Recording API, sometimes simply referred to as the Media Recording API or the MediaRecorder API, is closely affiliated with the Media Capture and Streams API and the WebRTC API. The MediaStream Recording API makes it possible to capture the data generated by a MediaStream
or HTMLMediaElement
object for analysis, processing, or saving to disk. It's also surprisingly easy to work with.
The MediaStream Recording API is comprised of a single major interface, MediaRecorder
, which does all the work of taking the data from a MediaStream
and delivering it to you for processing. The data is delivered by a series of dataavailable
events, already in the format you specify when creating the MediaRecorder
. You can then process the data further or write it to file as desired.
The process of recording a stream is simple:
MediaStream
or HTMLMediaElement
(in the form of an <audio>
or <video>
element) to serve as the source of the media data.MediaRecorder.ondataavailable
to an event handler for the dataavailable
event; this will be called whenever data is available for you.MediaRecorder
object, specifying the source stream and any desired options (such as the container's MIME type or the desired bit rates of its tracks.MediaRecorder.start()
to begin recording.dataavailable
event handler gets called every time there's data ready for you to do with as you will; the event has a data
attribute whose value is a Blob
that contains the media data. You can force a dataavailable
event to occur, thereby delivering the latest sound to you so you can filter it, save it, or whatever.MediaRecorder.stop()
.Note: Individual Blob
s containing slices of the recorded media will not necessarily be individually playable. The media needs to be reassembled before playback.
If anything goes wrong during recording, an error
event is sent to the MediaRecorder
. You can listen for error
events by setting up a onerror
event handler.
You can also use the properties of the MediaRecorder
object to determine the state of the recording process, and its pause()
and resume()
methods to pause and resume recording of the source media.
If you need or want to check to see if a specific MIME type is supported, that's possible as well. Just call MediaRecorder.isTypeSupported()
.
If your goal is to record camera and/or microphone input, you may wish to examine the available input devices before beginning the process of constructing the MediaRecorder
. To do so, you'll need to call navigator.mediaDevices.enumerateDevices()
to get a list of the available media devices. You can then examine that list and identify the potential input sources, and even filter the list based on desired criteria.
In this code snippet, enumerateDevices()
is used to examine the available input devices, locate those which are audio input devices, and create <option>
elements that are then added to a <select>
element representing an input source picker.
navigator.mediaDevices.enumerateDevices() .then(function(devices) { devices.forEach(function(device) { let menu = document.getElementById("inputdevices"); if (device.kind == "audioinput") { let item = document.createElement("option"); item.innerHTML = device.label; item.value = device.deviceId; menu.appendChild(item); } }); });
Code similar to this can be used to let the user restrict the set of devices they wish to use.
To learn more about using the MediaStream Recording API, see Using the MediaStream Recording API, which shows how to use the API to record audio clips. A second article, Recording a media element, describes how to receive a stream from an <audio>
or <video>
element and use the captured stream (in this case, recording it and saving it to a local disk).
BlobEvent
Blob
form using a BlobEvent
of type dataavailable
.MediaRecorder
MediaRecorderErrorEvent
error
property is a DOMException
that specifies that error occurred.Specification | Status | Comment |
---|---|---|
MediaStream Recording | Working Draft | Initial definition |
MediaRecorder
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 47 | ? | 25
|
? | 36 | ? |
MediaRecorder() constructor |
47 | ? | 25 | ? | 36 | ? |
mimeType |
47
|
? | 25 | ? | 36 | ? |
state |
47
|
? | 25 | ? | 36 | ? |
stream |
47
|
? | 25 | ? | 36 | ? |
ignoreMutedMedia
|
49 | ? | ? | ? | 36 | ? |
videoBitsPerSecond
|
49 | ? | ? | ? | 36 | ? |
audioBitsPerSecond
|
49 | ? | ? | ? | 36 | ? |
isTypeSupported |
47 | ? | 25 | ? | 36 | ? |
pause |
47
|
? | 25 | ? | 36 | ? |
requestData |
47
|
? | 25 | ? | 36 | ? |
resume |
47
|
? | 25 | ? | 36 | ? |
start |
47 | ? | 25 | ? | 36 | ? |
stop |
47
|
? | 25 | ? | 36 | ? |
ondataavailable |
47
|
? | 25 | ? | 36 | ? |
onerror |
47
|
? | 25 | ? | 36 | ? |
onpause |
47
|
? | 25 | ? | 36 | ? |
onresume |
47
|
? | 25 | ? | 36 | ? |
onstart |
47
|
? | 25 | ? | 36 | ? |
onstop |
47
|
? | 25 | ? | 36 | ? |
onwarning
|
47
|
? | 25 | ? | 36 | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 47 | 47 | ? | 25
|
36 | ? | 5.0 |
MediaRecorder() constructor |
47 | 47 | ? | 25 | 36 | ? | 5.0 |
mimeType |
49 | 49 | ? | 25 | 36 | ? | No |
state |
49 | 49 | ? | 25 | 36 | ? | No |
stream |
49 | 49 | ? | 25 | 36 | ? | No |
ignoreMutedMedia
|
49 | 49 | ? | ? | 36 | ? | 5.0 |
videoBitsPerSecond
|
49 | 49 | ? | ? | 36 | ? | 5.0 |
audioBitsPerSecond
|
49 | 49 | ? | ? | 36 | ? | 5.0 |
isTypeSupported |
47 | 47 | ? | 25 | 36 | ? | 5.0 |
pause |
49 | 49 | ? | 25 | 36 | ? | ? |
requestData |
49 | 49 | ? | 25 | 36 | ? | ? |
resume |
49 | 49 | ? | 25 | 36 | ? | ? |
start |
47 | 47 | ? | 25 | 36 | ? | 5.0 |
stop |
49 | 49 | ? | 25 | 36 | ? | ? |
ondataavailable |
49 | 49 | ? | 25 | 36 | ? | ? |
onerror |
49 | 49 | ? | 25 | 36 | ? | ? |
onpause |
49 | 49 | ? | 25 | 36 | ? | ? |
onresume |
49 | 49 | ? | 25 | 36 | ? | ? |
onstart |
49 | 49 | ? | 25 | 36 | ? | ? |
onstop |
49 | 49 | ? | 25 | 36 | ? | ? |
onwarning
|
49 | 49 | ? | 25 | 36 | ? | ? |
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/MediaStream_Recording_API