MediaStream Recording
The start() method of the MediaRecorder interface begins recording media into one or more Blob objects.
You can
record the entire duration of the media into a single Blob (or until you
call MediaRecorder.requestData), or you can specify the
number of milliseconds to record at a time. Then, each time that amount of media has
been recorded, an event will be delivered to let you act upon the recorded media, while
a new Blob is created to record the next slice of the media.
Assuming the MediaRecorder's MediaRecorder.state
is inactive, start() sets the state to
recording, then begins capturing media from the input stream. A
Blob is created and the data is collected in it until the time slice period
elapses or the source media ends. Each time a Blob is filled up to that
point (the timeslice duration or the end-of-media, if no slice duration was provided), a
MediaRecorder.dataavailable_event event is sent to the MediaRecorder with the
recorded data. If the source is still playing, a new Blob is created and
recording continues into that, and so forth.
When the source stream ends, state is set to inactive and
data gathering stops. A final MediaRecorder.dataavailable_event event is sent to the
MediaRecorder, followed by a MediaRecorder/stop_event event.
[!NOTE] If the browser is unable to start recording or continue recording, it will raise an MediaRecorder.error_event event, followed by a MediaRecorder.dataavailable_event event containing the
Blobit has gathered, followed by the MediaRecorder/stop_event event.
Syntax
start()
start(timeslice)
Parameters
timeslice-
: The number of milliseconds to record into each Blob. If this parameter isn't included, the entire media duration is recorded into a single
Blobunless the MediaRecorder.requestData method is called to obtain theBloband trigger the creation of a newBlobinto which the media continues to be recorded.[!NOTE] Like other time values in web APIs,
timesliceis not exact and the real intervals may be slightly longer due to other pending tasks before the creation of the next blob.
-
Return value
None (undefined).
Exceptions
Errors that can be detected immediately are thrown as DOM exceptions. All other errors
are reported through MediaRecorder.error_event events sent to the MediaRecorder
object. You can implement the MediaRecorder.error_event event
handler to respond to these errors.
InvalidStateErrorDOMException- : Thrown if the
MediaRecorderis not in theinactivestate; you cannot start recording media if it is already being recorded. See the MediaRecorder.state property.
- : Thrown if the
NotSupportedErrorDOMException- : Thrown if:
- The media stream you are attempting to record is inactive.
- One or more of the stream's tracks is in a format that cannot be recorded using the current configuration
- The
videoKeyFrameIntervalDurationandvideoKeyFrameIntervalCountparameter are both specified when creating theMediaRecorder.
- : Thrown if:
SecurityErrorDOMException- : Thrown if the MediaStream is configured to disallow recording. This may be the case, for example, with sources obtained using MediaDevices.getUserMedia when the user denies permission to use an input device. This exception may also be delivered as an MediaRecorder.error_event event if the security options for the source media change after recording begins.
Examples
record.onclick = () => {
mediaRecorder.start();
console.log("recorder started");
};
Specifications
Browser compatibility
See also
- Using the MediaStream Recording API
- Web Dictaphone: MediaRecorder + getUserMedia + Web Audio API visualization demo, by Chris Mills (source on GitHub.)
- simpl.info MediaStream Recording demo, by Sam Dutton.
- MediaDevices.getUserMedia
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
