April 29, 2015 11:35 am

How to Make a Custom Video Player with JavaScript, Part 2

This is the second part of our tutorial How to Make a Custom Video Player with JavaScript, Part 1, in first part we show you some CSS and basics of HTML. In this part we are going to explain a MediaController object to hold the Video Player functionality so we do not pollute the global scope and finalize it and give you download code.

How to Make a Custom Video Player with JavaScript, Part I

JavaScript API

We will create a MediaController object to hold the Video Player functionality so we do not pollute the global scope.

At first, we create the object and add an initialize method which we have to call to add the player’s functionality. It accepts one parameter – the name of the class which wraps each individual video.

In it, we loop over each container with that class and call createSingleController() with the video tag that is contained within that class as an argument.
The createSingleController() method does what you might expect – creates all the functionality of the video player but only for one video.

The createSingleController() method does what you might expect – creates all the functionality of the video player but only for one video.

In it, we create a container div with the class of video-menu and add each button to this div along with its functionality.

Then, we create the volume and time controls which are a bit more complicated in that there is both a button which does nothing and a input with type set to range.

JavaScript’s volume property of videos accepts values from 0 to 1. For example, 0.5 would mean the volume is set to 50% and 0.9 to 90%. That is why we set the range to step with 0.05 (5%) and be at least 0 and at most 1.

Then, we add the controller for changing the playback time which could be re-factored to work better:

We add the time button, loop until the video is ready and if it is ready – we set the maximum value of the range input to be the duration of the video. When the video’s time progresses – we update the input appropriately. If the user puts his mouse on the input – we pause the video if it is playing and when he removes his mouse from the input – we play it but only if it was playing before he initially placed his mouse on the input. Of course, we also listen for changes in the value of the time input and set the playback time appropriately when the value changes.

Finally, we return the div and initialize the Video Player.

Conclusion

I hope you have seen that making your own Video Player is not that difficult, even without frameworks. Now, you can build your own custom video players and sell them online.

What do you think about this video player implementation, how would you make it better? What features would you add? Feel free to modify my code and post your versions of this video player in the comments below.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories: