<audio>: The Embed Audio element

The HTML <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 MediaStream.

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

Attributes

A Boolean attribute: if specified (even if the value is "false"!), the audio will automatically begin playback as soon as it can do so, without waiting for the entire audio file to finish downloading.

Sites which automatically play audio (or videos with an audio track) can be an unpleasant experience for users, so it 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.

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, will automatically seek back to the start upon reaching the end of the audio.

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.

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: 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;

the empty string: synonym of the 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.

Usage notes:

The autoplay attribute has precedence over preload. If autoplay is 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.

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, 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.

Events

Usage notes

Basics

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.

Audio streams/Web Audio API

You can also use the Web Audio API to directly generate and manipulate audio streams from JavaScript code. See Web Audio API for details.