<audio>

Introduced in HTML5

Summary

The HTML <audio> element is used to represent sound content in documents. Added as part of HTML5, it may contain several audio sources, represented using the src attribute or the <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 controls attribute: interactive content and palpable content.
  • Permitted contentIf 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 parent elements Any element that accepts embedded content.
  • DOM interface HTMLAudioElement

Attributes

This element includes the global attributes.

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.
autobuffer Obsolete since Gecko 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 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) in favor of the preload attribute.
buffered
An attribute you can read to determine which time ranges of the media have been buffered. This attribute contains a TimeRanges object.
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.
loop
A Boolean attribute; if specified, we will, upon reaching the end of the audio, automatically seek back to the start.
mozCurrentSampleOffset
The offset, specified as the number of samples since the beginning of the audio stream, at which the audio is currently playing.
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.
played
A TimeRanges object indicating all the ranges of the audio that have been played.
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.
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.
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.

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
WHATWG HTML Living Standard Living Standard  
HTML5 Candidate Recommendation  

Browser compatibility

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

See also

Attachments

File Size Date Attached by
AudioTest (1).ogg
42324 bytes 2008-10-09 19:50:31 Sheppy
Zapfino.svg
1399728 bytes 2009-12-25 15:15:25 andydude