DEV Community

Cover image for HTML Tutorials: Introduction to HTML5 Audio and Video #8
Zahir Hadi Athallah
Zahir Hadi Athallah

Posted on

HTML Tutorials: Introduction to HTML5 Audio and Video #8

Introduction:

Welcome to Part 8 of our HTML tutorials! In this article, we will explore HTML5 audio and video elements, which allow us to embed and control media content directly in web pages. With HTML5 audio and video, you can enhance user experiences by providing multimedia content without relying on external plugins or players. Let's dive into the world of HTML5 audio and video!

HTML5 Audio Element:

The <audio> element in HTML5 allows us to embed audio content in a web page. We can specify the audio file source using the src attribute and provide alternative content within the element for browsers that do not support the audio element.

Example:

<audio controls>
    <source src="audio/music.mp3" type="audio/mpeg">
    Your browser does not support the audio element.
</audio>
Enter fullscreen mode Exit fullscreen mode

In the example above, we have added an audio element with the controls attribute, which displays the default audio controls (play, pause, volume) to the user. We have also provided an audio source in the MP3 format, along with alternative content for unsupported browsers.

HTML5 Video Element:

Similarly, the <video> element in HTML5 allows us to embed video content in a web page. We can specify the video file source using the src attribute and provide alternative content within the element for browsers that do not support the video element.

Example:

<video controls>
    <source src="video/video.mp4" type="video/mp4">
    Your browser does not support the video element.
</video>
Enter fullscreen mode Exit fullscreen mode

In the example above, we have added a video element with the controls attribute, enabling the default video controls for the user. We have specified a video source in the MP4 format and provided alternative content for unsupported browsers.

Controlling Audio and Video Playback:

HTML5 audio and video elements come with built-in controls that allow users to play, pause, seek, adjust volume, and more. However, we can also control media playback programmatically using JavaScript.

Example:

var audio = document.getElementById("myAudio");
var video = document.getElementById("myVideo");

function playMedia() {
    audio.play();
    video.play();
}

function pauseMedia() {
    audio.pause();
    video.pause();
}

function seekMedia(seconds) {
    audio.currentTime += seconds;
    video.currentTime += seconds;
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we have obtained references to the audio and video elements using JavaScript. We then define functions to play, pause, and seek the media by adjusting the currentTime property.

Styling Audio and Video Elements:
HTML5 audio and video elements can be styled using CSS to match the design of your web page. You can target the <audio> and <video> elements or their parent containers to apply custom styles.

Example:

audio {
    width: 100%;
}

video {
    width: 100%;
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we use CSS to set the width of the audio and video elements to 100%, making them responsive and fill the available space.

Closing:

HTML5 audio and video elements provide a seamless way to embed multimedia content directly in web pages. In this tutorial, we explored the basics of using <audio> and <video> elements, controlling media playback with JavaScript, and styling the elements using CSS. By harnessing the power of HTML5 audio and video, you can create engaging and interactive multimedia experiences for your users. Happy coding!

Top comments (0)