On this page10 sections
Lecture 042: Audio, Video, YouTube & Plug-ins
| Category | 7: Interactive Elements, Graphics & Media |
| Lecture | 042 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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,loopandposterattributes - Offer several file formats with
<source> - Embed YouTube videos and maps with
<iframe>
1. Video
Example
<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>| Attribute | Effect |
|---|---|
controls | Shows play, pause, volume and full-screen buttons |
poster | An image shown before the video starts |
width / height | Display size |
loop | Starts again when it ends |
muted | Starts with no sound |
autoplay | Starts 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:
<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
<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:
<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.comis 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:
<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
- Which attribute shows the play and pause buttons?
- What does the
posterattribute do? - When will browsers allow a video to autoplay?
- Why list several
<source>elements? - Which element embeds a YouTube video?
- Write an
<audio>element with controls that playssong.mp3.
Practice Answers
controls- It shows an image before the video starts playing.
- When it is also
muted. - So the browser can pick the first format it supports.
<iframe>-
<audio controls> <source src="song.mp3" type="audio/mpeg"> </audio>
Key Takeaways
<video>and<audio>play media natively; addcontrols.<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 atitle.