<audio>

  • Revision slug: HTML/Element/Audio
  • Revision title: audio
  • Revision id: 293668
  • Created:
  • Creator: mateus.m.luna
  • Is current revision? No
  • Comment

Revision Content

{{ HTMLVersionHeader("5") }}

Summary

The audio element is used to embed sound content in an HTML or XHTML document. The audio element was added as part of HTML5.

You can use the enhanced audio API features — which are Gecko-specific — to directly generate and manipulate audio streams from JavaScript code. See Introducing the audio API extension for details.

Usage Context

Permitted content Transparent content, containing either a src attribute or one or more {{ HTMLElement("source") }} elements, followed by either flow content or phrasing content , with no {{ HTMLElement("video") }} or <audio> elements.
Tag omission None, both start and end tags must be present
Permitted parent elements Any element that accepts flow content, or any element that accepts phrasing content.
Normative document HTML5, section 4.8.7

Attributes

Like all other HTML elements, this element supports the global attributes.

{{ htmlattrdef("autoplay") }}
A Boolean attribute; if specified (even if the value is "false"!), the audio will automatically begin to play back as soon as it can do so without stopping to finish loading the data.
{{ htmlattrdef("autobuffer") }} {{ obsolete_inline("2.0") }}
A Boolean attribute; if specified, the audio will automatically begin being downloaded, even if not set to automatically play. This continues until the media cache is full, or the entire audio file has been downloaded, whichever comes first. This should only be used when it is expected that the user will choose to play the audio; for example, if the user has navigated to a page using a "Play this audio" link. This attribute was removed in Gecko 2.0 {{ geckoRelease("2.0") }} in favor of the preload attribute.
{{ htmlattrdef("buffered") }}
An attribute you can read to determine which time ranges of the media have been buffered. This attribute contains a {{ domxref("TimeRanges") }} object.
{{ htmlattrdef("controls") }}
If this attribute is present, the browser will offer controls to allow the user to control audio playback, including volume, seeking, and pause/resume playback.
{{ htmlattrdef("loop") }}
A Boolean attribute; if specified, we will, upon reaching the end of the audio, automatically seek back to the start.
{{ htmlattrdef("mozCurrentSampleOffset") }} {{ gecko_minversion_inline("2.0") }} {{ non-standard_inline() }}
The offset, specified as the number of samples since the beginning of the audio stream, at which the audio is currently playing.
{{ htmlattrdef("muted") }}
A Boolean attribute which indicates whether the audio will be initially silenced. Its default value is false, meaning that the audio will be played.
{{ htmlattrdef("played") }}
A {{ domxref("TimeRanges") }} object indicating all the ranges of the video that have been played.
{{ htmlattrdef("preload") }}
This enumerated attribute is intended to provide a hint to the browser about what the author thinks will lead to the best user experience. It may have one of the following values:
  • none: hints that either the author thinks that the user won't need to consult that video or that the server wants to minimize its traffic; in others terms this hint indicates that the video should not be cached;
  • metadata: hints that though the author thinks that the user won't need to consult that video, fetching the metadata (e.g. length) is reasonable;
  • auto: hints that the user needs have priority; in others terms this hint indicated that, if needed, the whole video could be downloaded, even if the user is not expected to use it;
  • the empty string: which is a synonym of the auto value.

If not set, its default value is browser-defined (i.e. each browser can choose its own default value), though the spec advises it to be set to metadata.

Usage notes:
  • The autoplay attribute has precedence over this one as if one wants to automatically play a video, the browser will obviously need to download it. Setting both the autoplay and the preload attributes is allowed by the specification.
  • The browser is not forced by the specification to follow the value of this attribute; it is a mere hint.
{{ htmlattrdef("src") }}
The URL of the audio to embed. This is subject to HTTP access controls. This is optional; you may instead use the source element within the audio block to specify the audio to embed.

Time offsets are specified as float values indicating the number of seconds to offset.

Note: The time offset value definition has not yet been completed in the HTML5 specification and is subject to change.

Examples

<!-- Simple audio playback -->
<audio src="http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg" autoplay>
  Your browser does not support the <code>audio</code> element.
</audio>

<!-- Audio playback with captions -->
<audio src="foo.ogg">
  <track kind="captions" src="foo.en.vtt" srclang="en" label="English">
  <track kind="captions" src="foo.sv.vtt" srclang="sv" label="Svenska">
