Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 891391 of MediaRecorder.start()

  • Revision slug: Web/API/MediaRecorder/start
  • Revision title: MediaRecorder.start()
  • Revision id: 891391
  • Created:
  • Creator: Sebastianz
  • Is current revision? No
  • Comment Improved spec link

Revision Content

{{APIRef("Media Recorder API")}}

The Media.start() method (part of the MediaRecorder API) is used to start capturing media into a {{domxref("Blob")}}.

When the start() method is invoked, the UA queues a task that runs the following steps:

  1. If the {{domxref("MediaRecorder.state")}} is not "inactive", raise a DOM InvalidState error and terminate these steps. if the {{domxref("MediaRecorder.state")}} is "inactive", continue on to the next step.
  2. Set the {{domxref("MediaRecorder.state")}} to "recording" and wait until media becomes available from the stream passed into {{domxref("Navigator.getUserMedia")}}.
  3. Once data becomes available, raise a {{domxref("MediaRecorder.start")}} event and start gathering the data into a {{domxref("Blob")}} (see FILE-API).
  4. If the timeSlice argument has been provided, once that many milliseconds of data have been collected — or a minimum time slice imposed by the UA, whichever is greater — raise a {{domxref("MediaRecorder.dataavailable")}} event containing the Blob of collected data, and start gathering a new Blob of data. If timeSlice has not been provided, continue gathering data into the original Blob.
  5. When the stream is ended, set {{domxref("MediaRecorder.state")}} to "inactive" and stop gathering data.
  6. Raise a {{domxref("MediaRecorder.dataavailable")}} event containing the Blob of data.
  7. Raise a {{domxref("MediaRecorder.stop")}} event.

Note: If the browser is unable to start recording or continue recording, it will raise a {{domxref("DOMError")}} event, followed by a {{domxref("MediaRecorder.dataavailable")}} event containing the Blob it has gathered, followed by the {{domxref("MediaRecorder.stop")}} event.

Syntax

 void start (optional long timeslice);

Parameters

timeslice {{optional_inline}}
This parameter takes a value of milliseconds, and represents the length of media capture to return in each Blob. If it is not specified, all media captured will be returned in a single Blob, unless one or more calls are made to {{domxref("MediaRecorder.requestData")}}.

Errors

An InvalidState error is raised if the start() method is called while the MediaRecorder object’s {{domxref("MediaRecorder.state")}} is not "inactive" — it makes no sense to start media capture if it is already happening.

Example

...

  record.onclick = function() {
    mediaRecorder.start();
    console.log("recorder started");
  }

...

Specifications

Specification Status Comment
{{SpecName("MediaStream Recording", "#widl-MediaRecorder-start-void-long-timeslice", "MediaRecorder.start()")}} {{Spec2("MediaStream Recording")}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatNo}} {{CompatGeckoDesktop("25.0")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoDesktop("25.0")}} 1.3[1] {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] The intial Firefox OS implementation only supported audio recording.

See also

Revision Source

<div>{{APIRef("Media Recorder API")}}</div>

<p><span class="seoSummary">The <strong><code>Media.start()</code></strong> method (part of the <a href="/en-US/docs/Web/API/MediaRecorder_API">MediaRecorder API</a>) is used to start capturing media into a {{domxref("Blob")}}.</span></p>

<p>When the <code>start()</code> method is invoked, the UA queues a task that runs the following steps:</p>

