Courses
Help
We can link audio and video in two ways:
- Using
<a>(anchor tag) ā provides a simple download/play link. - Using special HTML5 tags (
<audio>and<video>) ā embeds media directly inside the webpage.
š 1. Linking Audio/Video with <a> Tag
š With the anchor tag, you only link to the file. The browser will either download it or open it in the default media player.
Example:
<!-- Audio Link -->
<a href="song.mp3">Play/Download Audio</a><br />
<!-- Video Link -->
<a href="movie.mp4">Play/Download Video</a>
- Clicking these will either play (if the browser supports the file) or download the media.
šµ 2. Embedding Audio with <audio> Tag (HTML5)
š HTML5 introduced the <audio> tag to directly play audio inside the webpage with controls.
Basic Syntax:
<audio src="song.mp3" controls></audio>
controlsā shows play, pause, volume, etc.autoplayā starts playing automatically (not always recommended).loopā repeats the audio.
Example with multiple formats (better browser support):
<audio controls>
<source src="song.mp3" type="audio/mpeg" />
<source src="song.ogg" type="audio/ogg" />
Your browser does not support the audio tag.
</audio>
š„ 3. Embedding Video with <video> Tag (HTML5)
š The <video> tag allows you to embed video directly in the webpage.
Basic Syntax:
<video src="movie.mp4" controls width="400" height="300"></video>
controlsā adds play, pause, fullscreen, etc.autoplayā video starts automatically.loopā video repeats.mutedā starts muted.poster="image.jpg"ā thumbnail before video plays.
Example with multiple formats:
<video controls width="500">
<source src="movie.mp4" type="video/mp4" />
<source src="movie.ogg" type="video/ogg" />
Your browser does not support the video tag.
</video>
āļø Comparison: <a> vs <audio>/<video>
| Feature | <a> Tag | <audio> / <video> Tag |
|---|---|---|
| Purpose | Just provides a link to open/download the file | Embeds media directly inside webpage |
| Controls | No built-in controls | Built-in play/pause/volume/fullscreen |
| User Experience | Opens in a new tab or downloads | Plays directly in the browser |
| Example | <a href="song.mp3">Play Audio</a> | <audio src="song.mp3" controls></audio> |
ā Complete Example
<!DOCTYPE html>
<html>
<head>
<title>Audio and Video Example</title>
</head>
<body>
<h2>Using Anchor Tag</h2>
<a href="song.mp3">Download/Play Song</a><br />
<a href="movie.mp4">Download/Play Movie</a>
<h2>Using HTML5 Tags</h2>
<!-- Audio -->
<audio controls>
<source src="song.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
<!-- Video -->
<video controls width="400" poster="poster.jpg">
<source src="movie.mp4" type="video/mp4" />
Your browser does not support the video element.
</video>
</body>
</html>
š In summary:
- Use
<a>when you just want to provide a download/play link. - Use
<audio>and<video>when you want media to play directly inside the webpage.