Skip to main content

What does the <audio> element do?

The <audio> element is used to play sound on a web page: music, podcasts, effects, voiceover recordings, and so on. It works similarly to <video>, but without a video stream: the browser creates an audio player with "play / pause" buttons, volume, and a timer.


1. Simplest example

html
<audio src="music.mp3" controls></audio>

What's here:

  • src="music.mp3": the path to the audio file;
  • controls: displays the standard control panel (play, pause, volume).

Result: the browser embeds an audio player, and the user can listen to the sound right on the page.


To support different formats (MP3, OGG, WAV), you can specify several <source> tags: the browser will pick the right one:

html
<audio controls> <source src="audio.mp3" type="audio/mpeg"> <source src="audio.ogg" type="audio/ogg"> <p>Your browser does not support the audio element.</p> </audio>

If the browser does not support any of the formats, it shows the text from <p>.


3. Main attributes of <audio>

AttributePurposeExample
srcPath to the audio file (if <source> is not used)src="sound.mp3"
controlsShows the control panelcontrols
autoplayStarts the audio automatically when the page loads (usually requires muted)autoplay
loopRepeats playback in a looploop
mutedMutes the sound by defaultmuted
preloadControls preloading (none, metadata, auto)preload="metadata"

4. How preload works

ValueBehavior
noneThe audio does not load until the user clicks play
metadataOnly information (duration, title, tags) is loaded
autoThe audio may load fully in advance

Example:

html
<audio src="podcast.mp3" preload="metadata" controls></audio>

5. Example with several attributes

html
<audio controls autoplay loop muted preload="auto"> <source src="track.mp3" type="audio/mpeg"> <source src="track.ogg" type="audio/ogg"> Your browser does not support audio playback. </audio>

Behavior:

  • starts playing automatically,
  • without sound (otherwise browsers block autoplay),
  • loops continuously,
  • loads in advance.

6. Supported formats

FormatMIME typeSupport
.mp3audio/mpegAll browsers
.oggaudio/oggFirefox, Chrome, Opera
.wavaudio/wavAll modern browsers (large size)

Summary:

Element<audio>
PurposePlay sound right in the browser
SourceVia src or several <source> tags
Main attributescontrols, autoplay, loop, muted, preload
AdvantagesNo plugins, works natively in all browsers
Supported formatsMP3, OGG, WAV

In simple terms: <audio> is a built-in player for sound. You specify the file via src or <source>, and the browser provides the playback buttons on its own: no plugins, no external services.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.