<ol>
 <li>If the {{domxref("MediaRecorder.state")}} is not "inactive", raise a DOM <code>InvalidState</code> error and terminate these steps. if the {{domxref("MediaRecorder.state")}} is "inactive", continue on to the next step.</li>
 <li>Set the {{domxref("MediaRecorder.state")}} to "recording" and wait until media becomes available from the <code>stream</code> passed into {{domxref("Navigator.getUserMedia")}}.</li>
 <li>Once data becomes available, raise a {{domxref("MediaRecorder.start")}} event and start gathering the data into a {{domxref("Blob")}} (see <cite><a class="bibref" href="https://dvcs.w3.org/hg/dap/raw-file/default/media-stream-capture/MediaRecorder.html#bib-FILE-API">FILE-API</a></cite>).</li>
 <li>If the <code>timeSlice</code> argument has been provided, once that many milliseconds of data have been collected — or a minimum time slice imposed by the UA, whichever is greater — raise a {{domxref("MediaRecorder.dataavailable")}} event containing the Blob of collected data, and start gathering a new Blob of data. If <code>timeSlice</code> has not been provided, continue gathering data into the original Blob.</li>
 <li>When the <code>stream</code> is ended, set {{domxref("MediaRecorder.state")}} to "inactive" and stop gathering data.</li>
 <li>Raise a {{domxref("MediaRecorder.dataavailable")}} event containing the Blob of data.</li>
 <li>Raise a {{domxref("MediaRecorder.stop")}} event.</li>
</ol>

<div class="note">
<p><strong>Note</strong>: If the browser is unable to start recording or continue recording, it<em> will</em> raise a {{domxref("DOMError")}} event, followed by a {{domxref("MediaRecorder.dataavailable")}} event containing the Blob it has gathered, followed by the {{domxref("MediaRecorder.stop")}} event.</p>
</div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
<span class="idlInterface" id="idl-def-IDBIndex"><span class="idlMethod"><span class="idlInterface" id="idl-def-MediaRecorder"><span class="idlMethod"><span class="idlMethType"><span class="idlInterface" id="idl-def-MediaRecorder"><span class="idlMethod"><span class="idlMethType"><span class="idlInterface" id="idl-def-MediaRecorder"><span class="idlMethod"> <span class="idlMethType">void</span> <span class="idlMethName"><a href="https://dvcs.w3.org/hg/dap/raw-file/default/media-stream-capture/MediaRecorder.html#widl-MediaRecorder-start-void-long-timeslice">start</a></span> (<span class="idlParam">optional <span class="idlParamType">long</span> <span class="idlParamName">timeslice</span></span>);</span></span></span></span></span></span></span></span></span></span></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>timeslice</code> {{optional_inline}}</dt>
 <dd>This parameter takes a value of milliseconds, and represents the length of media capture to return in each Blob. If it is not specified, all media captured will be returned in a single Blob, unless one or more calls are made to {{domxref("MediaRecorder.requestData")}}.</dd>
</dl>

<h3 id="Errors">Errors</h3>

<p>An <code>InvalidState</code> error is raised if the <code>start()</code> method is called while the <code>MediaRecorder</code> object’s {{domxref("MediaRecorder.state")}} is not "inactive" — it makes no sense to start media capture if it is already happening.</p>

<h2 id="Example">Example</h2>

<pre class="brush: js">
...

  record.onclick = function() {
    mediaRecorder.start();
    console.log("recorder started");
  }

...</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName("MediaStream Recording", "#widl-MediaRecorder-start-void-long-timeslice", "MediaRecorder.start()")}}</td>
   <td>{{Spec2("MediaStream Recording")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("25.0")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("25.0")}}</td>
   <td>1.3<sup>[1]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] The intial Firefox OS implementation only supported audio recording.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/MediaRecorder_API/Using_the_MediaRecorder_API">Using the MediaRecorder API</a></li>
 <li><a href="https://mdn.github.io/web-dictaphone/">Web Dictaphone</a>: MediaRecorder + getUserMedia + Web Audio API visualization demo, by <a href="https://twitter.com/chrisdavidmills">Chris Mills</a> (<a href="https://github.com/mdn/web-dictaphone/">source on Github</a>.)</li>
 <li><a href="https://simpl.info/mediarecorder/">simpl.info MediaStream Recording demo</a>, by <a href="https://twitter.com/sw12">Sam Dutton</a>.</li>
 <li>{{domxref("Navigator.getUserMedia")}}</li>
</ul>
Revert to this revision