HTML audio - how to use <audio> tag

The audio tag is used to play a sound on the web document. Currently, different browsers support different formats but MP3 is suuported by most browsers. Multiple audio sources can also be defined and the browser can choose the preferred one. The audio tag supports 3 formats - MP3, Wav and Ogg.

Older browsers may not support audio formats and thus a fallback text can also be used to display appropriate message. The failback message can be inserted within the audio tag.

The Web audio API can be used to directly manipulate the audio streams from JavaScript code.

Attributes

The audio tag supports the global and event attributes.

autoplay
This is a boolean attribute indicating to automatically play the audio file.
buffered
This attribute provides information on the time range the audio has been buffered.
controls
The presence of this attribute indicates the browser to display controls such as volume, seeking and play / pause.
loop
This is a boolean attribute that when specified will restart playing the audio file when completed.
muted
This is a boolean attribute that indicates whether the audio should be muted when started.
played
This indicates how far the audio file has been played.
preload
This provides data to the browser on how to best deal with the audio file.
Possible values include auto, metadata and none.
src
This indicates the source of the audio file.
volume
This indicates the playback volume. 0.0 is silent and 1.0 is maximum.

Mime Types

Formatmime type
MP3audio/mpeg
Oggaudio/ogg
Wavaudio/wav

Examples:

<div> <audio controls="controls" loop="loop"> Your browser does not support the <code>audio</code> element. <source src="foo.wav" type="audio/wav"> <source src="foo.ogg" type="audio/ogg"> </audio> </div>