</audio> 

Plays the audio file attached to this article.

DOM Interface

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 3.0 {{ CompatGeckoDesktop("1.9.1") }} 9.0 10.5 3.1
autoplay attribute 3.0 {{ CompatGeckoDesktop("1.9.1") }} 9.0 10.5 3.1
buffered attribute {{ CompatUnknown() }} {{ CompatGeckoDesktop("2.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
controls attribute 3.0 {{ CompatGeckoDesktop("1.9.1") }} 9.0 10.5 3.1
loop attribute 3.0 {{ CompatGeckoDesktop("11.0") }} 9.0 10.5 3.1
muted attribute {{ CompatUnknown() }} {{ CompatGeckoDesktop("11.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
played property {{ CompatUnknown() }} {{ CompatGeckoDesktop("15.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
preload attribute 3.0 {{ CompatGeckoDesktop("2.0") }} 9.0 Supported under the older name autobuffer 3.1
src attribute 3.0 {{ CompatGeckoDesktop("1.9.1") }} 9.0 10.5 3.1
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatGeckoMobile("1.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
autoplay attribute {{ CompatUnknown() }} {{ CompatGeckoMobile("1.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
buffered attribute {{ CompatUnknown() }} {{ CompatGeckoMobile("2.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
controls attribute {{ CompatUnknown() }} {{ CompatGeckoMobile("1.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
loop attribute {{ CompatUnknown() }} {{ CompatGeckoMobile("11.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
muted attribute {{ CompatUnknown() }} {{ CompatGeckoMobile("11.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
played property {{ CompatUnknown() }} {{ CompatGeckoMobile("15.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
preload attribute {{ CompatUnknown() }} {{ CompatGeckoMobile("2.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
src attribute {{ CompatUnknown() }} {{ CompatGeckoMobile("1.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Note: For Gecko to play audio, the server must serve the file using the correct MIME type.

See also

Revision Source

<div>
  {{ HTMLVersionHeader("5") }}</div>
<h2 id="Summary">Summary</h2>
<p>The <code>audio</code> element is used to embed sound content in an HTML or XHTML document. The <code>audio</code> element was added as part of HTML5.</p>
<p>You can use the enhanced audio API features — which are Gecko-specific — to directly generate and manipulate audio streams from JavaScript code. See <a href="/en-US/docs/Introducing_the_Audio_API_Extension" title="en-US/docs/Introducing the Audio API Extension">Introducing the audio API extension</a> for details.</p>
<h2 id="Usage_Context">Usage Context</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td>Permitted content</td>
      <td><a href="/en-US/docs/HTML/Content_categories#transparent_content" title="en-US/docs/HTML/Content categories#transparent content">Transparent content</a>, containing either a <code>src</code> attribute or one or more {{ HTMLElement("source") }} elements, followed by either <a href="/en-US/docs/HTML/Content_categories#flow_content" title="en-US/docs/HTML/Content categories#flow content">flow content</a> or <a href="/en-US/docs/HTML/Content_categories#phrasing_content" title="en-US/docs/HTML/Content categories#phrasing content">phrasing content</a> , with no {{ HTMLElement("video") }} or <code>&lt;audio&gt;</code> elements.</td>
    </tr>
    <tr>
      <td>Tag omission</td>
      <td>None, both start and end tags must be present</td>
    </tr>
    <tr>
      <td>Permitted parent elements</td>
      <td>Any element that accepts <a href="/en-US/docs/HTML/Content_categories#flow_content" rel="internal">flow content</a>, or any element that accepts <a href="/en-US/docs/HTML/Content_categories#phrasing_content" rel="internal">phrasing content</a>.</td>
    </tr>
    <tr>
      <td>Normative document</td>
      <td><a class="external" href="http://www.w3.org/TR/html5/video.html#audio" title="http://www.w3.org/TR/html5/video.html#audio">HTML5, section 4.8.7</a></td>
    </tr>
  </tbody>
</table>
<h2 id="Attributes">Attributes</h2>
<p>Like all other HTML elements, this element supports the <a href="/en-US/docs/HTML/Global_attributes" title="en-US/docs/HTML/Global attributes">global attributes</a>.</p>
<dl>
  <dt>
    {{ htmlattrdef("autoplay") }}</dt>
  <dd>
    A Boolean attribute; if specified (even if the value is "false"!), the audio will automatically begin to play back as soon as it can do so without stopping to finish loading the data.</dd>
  <dt>
    {{ htmlattrdef("autobuffer") }} {{ obsolete_inline("2.0") }}</dt>
  <dd>
    A Boolean attribute; if specified, the audio will automatically begin being downloaded, even if not set to automatically play. This continues until the media cache is full, or the entire audio file has been downloaded, whichever comes first. This should only be used when it is expected that the user will choose to play the audio; for example, if the user has navigated to a page using a "Play this audio" link. This attribute was removed in Gecko 2.0 {{ geckoRelease("2.0") }} in favor of the <code>preload</code> attribute.</dd>
  <dt>
    {{ htmlattrdef("buffered") }}</dt>
  <dd>
    An attribute you can read to determine which time ranges of the media have been buffered. This attribute contains a {{ domxref("TimeRanges") }} object.</dd>
  <dt>
    {{ htmlattrdef("controls") }}</dt>
  <dd>
    If this attribute is present, the browser will offer controls to allow the user to control audio playback, including volume, seeking, and pause/resume playback.</dd>
  <dt>
    {{ htmlattrdef("loop") }}</dt>
  <dd>
    A Boolean attribute; if specified, we will, upon reaching the end of the audio, automatically seek back to the start.</dd>
  <dt>
    {{ htmlattrdef("mozCurrentSampleOffset") }} {{ gecko_minversion_inline("2.0") }} {{ non-standard_inline() }}</dt>
  <dd>
    The offset, specified as the number of samples since the beginning of the audio stream, at which the audio is currently playing.</dd>
  <dt>
    {{ htmlattrdef("muted") }}</dt>
  <dd>
    A Boolean attribute which indicates whether the audio will be initially silenced. Its default value is false, meaning that the audio will be played.</dd>
  <dt>
    {{ htmlattrdef("played") }}</dt>
  <dd>
    A {{ domxref("TimeRanges") }} object indicating all the ranges of the video that have been played.</dd>
  <dt>
    {{ htmlattrdef("preload") }}</dt>
  <dd>
    This enumerated attribute is intended to provide a hint to the browser about what the author thinks will lead to the best user experience. It may have one of the following values:
    <ul>
      <li><span style="font-family: Courier New;">none: </span>hints that either the author thinks that the user won't need to consult that video or that the server wants to minimize its traffic; in others terms this hint indicates that the video should not be cached;</li>
      <li><span style="font-family: Courier New;">metadata</span>: hints that though the author thinks that the user won't need to consult that video, fetching the metadata (e.g. length) is reasonable;</li>
      <li><span style="font-family: Courier New;">auto</span>: hints that the user needs have priority; in others terms this hint indicated that, if needed, the whole video could be downloaded, even if the user is not expected to use it;</li>
      <li>the <em>empty string</em>: which is a synonym of the <span style="font-family: Courier New;">auto </span>value.</li>
    </ul>
    <p>If not set, its default value is browser-defined (i.e. each browser can choose its own default value), though the spec advises it to be set to <span style="font-family: Courier New;">metadata</span>.</p>
    <div class="note">
      <strong>Usage notes:</strong>
      <ul>
        <li>The <strong>autoplay </strong>attribute has precedence over this one as if one wants to automatically play a video, the browser will obviously need to download it. Setting both the <strong>autoplay</strong> and the <strong>preload</strong> attributes is allowed by the specification.</li>
        <li>The browser is not forced by the specification to follow the value of this attribute; it is a mere hint.</li>
      </ul>
    </div>
  </dd>
  <dt>
    {{ htmlattrdef("src") }}</dt>
  <dd>
    The URL of the audio to embed. This is subject to <a class="internal" href="/en-US/docs/HTTP_access_control" title="en-US/docs/HTTP access control">HTTP access controls</a>. This is optional; you may instead use the <a class="internal" href="/en-US/docs/HTML/Element/source" title="en-US/docs/HTML/Element/Source"><code>source</code></a> element within the audio block to specify the audio to embed.</dd>
</dl>
<p>Time offsets are specified as float values indicating the number of seconds to offset.</p>
<div class="note">
  <strong>Note:</strong> The time offset value definition has not yet been completed in the HTML5 specification and is subject to change.</div>
<h2 id="Examples">Examples</h2>
<pre class="brush: html">
&lt;!-- Simple audio playback --&gt;
&lt;audio src="http://developer.mozilla.org/@api/deki/files/2926/=AudioTest_(1).ogg" autoplay&gt;
  Your browser does not support the &lt;code&gt;audio&lt;/code&gt; element.
&lt;/audio&gt;

&lt;!-- Audio playback with captions --&gt;
&lt;audio src="foo.ogg"&gt;
  &lt;track kind="captions" src="foo.en.vtt" srclang="en" label="English"&gt;
  &lt;track kind="captions" src="foo.sv.vtt" srclang="sv" label="Svenska"&gt;
&lt;/audio&gt; 
</pre>
<p>Plays the audio file attached to this article.</p>
<h2 id="DOM_Interface">DOM Interface</h2>
<ul>
  <li><a href="/en-US/docs/DOM/HTMLAudioElement" title="en-US/docs/DOM/HTMLAudioElement">HTMLAudioElement</a></li>
</ul>
<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</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("1.9.1") }}</td>
        <td>9.0</td>
        <td>10.5</td>
        <td>3.1</td>
      </tr>
      <tr>
        <td><code>autoplay</code> attribute</td>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("1.9.1") }}</td>
        <td>9.0</td>
        <td>10.5</td>
        <td>3.1</td>
      </tr>
      <tr>
        <td><code>buffered</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("2.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>controls</code> attribute</td>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("1.9.1") }}</td>
        <td>9.0</td>
        <td>10.5</td>
        <td>3.1</td>
      </tr>
      <tr>
        <td><code>loop</code> attribute</td>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("11.0") }}</td>
        <td>9.0</td>
        <td>10.5</td>
        <td>3.1</td>
      </tr>
      <tr>
        <td><code>muted</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("11.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>played</code> property</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("15.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>preload</code> attribute</td>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("2.0") }}</td>
        <td>9.0</td>
        <td>Supported under the older name <code>autobuffer</code></td>
        <td>3.1</td>
      </tr>
      <tr>
        <td><code>src</code> attribute</td>
        <td>3.0</td>
        <td>{{ CompatGeckoDesktop("1.9.1") }}</td>
        <td>9.0</td>
        <td>10.5</td>
        <td>3.1</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>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("1.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>autoplay</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("1.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>buffered</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("2.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>controls</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("1.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>loop</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("11.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>muted</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("11.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>played</code> property</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("15.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>preload</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("2.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
      <tr>
        <td><code>src</code> attribute</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatGeckoMobile("1.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div class="note">
  <strong>Note:</strong> For Gecko to play audio, the server must serve the file using the correct MIME type.</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a class="internal" href="/en-US/docs/Media_formats_supported_by_the_audio_and_video_elements" title="en-US/docs/Media formats supported by the audio and video elements">Media formats supported by the audio and video elements</a></li>
  <li><a href="/en-US/docs/Introducing_the_Audio_API_Extension" title="en-US/docs/Introducing the Audio API Extension">Introducing the audio API extension</a></li>
  <li><a href="/en-US/docs/DOM/HTMLAudioElement" title="en-US/docs/DOM/HTMLAudioElement"><code>HTMLAudioElement</code></a></li>
  <li><a class="internal" href="/en-US/docs/XPCOM_Interface_Reference/NsIDOMHTMLMediaElement" title="en-US/docs/NsIDOMHTMLMediaElement"><code>nsIDOMHTMLMediaElement</code></a></li>
  <li><a class="internal" href="/en-US/docs/HTML/Element/Video" title="en-US/docs/HTML/Element/Video"><code>video</code></a></li>
  <li><a class="internal" href="/en-US/docs/Using_HTML5_audio_and_video" title="en-US/docs/Using audio and video in Firefox">Using audio and video</a></li>
  <li><a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/#audio" title="http://www.whatwg.org/specs/web-apps/current-work/#audio">The <code>audio</code> element</a> (HTML5 specification)</li>
</ul>
<!-- languages( { "es": "es/docs/HTML/Elemento/audio", "fr": "fr/docs/HTML/Element/audio" }) -->
Revert to this revision