The HTML5 <video> element enables native video playback directly in the browser without relying on plugins. It supports a set of JavaScript APIs that allow developers to programmatically control video behavior such as play, pause, seek, volume, and track selection. Built-in controls can be used for quick implementation, while custom controls offer greater flexibility and UI integration.

The API also exposes video metadata, events, and track management options for subtitles and captions. Developers can listen to playback events to update the interface or trigger application logic. This functionality is essential for building responsive, interactive video experiences on the web.

HTML5 Video Controls

The <video> element includes a controls attribute that renders default playback controls (play/pause, seek bar, volume). These built-in controls are useful for basic use cases, but for custom functionality or styling, developers often override these controls with JavaScript-based interfaces.

Built-in Video Controls

By adding the controls attribute to the <video> tag, you enable the browser"s default video controls:

code
<video controls>
<source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

This automatically adds UI elements like play/pause buttons, volume control, and a seek bar. While convenient, developers often need more flexibility, which can be achieved via the Video API.

HTML5 Video Player

Custom Video Controls with JavaScript

You can implement custom controls by adjusting the video"s playback properties via JavaScript. For example, controlling playback, volume, and seeking:

code
<video id="myVideo" width="640" height="360">
<source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

<button onclick="playPause()">Play/Pause</button>
<button onclick="setVolume(0.5)">Set Volume to 50%</button>
<button onclick="seekTo(30)">Seek to 30s</button>

<script>
var video = document.getElementById('myVideo');

function playPause() {
if (video.paused) {
video.play();
} else {
video.pause();
}
}

function setVolume(level) {
video.volume = level;
}

function seekTo(time) {
video.currentTime = time;
}
</script>

Explanation:

  • playPause(): Toggles the play/pause state.
  • setVolume(): Adjusts the video"s volume (between 0.0 and 1.0).
  • seekTo(): Moves the playback to a specified time (in seconds).

Managing Video Tracks: Subtitles and Captions

HTML5 <video> supports multiple text tracks, such as subtitles, captions, and chapters, using the <track> element. Tracks can be displayed automatically or controlled via JavaScript.

Adding and Controlling Tracks

Each <track> tag defines a single timed text track. The browser may expose track selection options in the UI if multiple tracks are provided.

code
<video id="myVideo" controls>
<source src="video.mp4" type="video/mp4">
<track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English">
<track src="subtitles_es.vtt" kind="subtitles" srclang="es" label="Spanish">
</video>

Explanation:

  • kind="subtitles": Specifies the type of track (subtitles, captions, chapters).
  • srclang="en": Defines the language of the track (English in this case).
  • label="English": A label that the player can display to the user.

JavaScript Control of Tracks

You can enable, disable, or change tracks dynamically with JavaScript:

code
var video = document.getElementById('myVideo');
var track = video.textTracks[0]; // Access first subtitle track

// Enable the first subtitle track
track.mode = 'showing';

// Disable all tracks
for (var i = 0; i < video.textTracks.length; i++) {
video.textTracks[i].mode = 'disabled';
}

Explanation:

  • track.mode = 'showing': Displays the subtitles for the selected track.
  • track.mode = 'disabled': Disables the track (i.e., stops displaying subtitles).

Handling Video Events

HTML5 Video API provides several events to handle user interactions and video playback states. These events can be useful for custom logic, such as updating UI elements or performing actions when the video reaches a certain state.

Common Video Events

Here are some common events associated with the <video> element:

Event Trigger
play When playback starts
pause When playback is paused
ended When media has finished playing
timeupdate As currentTime is updated during play
volumechange When volume or mute state changes
seeking When the seek operation begins
seeked When the seek operation completes

Example of Handling Video Events

code
<video id="myVideo" width="640" height="360" controls>
<source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

<script>
var video = document.getElementById('myVideo');

video.addEventListener('play', function() {
console.log('Video is playing');
});

video.addEventListener('pause', function() {
console.log('Video is paused');
});

video.addEventListener('ended', function() {
console.log('Video has ended');
});

video.addEventListener('timeupdate', function() {
console.log('Current time: ' + video.currentTime);
});

video.addEventListener('volumechange', function() {
console.log('Volume level: ' + video.volume);
});
</script>

Explanation:

  • The play, pause, and ended events log messages when the video enters these states.
  • timeupdate logs the current playback time during playback.
  • volumechange logs changes in the volume level.