DEV Community

Sh Raj
Sh Raj

Posted on

Adding custom video player to website

Adding a custom video player to your website can enhance user experience, improve branding, and provide more control over video playback features. Here's a step-by-step guide to help you create and integrate a custom video player into your website.

Step 1: Choose the Right Tools

  1. HTML5 Video: The HTML5 <video> element is a great starting point for embedding videos.
  2. JavaScript Libraries: Consider using libraries like Video.js, Plyr, or custom JavaScript to add advanced functionalities.
  3. CSS: Custom styles to match your website’s design.

Step 2: Basic HTML5 Video Embedding

Start by embedding a simple HTML5 video player on your webpage.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Video Player</title>
    <style>
        /* Basic styling for the video player */
        video {
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
    <video controls>
        <source src="path/to/your/video.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 3: Add JavaScript for Enhanced Functionality

To add more control and customization, use JavaScript. Below is an example using a basic custom control setup.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Video Player</title>
    <style>
        /* Basic styling for the video player */
        video {
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
            display: block;
        }

        /* Custom controls styling */
        .controls {
            display: flex;
            justify-content: center;
            margin-top: 10px;
        }

        .controls button {
            margin: 0 5px;
            padding: 10px;
        }
    </style>
</head>
<body>
    <video id="myVideo">
        <source src="path/to/your/video.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
    <div class="controls">
        <button id="playPause">Play/Pause</button>
        <button id="stop">Stop</button>
        <button id="mute">Mute/Unmute</button>
    </div>

    <script>
        const video = document.getElementById('myVideo');
        const playPauseButton = document.getElementById('playPause');
        const stopButton = document.getElementById('stop');
        const muteButton = document.getElementById('mute');

        playPauseButton.addEventListener('click', () => {
            if (video.paused) {
                video.play();
                playPauseButton.textContent = 'Pause';
            } else {
                video.pause();
                playPauseButton.textContent = 'Play';
            }
        });

        stopButton.addEventListener('click', () => {
            video.pause();
            video.currentTime = 0;
            playPauseButton.textContent = 'Play';
        });

        muteButton.addEventListener('click', () => {
            video.muted = !video.muted;
            muteButton.textContent = video.muted ? 'Unmute' : 'Mute';
        });
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 4: Advanced Customization with Video.js

Video.js is a popular open-source library that simplifies the process of creating custom video players with advanced features.

  1. Include Video.js: Add the Video.js CSS and JavaScript files to your project.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Video Player with Video.js</title>
    <link href="https://vjs.zencdn.net/7.14.3/video-js.css" rel="stylesheet" />
</head>
<body>
    <video id="my-video" class="video-js" controls preload="auto" width="640" height="264"
        poster="path/to/your/poster.jpg" data-setup="{}">
        <source src="path/to/your/video.mp4" type="video/mp4" />
        <p class="vjs-no-js">
            To view this video please enable JavaScript, and consider upgrading to a
            web browser that
            <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
        </p>
    </video>

    <script src="https://vjs.zencdn.net/7.14.3/video.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
  1. Customizing Video.js: Video.js allows for extensive customization through options and plugins.
<script>
    var player = videojs('my-video', {
        controls: true,
        autoplay: false,
        preload: 'auto',
        // More options here
    });

    // Example of adding custom button
    videojs.registerComponent('MyButton', videojs.extend(videojs.getComponent('Button'), {
        constructor: function () {
            videojs.getComponent('Button').apply(this, arguments);
            this.addClass('vjs-icon-fullscreen-enter');
            this.controlText('My Button');
        },
        handleClick: function () {
            alert('My Button Clicked!');
        }
    }));

    player.getChild('controlBar').addChild('MyButton', {});
</script>
Enter fullscreen mode Exit fullscreen mode

Step 5: CSS Customization

Customize the appearance of your video player to match your website’s design. Below is an example of how you can style Video.js.

/* Example of custom Video.js styles */
.video-js .vjs-control-bar {
    background-color: rgba(0, 0, 0, 0.5);
}

.video-js .vjs-big-play-button {
    background-color: rgba(0, 0, 0, 0.5);
    border: none;
}

.video-js .vjs-icon-placeholder:before {
    color: #fff;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Adding a custom video player to your website can significantly enhance the user experience. By starting with basic HTML5 and CSS, and then integrating JavaScript or a library like Video.js, you can create a robust and attractive video player tailored to your specific needs. Remember to test the video player across different browsers and devices to ensure compatibility and performance.

Bonus Use SopPlayer

To integrate the Sopplayer video player into your website, follow these steps:

Step 1: HTML Setup

Add the class="sopplayer" and data-setup="{}" attributes to your <video> tag. This initializes the player with default settings.

<video id="my-video" class="sopplayer" controls preload="auto" data-setup="{}" width="500px">
  <source src="https://cdn.jsdelivr.net/gh/SH20RAJ/Sopplayer@main/sample.mp4" type="video/mp4" />
</video>
Enter fullscreen mode Exit fullscreen mode

Step 2: Adding CSS

Include the Sopplayer CSS file in the <head> section of your HTML to style the video player.

<link href="https://cdn.jsdelivr.net/gh/SH20RAJ/Sopplayer/sopplayer.min.css" rel="stylesheet" />
Enter fullscreen mode Exit fullscreen mode

Step 3: Adding JavaScript

Include the Sopplayer JavaScript file before the closing </body> tag to enable the player’s functionality.

<script src="https://cdn.jsdelivr.net/gh/SH20RAJ/Sopplayer/sopplayer.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

Full HTML Example

Here is a complete example of an HTML file integrating Sopplayer:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/gh/SH20RAJ/Sopplayer/sopplayer.min.css" rel="stylesheet" />
</head>
<body>
  <center>
    <video id="my-video" class="sopplayer" controls preload="auto" data-setup="{}" width="500px">
      <source src="sample.mp4" type="video/mp4" />
    </video>
  </center>
  <script src="https://cdn.jsdelivr.net/gh/SH20RAJ/Sopplayer/sopplayer.min.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Sopplayer offers a sleek interface, cross-platform compatibility, and customization options, making it a great choice for enhancing the video playback experience on your website【11†source】【12†source】【13†source】.

For more detailed documentation and updates, you can visit the Sopplayer GitHub page or check out the Sopplayer homepage.

Top comments (0)