Summary
The HTML <audio>
element is used to embed sound content in documents. 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 Web Audio API to directly generate and manipulate audio streams from JavaScript code. See Web Audio API 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
This element includes 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, when specified, will automatically seek back to the start upon reaching the end of the audio.
- {{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 audio 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 theautoplay
and thepreload
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 {{htmlelement("source")}} element within the audio block to specify the audio to embed.
- {{htmlattrdef("volume")}}
- The playback volume of any audio portions, in the range 0.0 (silent) to 1.0 (loudest).
Time offsets are specified as float values indicating the number of seconds to offset.
Events
An audio element can fire various events.
Examples
Basic usage
<!-- Simple audio playback --> <audio src="https://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', 'embedded-content-0.html#the-audio-element', '<audio>')}} | {{Spec2('HTML5 W3C')}} |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 3.0 | {{CompatGeckoDesktop("1.9.1")}} [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 | {{CompatVersionUnknown}} [2] | 3.1 |
src attribute |
3.0 | {{CompatGeckoDesktop("1.9.1")}} | 9.0 | 10.5 | 3.1 |
volume attribute |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatGeckoMobile("1.0")}} [1] | {{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}} |
volume attribute |
[1] For Gecko to play audio, the server must serve the file using the correct MIME type.
[2] Supported under the older name autobuffer
See also
- Media formats supported by the audio and video elements
- Web Audio API
- {{domxref("HTMLAudioElement")}}
nsIDOMHTMLMediaElement
- {{htmlelement("source")}}
- {{htmlelement("video")}}
- Using audio and video
- The
audio
element (HTML5 specification)
{{HTMLRef}}