Part 16: Embedding Audio and Video in HTML
Senior Full-Stack Developer
The <audio> tag is used to embed sound content in web pages. It supports various audio formats and provides built-in controls for playback.
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
The <video> tag is used to embed video content in web pages. It supports multiple video formats and provides comprehensive playback controls.
<video controls width="640">
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
Your browser does not support the video tag.
</video>
Built directly into modern browsers without plugins
Built-in controls and keyboard navigation
Can be styled with CSS and controlled with JavaScript
Displays built-in playback controls (play, pause, volume, etc.). Essential for user interaction with media elements.
Automatically starts playing the media when the page loads.
Often requires muted attribute for modern browsers.
Mutes the audio output. Required for autoplay in many browsers and useful for background videos.
Automatically restarts the media when it reaches the end. Perfect for background music or looping animations.
Specifies an image to show while the video is downloading or until the user plays the video.
Sets the display dimensions of the video player. Maintains aspect ratio if only one dimension is specified.
<video autoplay muted loop>
<source src="background.mp4" type="video/mp4">
</video>
<audio controls>
<source src="music.mp3" type="audio/mpeg">
</audio>
The <source> element provides fallback options for different browsers and devices
Different browsers support different media formats
Serve optimized files for different screen sizes and connections
Automatic fallback if primary format isn't supported
<video controls width="640">
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
<source src="video.ogv" type="video/ogg">
<p>Your browser doesn't support HTML5 video.</p>
</video>
MP4 (H.264)
Universal browser support
WebM
Open format, good compression
MP3
Most common audio format
Ogg Vorbis
Open audio format
Embedding an audio file with multiple format support
Audio Player Preview
Controls would appear here with actual audio file<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>
Embedding a video file with poster and controls
Video Player Preview
Video controls would appear here<video
controls
width="100%"
poster="thumbnail.jpg">
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
Your browser does not support the video tag.
</video>
Compress media files for faster loading times
Use percentage widths for responsive video players
Always provide fallback content for unsupported browsers
Use compressed media formats and optimize file sizes for faster loading
Implement lazy loading for media that's below the fold
Use preload="metadata" to only load essential data initially
Serve media files from Content Delivery Networks for better performance
Use autoplay sparingly and always with muted attribute
Show loading indicators for large media files
Test media playback on mobile devices and optimize accordingly
Serve different quality versions based on user's connection speed
Ensure your audio and video content is usable by all visitors
Provide text alternatives for audio content using WebVTT files
<track kind="captions">
Describe visual content for visually impaired users
<track kind="descriptions">
Ensure all media controls are accessible via keyboard
tabindex="0"
Always provide alternative content for unsupported browsers
<p>Fallback text</p>
You've mastered HTML Audio and Video
Embedding sound content
Embedding video content
Controls, autoplay, muted, loop
Multiple format support
Live implementation
Inclusive media practices
Build a portfolio page that showcases your work using HTML media elements:
Next: Learn about canvas element, SVG, and creating graphics with HTML