Logo
  Sunday, May 20, 2012
Sign-In  |  Sign-Up  |  Contact Us  | Bookmark |  RSS Feed

HTML5 Tutorial FeedBurner

Custom HTML5 Video Controller  
HTML5 video controller allows user to interact with your embedded video just like YouTube video player allows you to start, pause and end video playing.

HTML5 video controller allows you to add start, pause and end video buttons. However, these buttons are very plain and may not feet overall design of your HTML5 web page.

Thankfully, HTML5 goes extra mile by allowing us to control the appearance of these buttons on the form.

First, let us look at the regular video tag that allows us view video file on HTML5 compatible browser.

<video width=400" height="200" src="myVideoFile.mp4" controls />

Browsers render this video tag differently based on the browser type and version. We can override this undesired behavior and make sure that our video control is rendered the same on all browsers. Let’s see how we can use HTML5 to accomplish this task.

First, you need to use HTML5 media attributes and DOM events. For example, there several important attributes that we’ll need while building custom video controller.

  • media.paused
  • media.defaultPlaybackRate
  • Media. playbackRate
  • media.ended
  • Media.play()
  • Media.volume
  • Media.muted
  • Media.currentTime
These attributes are all very important for our JavaScript code that will use them to modify the way date and time control looks and behaves.

Example of this JavaScript code is below:

<script>

if (video.paused)

{

      video.play();

}

      else if (video.ended)

{

video.currentTime=10; //Sets position from which to stat video playing.

 

video.play();

 

</script>

Here is complete example in HTML5 and JavaScript and you can use as your boilerplate for coding your own custom video control.

<!DOCTYPE html>

<html>

<head>

    <title>Custom HTML5 my_video Control </title> 

    <script type="text/javascript"> 

var my_video; 

window.onload = function()

{ 

    my_video = document.getElementsByTagName("video")[0]; 

    var btnPlay = document.getElementById("btnPlay");

    var btnPause = document.getElementById("btnPause");

    var btnMute = document.getElementById("btnMute");

    var btn_btnVolume = document.getElementById('btnVolume'); 

    btnPlay.addEventListener('click', doVideoPlay, false);

    btnPause.addEventListener('click', doVideoPause, false);

    btnMute.addEventListener('click', doVideoMute, false);

    btn_btnVolume.value = my_video.btnVolume;

 

    btn_btnVolume.addEventListener('change',function(e)

    { 

    myVol= e.target.value; 

    my_video.btnVolume=myVol;

    if (myVol==0)

    {

       my_video.muted = true;            

    }

    else

    { 

       my_video.muted = false;

    }

            return false;

    }, true);

};

 

function doVideoPlay()

{

    if (video.paused)

    {

        my_video.play();

    }

    else if (video.ended)

    {

        my_video.currentTime=0;

        my_video.play();

    };

};

 

function doVideoPause()

{

    if (video.play)

    {         

      my_video.pause();

    };

};

 

function doVideoMute()

{

    document.getElementById('btnVolume').value = 0;

    my_video.muted = true;

}; 

    </script> 

</head>

<body>

        <video width="400" height="200" src="myVideoAudioFile.mp4" />

        <button id="btnPlay">Play</button>

        <button id="btnPause">Pause</button>

        <input type="range" min="0" max="2" step="0.2" id="btnVolume">

        <button id="btnMute">Mute</button>

</body>

</html>

Download HTML5 Video Controller sample code: Download HTML5 Video Controller Code

Print Custom HTML5 Video Controller Bookmark Custom HTML5 Video Controller

Related Articles  
Opening HTML5 Video in Full Screen Mode
These days videos come in very good quality and someof the paying sites provide HD quality videos as well. So, it is ...
HTML5 Baseline Codec Media Element
HTML5 baseline codec is a video and audio formats supported by all browsers. As a result, we can rely on this media ...
HTML5 Video Conferencing
The HTML5 WebSockets allows HTML5 video conferencing and to share data through our web server with other user browsers. ...
HTML5 Video Sample Code
In order to create HTML5 video page, we need to create video file itself. There are many different video file formats ...
HTML5 Video Browser Support
There are several major browsers that provide support for HTML5 video codec formats. However, this support is not ...
HTML5 Video Tag
HTML5 video tag is used to provide video content linkage within your HTML5 webpage. HTML5 video tag has "src" attribute ...
More