HTML · Chapter 40 of 45

HTML Audio and Video

The <audio> and <video> tags natively embed media without needing third-party plugins like Flash. Both support the controls attribute to show built-in play/pause/volume UI.

Inside <audio> or <video>, multiple <source> tags can provide different file formats, letting the browser choose the one it supports, with fallback text shown for very old browsers.

Syntax
<video controls><source src="movie.mp4" type="video/mp4"></video>

Basic usage

controls shows the native player UI. autoplay starts playback automatically (often blocked by browsers unless muted). loop repeats playback.

Multiple sources

Provide several <source> elements with different formats (like .mp4 and .webm) since browser codec support varies; the browser picks the first one it can play.

Example 1 (html)
<audio controls>
  <source src="song.mp3" type="audio/mpeg">
  Your browser does not support audio.
</audio>
Output
(shows an audio player with play/pause controls)

The fallback text only shows in browsers that don't support the audio tag at all.

Example 2 (html)
<video width="320" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.webm" type="video/webm">
</video>
Output
(shows a video player, picking a supported format)

Multiple sources maximize cross-browser compatibility.

Key points

  • <audio> and <video> embed media without plugins.
  • controls shows the browser's built-in playback UI.
  • Multiple <source> tags provide format fallbacks.
  • autoplay is often blocked unless the media is also muted.
💡 Note: Always provide captions/subtitles via <track> for video accessibility.

📝 Quick Quiz

1. Which attribute shows native play/pause controls?

2. Why use multiple <source> tags?

3. What is often required alongside autoplay for it to work?