HTML Video DOM Methods šŸŽÆ

beginner
17 min

HTML Video DOM Methods šŸŽÆ

Welcome back to CodeYourCraft! Today, we're diving into the exciting world of HTML Video DOM Methods. Let's get started! šŸ“

Understanding HTML Video Element šŸ“

Before we dive into the methods, let's familiarize ourselves with the HTML <video> element. This tag allows us to embed video content in our web pages.

html
<video width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> Your browser does not support the video tag. </video>

šŸ’” Pro Tip: The <source> tag allows us to provide multiple video sources with different formats. This ensures that the video is playable across various browsers.

Interacting with Video Element using DOM šŸ’”

Now that we have our video embedded, let's learn how to interact with it using JavaScript (DOM) methods. Here are some essential methods:

1. video.play() šŸŽ¬

This method starts playing the video.

javascript
// Get the video element let video = document.getElementById('myVideo'); // Play the video video.play();

2. video.pause() šŸ›‘

This method pauses the currently playing video.

javascript
// Pause the video video.pause();

3. video.currentTime ā±

This property represents the current playback position of the video.

javascript
// Get the current playback position (in seconds) let currentTime = video.currentTime;

4. video.duration šŸ•’

This property represents the total duration of the video.

javascript
// Get the total duration of the video (in seconds) let duration = video.duration;

5. video.volume šŸ”Š

This property represents the volume level of the video.

javascript
// Change the volume level (between 0 and 1) video.volume = 0.5;

6. video.muted 🤫

This property determines whether the audio for the video is muted or not.

javascript
// Mute the video video.muted = true;

Quiz Time šŸŽ“

Quick Quiz
Question 1 of 1

Which method stops the currently playing video?

Practical Application šŸ› ļø

Let's create a simple HTML page with a video player that allows us to play, pause, seek, and change the volume.

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HTML Video DOM Methods</title> </head> <body> <video id="myVideo" width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> Your browser does not support the video tag. </video> <div id="controls"> <button onclick="playPause()">Play/Pause</button> <button onclick="seek(10)">Seek to 10 seconds</button> <button onclick="changeVolume(0.2)">Lower Volume</button> </div> <script> function playPause() { let video = document.getElementById('myVideo'); if (video.paused) { video.play(); } else { video.pause(); } } function seek(seconds) { let video = document.getElementById('myVideo'); video.currentTime = seconds; } function changeVolume(level) { let video = document.getElementById('myVideo'); video.volume = level; } </script> </body> </html>

šŸ’” Pro Tip: In the example above, we have created a simple control panel for our video player. The playPause() function toggles between playing and pausing the video, the seek(seconds) function allows us to seek to a specific point in the video, and the changeVolume(level) function adjusts the volume level.

That's it for today's lesson! We hope you enjoyed learning about HTML Video DOM Methods. Stay tuned for more exciting tutorials at CodeYourCraft! šŸŽ“šŸš€