<audio> element 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
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
The above example shows simple usage of the
<audio> element. In a similar manner to the
<img> element, we include a path to the media we want to embed inside the
src attribute; we can include other attributes to specify information such as whether we want it to autoplay and loop, whether we want to show the browser's default audio controls, etc.
The content inside the opening and closing
<audio></audio> tags is shown as a fallback in browsers that don't support the element.
<audio controls> <source src="myAudio.mp3" type="audio/mp3"> <source src="myAudio.ogg" type="audio/ogg"> <p>Your browser doesn't support HTML5 audio. Here is a <a href="myAudio.mp4">link to the audio</a> instead.</p> </audio>
Other usage notes:
- If you don't specify the
- To allow precise control over your audio content,
HTMLMediaElements fire many different events.
<audio>elements can't have subtitles/captions associated with them in the same way that
<video>elements can. See WebVTT and Audio by Ian Devlin for some useful information and workarounds.
A good general source of information on using HTML
<video> is the Video and audio content beginner's tutorial.
This element's attributes include the global attributes.
- A Boolean attribute: if specified, the audio will automatically begin playback as soon as it can do so, without waiting for the entire audio file to finish downloading.
Note: Sites that automatically play audio (or videos with an audio track) can be an unpleasant experience for users, so should be avoided when possible. If you must offer autoplay functionality, you should make it opt-in (requiring a user to specifically enable it). However, this can be useful when creating media elements whose source will be set at a later time, under user control.
- 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.
- This enumerated attribute indicates whether to use CORS to fetch the related image. CORS-enabled resources can be reused in the
<canvas>element without being tainted. The allowed values are:
- Sends a cross-origin request without a credential. In other words, it sends the
Origin:HTTP header without a cookie, X.509 certificate, or performing HTTP Basic authentication. If the server does not give credentials to the origin site (by not setting the
Access-Control-Allow-Origin:HTTP header), the image will be tainted, and its usage restricted.
- Sends a cross-origin request with a credential. In other words, it sends the
Origin:HTTP header with a cookie, a certificate, or performing HTTP Basic authentication. If the server does not give credentials to the origin site (through
Access-Control-Allow-Credentials:HTTP header), the image will be tainted and its usage restricted.
Origin:HTTP header), preventing its non-tainted used in
<canvas>elements. If invalid, it is handled as if the enumerated keyword anonymous was used. See CORS settings attributes for additional information.
- A Boolean attribute: if specified, the audo player will automatically seek back to the start upon reaching the end of the audio.
- A Boolean attribute that indicates whether the audio will be initially silenced. Its default value is
- 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: 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 can be downloaded, even if the user is not expected to use it.
- empty string: A synonym of the
If not set,
preload's default value is browser-defined (i.e. each browser may have its own default value). The spec advises it to be set to
autoplayattribute has precedence over
autoplayis specified, the browser would obviously need to start downloading the audio for playback.
- The browser is not forced by the specification to follow the value of this attribute; it is a mere hint.
- 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.
Styling with CSS
<audio> element has no intrinsic visual output of its own unless the
controls attribute is specified, in which case the browser's default controls are shown.
The default controls have a
display value of
inline by default, and it is often a good idea set the value to
block to improve control over positioning and layout, unless you want it to sit within a text block or similar.
You can style the default controls with properties that affect the block as a single unit, so for example you can give it a
margin, etc. You can't however style the individual components inside the audio player (e.g. change the button size or icons, change the font, etc.), and the controls are different across the different browsers.
HTMLMediaElement API to wire up their functionality.
Video player styling basics provides some useful styling techniques — it is written in the context of
<video>, but much of it is equally applicable to
The following example shows simple usage of the
<audio> element to play an OGG file. It will autoplay due to the
autoplay attribute, and also includes fallback content.
<!-- Simple audio playback --> <audio src="AudioTest.ogg" autoplay> Your browser does not support the <code>audio</code> element. </audio>
<audio> element with
This example specifies which audio track to embed using the
src attribute on a nested
<source> element rather than directly on the
<audio> element. It is always useful to include the file's MIME type inside the
type attribute, as the browser is able to instantly tell if it can play that file, and not waste time on it if not.
<audio controls="controls"> <source src="foo.wav" type="audio/wav"> Your browser does not support the <code>audio</code> element. </audio>
<audio> with multiple
This example includes multiple
<source> elements. The browser tries to load the first source element (Opus) if it is able to play it; if not it 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>
Audio with spoken dialog should provide both captions and transcripts that accurately describe its content. Captions allow people who are experiencing hearing loss to understand an audio recording's content as the recording is being played, while transcripts allow people who need additional time to be able to review the recording's content at a pace and format that is comfortable for them.
If automatic captioning services are used, it is important to review the generated content to ensure it accurately represents the source audio.
In addition to spoken dialog, subtitles and transcripts should also identify music and sound effects that communicate important information. This includes emotion and tone:
1 00:00:00 --> 00:00:45 [Energetic techno music] 2 00:00:46 --> 00:00:51 Welcome to the Time Keeper's podcast! In this episode we're discussing which Swisswatch is a wrist switchwatch? 16 00:00:52 --> 00:01:02 [Laughing] Sorry! I mean, which wristwatch is a Swiss wristwatch?
- MDN Subtitles and closed caption — Plugins
- Web Video Text Tracks Format (WebVTT)
- WebAIM: Captions, Transcripts, and Audio Descriptions
- MDN Understanding WCAG, Guideline 1.2 explanations
- Understanding Success Criterion 1.2.1 | W3C Understanding WCAG 2.0
- Understanding Success Criterion 1.2.2 | W3C Understanding WCAG 2.0
|Content categories||Flow content, phrasing content, embedded content. If it has a
|Permitted content||If the element has a
Else: zero or more
|Tag omission||None, both the starting and ending tag are mandatory.|
|Permitted parents||Any element that accepts embedded content.|
|Permitted ARIA roles||
|HTML Living Standard
The definition of '<audio>' in that specification.
The definition of '<audio>' in that specification.
3.5 — 42
10.5 — 152
|Feature||Android webview||Chrome for Android||Edge mobile||Firefox for Android||Opera Android||iOS Safari||Samsung Internet|
Yes — 142
1. For Firefox to play audio, the server must serve the file using the correct MIME type.
2. Supported as