Native Media & Embedding Video
audio, video, source and track — plus embedding YouTube with iframes.
HTML5 Media Elements
| Element | Purpose |
|---|---|
<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
- Upload your video to YouTube and obtain its video ID from the URL.
- Use an
<iframe>element — not the older<embed>or<object>tags. - Set the
srctohttps://www.youtube.com/embed/{VIDEO_ID}. - Control dimensions with
widthandheightattributes. - 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>