The MediaRecorder()
constructor creates a new MediaRecorder
object that will record a specified MediaStream
. The object can optionally be configured to record using a specific media container (file type), and, further, can specify the exact codec and codec configuration(s) to use by specifying the codecs
parameter.
Syntax
var mediaRecorder = new MediaRecorder(stream[, options]);
Parameters
stream
- The
MediaStream
that will be recorded. This source media can come from a stream created usingnavigator.mediaDevices.getUserMedia()
or from an<audio>
,<video>
or<canvas>
element. -
options
Optional -
A dictionary object that can contain the following properties:
mimeType
: A MIME type specifying the format for the resulting media; you may simply specify the container format (the browser will select its preferred codecs for audio and/or video), or you may use thecodecs
parameter and/or theprofiles
parameter to provide detailed information about which codecs to use and how to configure them. Applications can check in advance if amimeType
is supported by the user agent by callingMediaRecorder.isTypeSupported()
.audioBitsPerSecond
: The chosen bitrate for the audio component of the media.videoBitsPerSecond
: The chosen bitrate for the video component of the media.bitsPerSecond
: The chosen bitrate for the audio and video components of the media. This can be specified instead of the above two properties. If this is specified along with one or the other of the above properties, this will be used for the one that isn't specified.
If bits per second values are not specified for video and/or audio, the default adopted for video is 2.5Mbps, while the audio default is adaptive, depending upon the sample rate and the number of channels.
Exceptions
NotSupportedError
- The specified MIME type is not supported by the user agent.
Example
This example shows how to create a media recorder for a specified stream, whose audio bit rate is set to 128 Kbit/sec and whose video bit rate is set to 2.5 Mbit/sec. The recorded media data will be stored in an MP4 wrapper (so if you gather the chunks of media data and save them to disk, they will be in an MP4 file).
... if (navigator.mediaDevices.getUserMedia) { var constraints = { audio: true, video: true }; var chunks = []; var onSuccess = function(stream) { var options = { audioBitsPerSecond : 128000, videoBitsPerSecond : 2500000, mimeType : 'video/mp4' } var mediaRecorder = new MediaRecorder(stream,options); m = mediaRecorder; ... } }
Specifications
Specification | Status | Comment |
---|---|---|
MediaStream Recording | Working Draft | Initial definition |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
MediaRecorder() constructor | Chrome Full support 47 | Edge Full support 79 | Firefox Full support 25 | IE No support No | Opera Full support 36 | Safari No support No | WebView Android Full support 47 | Chrome Android Full support 47 | Firefox Android Full support 25 | Opera Android Full support 36 | Safari iOS No support No | Samsung Internet Android Full support 5.0 |
options object | Chrome Full support 49 | Edge Full support 79 | Firefox Full support 43 | IE No support No | Opera Full support 36 | Safari No support No | WebView Android Full support 49 | Chrome Android Full support 49 | Firefox Android ? | Opera Android Full support 36 | Safari iOS No support No | Samsung Internet Android Full support 5.0 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
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.mediaDevices.getUserMedia()