Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Using <audio> and <video> Tags


HTML provides the <audio> and <video> tags for embedding multimedia content directly into web pages. These tags support various media formats and offer attributes to control playback, display, and accessibility. This article demonstrates how to use these tags with examples.

1. The <audio> Tag

The <audio> tag allows you to embed audio files in HTML documents. Common audio formats include .mp3, .wav, and .ogg. The <audio> tag also includes several useful attributes, such as:

Example of Using <audio> Tag:

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

In this example, the <audio> tag includes two <source> tags for different audio formats. If the browser doesn't support one format, it will try the other.

2. The <video> Tag

The <video> tag is used to embed video files in HTML documents. Common video formats include .mp4, .webm, and .ogg. Similar to the <audio> tag, the <video> tag supports several attributes:

Example of Using <video> Tag:

    <video controls width="600" poster="poster.jpg">
        <source src="videofile.mp4" type="video/mp4">
        <source src="videofile.ogg" type="video/ogg">
        Your browser does not support the video element.
    </video>
        

In this example, the controls attribute enables video controls, and the poster attribute specifies an image to display before the video plays. The browser will use videofile.mp4 or videofile.ogg based on its compatibility.

3. Autoplay and Loop

Both <audio> and <video> tags support the autoplay and loop attributes. The autoplay attribute starts playing the media automatically, while the loop attribute allows the media to play continuously.

Example of Autoplay and Loop:

    <audio autoplay loop>
        <source src="background-music.mp3" type="audio/mpeg">
        Your browser does not support the audio element.
    </audio>

    <video autoplay loop muted>
        <source src="background-video.mp4" type="video/mp4">
        Your browser does not support the video element.
    </video>
        

Here, the audio will play in a loop continuously. For the video, autoplay starts playback, loop repeats it, and muted prevents audio from playing on page load (a common requirement for autoplaying videos).

4. Complete Example of Audio and Video Tags

Below is an example that uses both the <audio> and <video> tags within the same page:

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

    <h2>Video Example</h2>
    <video controls width="600" poster="thumbnail.jpg">
        <source src="movie.mp4" type="video/mp4">
        <source src="movie.webm" type="video/webm">
        Your browser does not support the video element.
    </video>
        

Conclusion

The <audio> and <video> tags in HTML make it easy to embed multimedia content on a webpage. By using attributes like controls, autoplay, loop, and muted, you can control how the media behaves, enhancing the user experience.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java