Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14
Chapter 4

Native Media & Embedding Video

audio, video, source and track — plus embedding YouTube with iframes.


HTML5 Media Elements

ElementPurpose
<audio>Embeds audio with native browser controls. Supports MP3, OGG, WAV. No Flash required.
<video>Embeds video. Supports MP4, WebM, OGG. Attributes: controls, autoplay, loop, muted.
<source>Specifies multiple media resources so the browser picks its best-supported format.
<track>Adds timed text tracks (subtitles, captions, chapters) to video and audio.
<embed>Container for external applications or plug-ins (legacy) — prefer audio/video.

Video Example

<video width="640" height="368" controls poster="thumb.jpg">
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.webm" type="video/webm">
  Your browser does not support the video tag.
</video>

Audio Example

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

Use multiple <source> elements to ensure cross-browser compatibility.

Embedding YouTube Videos

  1. Upload your video to YouTube and obtain its video ID from the URL.
  2. Use an <iframe> element — not the older <embed> or <object> tags.
  3. Set the src to https://www.youtube.com/embed/{VIDEO_ID}.
  4. Control dimensions with width and height attributes.
  5. Add parameters for autoplay, loop, controls, etc.
<!-- Basic YouTube embed -->
<iframe
  src="https://www.youtube.com/embed/VIDEO_ID"
  width="560" height="315"
  title="YouTube video"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen>
</iframe>

Activity — complete the code

Complete the code correctly to mark this lesson as complete automatically.

Complete the native video element.

<video width="640" height="368" poster="thumb.jpg">
<source src="movie.mp4" type="">
<source src="movie.webm" type="video/webm">
< src="subs.vtt" kind="captions" default>
Your browser does not support the video tag.
</video>