Integrating MediaElement.js - Custom HTML5 Video Player

Sh Raj - Jul 26 - - Dev Community

Integrating MediaElement.js into a Custom HTML5 Video Player

Introduction

MediaElement.js is a powerful JavaScript library that allows developers to create a consistent and feature-rich video and audio player across different browsers and devices. By integrating MediaElement.js into your HTML5 video player, you can take advantage of its extensive customization options and plugins to enhance the media playback experience.

Steps to Integrate MediaElement.js

1. Include MediaElement.js Library

First, you need to include the MediaElement.js CSS and JavaScript files in your HTML document. You can either download these files or use a CDN (Content Delivery Network) to link them directly.

Using CDN

Add the following lines to the <head> section of your HTML document:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.16/mediaelementplayer.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.16/mediaelement-and-player.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

2. Create Your HTML5 Video Player

Next, define your HTML5 video element with the necessary attributes. The class="mejs__player" attribute is used to apply MediaElement.js styles to your video tag.

<video id="player1" width="640" height="360" controls class="mejs__player">
    <source src="path/to/your/video.mp4" type="video/mp4">
    <!-- You can add more sources for different formats here -->
</video>
Enter fullscreen mode Exit fullscreen mode
  • id: A unique identifier for your video element.
  • width and height: Set the dimensions of the video player.
  • controls: Enables the default browser controls for the video player.
  • class="mejs__player": Applies MediaElement.js styles to the player.

3. Initialize MediaElement.js

To initialize MediaElement.js on your video element, use JavaScript. This code should be placed just before the closing </body> tag.

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var player = new MediaElementPlayer('player1', {
            // Options
            features: ['playpause', 'current', 'progress', 'duration', 'volume', 'fullscreen'],
            success: function(mediaElement, originalNode, instance) {
                // Your code here
            },
            error: function() {
                // Handle error here
            }
        });
    });
</script>
Enter fullscreen mode Exit fullscreen mode
  • features: An array specifying the control elements to be displayed (e.g., play/pause button, current time, progress bar, duration, volume control, fullscreen button).
  • success: A callback function executed after the player is successfully initialized.
  • error: A callback function executed if there is an error initializing the player.

Complete Example

Here is a complete example of integrating MediaElement.js into an HTML5 video player:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom HTML5 Video Player with MediaElement.js</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.16/mediaelementplayer.min.css" />
</head>
<body>
    <video id="player1" width="640" height="360" controls class="mejs__player">
        <source src="path/to/your/video.mp4" type="video/mp4">
        <!-- You can add more sources for different formats here -->
    </video>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/mediaelement/4.2.16/mediaelement-and-player.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var player = new MediaElementPlayer('player1', {
                // Options
                features: ['playpause', 'current', 'progress', 'duration', 'volume', 'fullscreen'],
                success: function(mediaElement, originalNode, instance) {
                    // Your code here
                },
                error: function() {
                    // Handle error here
                }
            });
        });
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Customizing the Player

MediaElement.js provides extensive customization options to tailor the player to your needs.

Custom Features

You can customize the control elements displayed on the player by modifying the features array:

features: ['playpause', 'current', 'progress', 'duration', 'volume', 'fullscreen', 'tracks', 'speed', 'download']
Enter fullscreen mode Exit fullscreen mode

Skinning the Player

MediaElement.js allows you to apply custom skins to your player. You can create a custom CSS file to override the default styles. Add your custom CSS file after the MediaElement.js CSS file:

<link rel="stylesheet" href="path/to/your/custom-skin.css" />
Enter fullscreen mode Exit fullscreen mode

Plugins and Extensions

MediaElement.js supports various plugins and extensions to enhance functionality. You can add plugins for subtitles, quality selection, and more. Refer to the MediaElement.js documentation for a complete list of available plugins and how to integrate them.

Conclusion

Integrating MediaElement.js into your custom HTML5 video player provides a consistent and feature-rich media playback experience across different browsers and devices. With its extensive customization options and support for plugins, you can create a video player that meets your specific requirements. This article provided a step-by-step guide to integrating MediaElement.js, along with examples and customization tips to help you get started.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .