On this page10 sections
  1. Learning Objectives
  2. 1. Video
  3. 2. Several Formats With <source>
  4. 3. Audio
  5. 4. Embedding With <iframe>
  6. 5. Plug-ins: <object> and <embed>
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 042: Audio, Video, YouTube & Plug-ins

Category7: Interactive Elements, Graphics & Media
Lecture042 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

Modern browsers play sound and video with no plug-ins. Two elements, <audio> and <video>, do it, and <iframe> places another page, such as a YouTube video or a map, inside yours.


Learning Objectives

  • Play sound with <audio> and video with <video>
  • Use the controls, autoplay, muted, loop and poster attributes
  • Offer several file formats with <source>
  • Embed YouTube videos and maps with <iframe>

1. Video

Example

html
<video controls width="360" poster="https://picsum.photos/id/1011/360/200">
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4" type="video/mp4">
  Your browser cannot play this video.
</video>
AttributeEffect
controlsShows play, pause, volume and full-screen buttons
posterAn image shown before the video starts
width / heightDisplay size
loopStarts again when it ends
mutedStarts with no sound
autoplayStarts by itself. Browsers only allow this when muted is also set

The text inside <video> only appears in very old browsers that can't play video.


2. Several Formats With <source>

List more than one file and the browser plays the first one it supports:

html
<video controls width="360">
  <source src="movie.webm" type="video/webm">
  <source src="movie.mp4" type="video/mp4">
</video>

MP4 works almost everywhere, so it's the safest single choice.


3. Audio

Example

html
<audio controls>
  <source src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3" type="audio/mpeg">
  Your browser cannot play this audio.
</audio>

<audio> takes the same attributes as <video>, apart from the size and poster ones.


4. Embedding With <iframe>

An <iframe> shows another web page inside a box on your page. Sites such as YouTube and Google Maps give you a ready-made iframe under Share → Embed.

Example: a YouTube video:

html
<iframe width="360" height="203"
  src="https://www.youtube-nocookie.com/embed/NrO0CJCbYLA"
  title="Introduction to Dart"
  allowfullscreen></iframe>
  • Always add a title, so screen readers can say what the frame contains.
  • youtube-nocookie.com is YouTube's privacy-friendly embed address.

5. Plug-ins: <object> and <embed>

Years ago, browsers needed plug-ins such as Flash and Java applets to play videos and games. Those plug-ins are gone now, and browsers no longer support them. Use <video>, <audio> and <iframe> instead.

Two old elements are still sometimes used to show another file, most often a PDF:

html
<object data="timetable.pdf" type="application/pdf" width="400" height="300">
  <a href="timetable.pdf">Download the timetable (PDF)</a>
</object>

<embed src="timetable.pdf" type="application/pdf" width="400" height="300">

The content inside <object> is a fallback for browsers that can't show the file, so always include a download link.


Common Beginner Mistakes

1. Autoplay with sound. Browsers block it, and visitors dislike it. If you must autoplay, add muted.

2. Huge video files in your own folder. Long videos load slowly and use a lot of bandwidth. Host them on YouTube or Vimeo and embed them.

3. No controls. Without them, visitors can't pause or change the volume.

4. Iframes without title. Screen-reader users hear only "frame".


Practice Questions

  1. Which attribute shows the play and pause buttons?
  2. What does the poster attribute do?
  3. When will browsers allow a video to autoplay?
  4. Why list several <source> elements?
  5. Which element embeds a YouTube video?
  6. Write an <audio> element with controls that plays song.mp3.

Practice Answers

  1. controls
  2. It shows an image before the video starts playing.
  3. When it is also muted.
  4. So the browser can pick the first format it supports.
  5. <iframe>
  6. html
    <audio controls>
      <source src="song.mp3" type="audio/mpeg">
    </audio>

Key Takeaways

  • <video> and <audio> play media natively; add controls.
  • <source> offers alternative formats; MP4 / MP3 are the safe choices.
  • Autoplay only works muted, and should be rare.
  • <iframe> embeds other pages (YouTube, maps); always give it a title.