<audio>

  • Revision slug: HTML/Element/audio
  • Revision title: <audio>
  • Revision id: 333637
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{HTMLVersionHeader("5")}}

Summary

The HTML <audio> element is used to represents sound content in documents. Added as part of HTML5, it may contain several audio sources, represented using the src attribute or the {{HTMLElement("source")}} element; the browser will choose the most suitable one. Fallback content for browser not supporting the <audio> element can be added too.

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

  • Content categories Flow content, phrasing content, embedded content. If it has a {{htmlattrxref("controls", "audio")}} attribute: interactive content and palpable content.
  • Permitted contentIf the element has a {{htmlattrxref("src", "audio")}} attribute: zero or more {{HTMLElement("track")}} element, followed by transparent content that contains no media elements, that is no {{HTMLElement("audio")}} or {{HTMLElement("video")}}
    Else: zero or more {{HTMLElement("source")}} element, followed by zero or more {{HTMLElement("track")}} element, followed by transparent content that contains no media elements, that is no {{HTMLElement("audio")}} or {{HTMLElement("video")}}.
  • Tag omission {{no_tag_omission}}
  • Permitted parent elements Any element that accepts embedded content.
  • DOM interface {{domxref("HTMLAudioElement")}}

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 audio or that the server wants to minimize its traffic; in others terms this hint indicates that the audio should not be cached;
  • metadata: hints that though the author thinks that the user won't need to consult that audio, 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 audio 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 audio, 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

Basic usage

<!-- 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> 

Audio element with source element

<audio controls="controls">
  Your browser does not support the <code>audio</code> element.
  <source src="foo.wav" type="audio/wav">
</audio>

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', 'the-video-element.html#the-audio-element', '<audio>')}} {{Spec2('HTML WHATWG')}}  
{{SpecName('HTML5 W3C', 'the-audio-element.html#the-audio-element', '<article>')}} {{Spec2('HTML5 W3C')}}  

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 <strong>HTML <code>&lt;audio&gt;</code> element</strong> is used to represents sound content in documents. Added as part of HTML5, it may contain several audio sources, represented using the <code>src</code> attribute or the {{HTMLElement("source")}} element; the browser will choose the most suitable one. Fallback content for browser not supporting the <code>&lt;audio&gt;</code> element can be added too.</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="Introducing the Audio API Extension">Introducing the audio API extension</a> for details.</p>
<h2 id="Usage_Context">Usage Context</h2>
<ul class="htmlelt">
  <li><dfn><a href="/en-US/docs/HTML/Content_categories" title="HTML/Content_categories">Content categories</a></dfn> <a href="/en-US/docs/HTML/Content_categories#Flow_content" title="HTML/Content categories#Flow content">Flow content</a>, phrasing content, embedded content. If it has a {{htmlattrxref("controls", "audio")}} attribute: interactive content and palpable content.</li>
  <li><dfn>Permitted content</dfn>If the element has a {{htmlattrxref("src", "audio")}} attribute: zero or more {{HTMLElement("track")}} element, followed by transparent content that contains no media elements, that is no {{HTMLElement("audio")}} or {{HTMLElement("video")}}<br />
    Else: zero or more {{HTMLElement("source")}} element, followed by zero or more {{HTMLElement("track")}} element, followed by transparent content that contains no media elements, that is no {{HTMLElement("audio")}} or {{HTMLElement("video")}}.</li>
  <li><dfn>Tag omission</dfn> {{no_tag_omission}}</li>
  <li><dfn>Permitted parent elements</dfn> Any element that accepts embedded content.</li>
  <li><dfn>DOM interface</dfn> {{domxref("HTMLAudioElement")}}</li>
</ul>
<h2 id="Attributes">Attributes</h2>
<p>Like all other HTML elements, this element supports the <a href="/en-US/docs/HTML/Global_attributes" title="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 audio or that the server wants to minimize its traffic; in others terms this hint indicates that the audio 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 audio, 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 audio 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 <code>autoplay</code> attribute has precedence over this one as if one wants to automatically play a audio, the browser will obviously need to download it. Setting both the <code>autoplay</code> and the <code>preload</code> 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 href="/en-US/docs/HTTP_access_control" title="HTTP access control">HTTP access controls</a>. This is optional; you may instead use the <a href="/en-US/docs/HTML/Element/source" title="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>
<h3 id="Basic_usage">Basic usage</h3>
<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>
<h3 id="Audio_element_with_source_element">Audio element with source element</h3>
<pre class="brush: html">
&lt;audio controls="controls"&gt;
  Your browser does not support the &lt;code&gt;audio&lt;/code&gt; element.
  &lt;source src="foo.wav" type="audio/wav"&gt;
&lt;/audio&gt;
</pre>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{SpecName('HTML WHATWG', 'the-video-element.html#the-audio-element', '&lt;audio&gt;')}}</td>
      <td>{{Spec2('HTML WHATWG')}}</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>{{SpecName('HTML5 W3C', 'the-audio-element.html#the-audio-element', '&lt;article&gt;')}}</td>
      <td>{{Spec2('HTML5 W3C')}}</td>
      <td>&nbsp;</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</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 href="/en-US/docs/Media_formats_supported_by_the_audio_and_video_elements" title="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="Introducing the Audio API Extension">Introducing the audio API extension</a></li>
  <li><a href="/en-US/docs/DOM/HTMLAudioElement" title="DOM/HTMLAudioElement"><code>HTMLAudioElement</code></a></li>
  <li><a href="/en-US/docs/XPCOM_Interface_Reference/NsIDOMHTMLMediaElement" title="NsIDOMHTMLMediaElement"><code>nsIDOMHTMLMediaElement</code></a></li>
  <li><a href="/en-US/docs/HTML/Element/source" title="HTML/Element/source"><code>source</code></a></li>
  <li><a href="/en-US/docs/HTML/Element/Video" title="HTML/Element/Video"><code>video</code></a></li>
  <li><a href="/en-US/docs/Using_HTML5_audio_and_video" title="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>
Revert to this revision