The <audio>
is used to embed sound content in documents. It may contain one or more audio sources, represented using the src
attribute or the <source>
element: the browser will choose the most suitable one. It can also be the destination for streamed media, using a MediaStream
.
Content categories |
Flow content, phrasing content, embedded content. If it has a controls attribute: interactive content and palpable content. |
---|---|
Permitted content | If the element has a src attribute: zero or more <track> element, followed by transparent content that contains no media elements, that is no <audio> or <video> Else: zero or more <source> element, followed by zero or more <track> element, followed by transparent content that contains no media elements, that is no <audio> or <video> . |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts embedded content. |
Permitted ARIA roles | application |
DOM interface | HTMLAudioElement |
This element's attributes include the global attributes.
autoplay
"false"
!), the audio will automatically begin playback as soon as it can do so, without waiting for the entire audio file to finish downloading. buffered
TimeRanges
object.controls
loop
mozCurrentSampleOffset
muted
played
TimeRanges
object indicating all the ranges of the audio that have been played.preload
none
: indicates that the audio should not be preloaded;metadata
: indicates that only audio metadata (e.g. length) is fetched;auto
: indicates that the whole audio file could be downloaded, even if the user is not expected to use it;auto
value.If not set, its default value is browser-defined (i.e. each browser may have its own default value). The spec advises it to be set to metadata
.
autoplay
attribute has precedence over preload
. If autoplay
is specified, the browser would obviously need to start downloading the audio for playback.src
<source>
element within the audio block to specify the audio to embed.volume
Time offsets are specified as float values indicating the number of seconds to offset.
An audio element can fire various events.
Fallback content for browsers not supporting the <audio>
element can be added inside the opening and closing <audio></audio>
tags.
The most basic playback functionality can be made available using the controls
attribute (see Examples below); for more advanced usage, audio playback and controls can be manipulated using the HTML Media API, and more specifically the features defined in the HTMLAudioElement
interface.
You can also use the Web Audio API to directly generate and manipulate audio streams from JavaScript code. See Web Audio API for details.
While HTML5 <video>
can have subtitles included via the <track>
element (see Adding captions and subtitles to HTML5 video), <audio>
elements can't — any <track>
elements included within <audio></audio>
tags are just ignored. See WebVTT and Audio by Ian Devlin for some more useful information and workarounds.
<!-- 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>
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>
<audio>
with multiple <source>
elementsThe browser tries to load the first source element (Opus) if it is able to play it, if not falls back to the second (vorbis) and finally back to mp3:
<audio controls=""> <source src="foo.opus" type="audio/ogg; codecs=opus"/> <source src="foo.ogg" type="audio/ogg; codecs=vorbis"/> <source src="foo.mp3" type="audio/mpeg"/> </audio>
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of '<audio>' in that specification. | Living Standard | |
HTML5 The definition of '<audio>' in that specification. | Recommendation |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 3 | Yes | 3.51 | 9 | 10.5 | 3.1 |
autoplay |
3 | Yes | 3.5 | 9 | 10.5 | 3.1 |
buffered |
? | Yes | 4 | ? | ? | ? |
controls |
3 | Yes | 3.5 | 9 | 10.5 | 3.1 |
loop |
3 | Yes | 11 | 9 | 10.5 | 3.1 |
mozcurrentsampleoffset |
No | No | 3.5 | No | No | No |
muted |
? | Yes | 11 | ? | ? | ? |
played |
49 | 14 | 15 | 11 | 46 | 9.1 |
preload |
3 | Yes |
4 3.5 — 42 |
9 |
15 10.5 — 152 |
3.1 |
src |
3 | Yes | 3.5 | 9 | 10.5 | 3.1 |
volume |
? | Yes | ? | ? | ? | ? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | 3 | 3 | Yes | 41 | ? | Yes | Yes |
autoplay |
3 | 3 | Yes | 4 | ? | Yes | Yes |
buffered |
? | ? | Yes | 4 | ? | ? | ? |
controls |
3 | 3 | Yes | 4 | ? | Yes | Yes |
loop |
3 | 3 | Yes | 14 | ? | Yes | Yes |
mozcurrentsampleoffset |
No | No | No | 4 | No | No | No |
muted |
? | ? | Yes | 14 | ? | ? | ? |
played |
49 | 49 | 14 | 15 | ? | Yes | Yes |
preload |
3 | 3 | Yes | 4 | ? |
15 Yes — 152 |
Yes |
src |
3 | 3 | Yes | 4 | ? | Yes | Yes |
volume |
? | ? | Yes | ? | ? | Yes | Yes |
1. For Firefox to play audio, the server must serve the file using the correct MIME type.
2. Supported as autobuffer
.
HTMLAudioElement
nsIDOMHTMLMediaElement
<source>
<video>
audio
element (HTML5 specification)
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio