Lesson 18 of 24
In Progress

Creating MultiMedia Content with Audio and Video Elements

The HTML <audio> and <video> elements allow you to add multimedia content, such as audio and video files, to your web pages. These elements make it easy to play audio and video files directly in the browser, without the need for external plugins or applications.

In this article, we’ll take a closer look at how to use the <audio> and <video> elements in your HTML documents.

The <audio> Element

The <audio> element is used to embed an audio file in an HTML document. Here’s an example of how to use the <audio> element:

<audio src="audio.mp3" controls></audio>

In this example, we have used the src attribute to specify the location of the audio file, and the controls attribute to add controls to the audio player. The controls attribute allows the user to play, pause, and adjust the volume of the audio.

You can also use the <audio> element to specify multiple audio sources, in case the browser doesn’t support a particular audio format. For example:

<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>

n this example, we have provided both an MP3 and an OGG audio file as sources. If the browser doesn’t support MP3 files, it will try to play the OGG file instead. The text inside the <audio> element is displayed if the browser doesn’t support the <audio> element at all.

The <video> Element

The <video> element is used to embed a video file in an HTML document. Here’s an example of how to use the <video> element:

<video src="video.mp4" width="640" 
height="360" controls></video>

In this example, we have used the src attribute to specify the location of the video file, and the width and height attributes to specify the size of the video player. We have also used the controls attribute to add controls to the video player.

Like the <audio> element, you can use the <video> element to specify multiple video sources, in case the browser doesn’t support a particular video format. For example:

<video width="640" height="360" controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  Your browser does not support the video element.
</video>

In this example, we have provided both an MP4 and a WEBM video file as sources. If the browser doesn’t support MP4 files, it will try to play the WEBM file instead. The text inside the <video> element is displayed if the browser doesn’t support the <video> element at all.

The <source> Element

The <source> element is used inside the <audio> and <video> elements to specify multiple sources for the audio or video file. The <source> element has two attributes: src and type. The src attribute specifies the location of the audio or video file, and the type attribute specifies the MIME type of the file.

Here’s an example of how to use the <source> element:

<video width="640" height="360" controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  Your browser does not support the video element.
</video>

In this example, we have used the <source> element to specify two video sources: an MP4 file and a WEBM file. The browser will try to play the first source that it supports. If it doesn’t support any of the sources, it will display the text inside the <video> element.

The <track> Element

The <track> element is used inside the <audio> and <video> elements to specify a text track for the audio or video file. The <track> element has several attributes:

  • src: The location of the text track file
  • kind: The kind of text track (e.g. subtitles, captions, descriptions)
  • srclang: The language of the text track
  • label: The label for the text track

Here’s an example of how to use the <track> element:

<video width="640" height="360" controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.webm" type="video/webm">
  <track src="captions.vtt" kind="captions" srclang="en" label="English">
  Your browser does not support the video element.
</video>

In this example, we have used the <track> element to specify an English caption track for the video file. The caption track is a separate file in the WebVTT format. The kind attribute specifies that the track is a caption track, the srclang attribute specifies the language of the track, and the label attribute provides a label for the track.

Conclusion

The <audio> and <video> elements, along with the <source> and <track> elements, make it easy

to add multimedia content to your web pages. These elements allow you to play audio and video files directly in the browser, without the need for external plugins or applications.

Remember to always use the appropriate audio and video formats that are supported by the majority of browsers. You can use the <source> element to specify multiple sources in different formats, so that the browser can choose the one that it supports.

Additionally, you can use the <track> element to provide text tracks for audio and video files, such as subtitles or captions. This can improve the accessibility of your multimedia content for users with hearing impairments or for those who prefer to watch a video with captions.

I hope this article has helped you learn how to use the <audio>, <video>, <source>, and <track> elements in your HTML documents. With these elements, you can easily add multimedia content to your web pages and improve the user experience for your visitors.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Which HTML element is used to embed an audio file in an HTML document?

The <audio> element is used to embed an audio file in an HTML document.

Which HTML element is used to specify multiple sources for an audio or video file?

The <source> element is used to specify multiple sources for an audio or video file.

How do you add controls to an <audio> element?

You can add controls to an <audio> element by using the controls attribute. For example: <audio src="audio.mp3" controls></audio>

What is the <track> element used for?

The <track> element is used to specify a text track for an audio or video file. It can be used to provide subtitles, captions, or descriptions for the audio or video.

What is the srclang attribute used for in the <track> element?

The srclang attribute in the <track> element is used to specify the language of the text track. For example: <track src="captions.vtt" kind="captions" srclang="en" label="English">