Showing posts with label youtube. Show all posts
Showing posts with label youtube. Show all posts

Tuesday, February 25, 2014

Custom html5 video player with JavaScript

In this post we are going to create a custom html5 video player with JavaScript. at 24/2/2014 i got a mail from a reader to explain the html5 video api the funny thing is that i was knowing less about a html5 video api with javascript that why i decided to rub my hands on video api and read a article on w3school about HTML Audio/Video DOM Reference and created a custom html5 video player and now its ready to rock and roll the web
Custom html5 Video Player With JavaScript

Download Script View Demo 

Why Their is a need of a custom video player?

The problem is that every browser develop its own video player and unfortunately they are different from each other. to show same user experience around cross browser we have to create a custom video player.
Native browser video controls across different browsers

html

html is super easy u just need to wrap the <video> tag in a <div> and put some additional divs after the <video> tag to make video controls sounds little bit confusing wait i show the code
<div id="video_container">
 <video load="metadata" id="my_vid" width="550" height="320">
  <source src="http://www.w3schools.com/tags/mov_bbb.mp4" type="video/mp4"></source>
 </video>
 <div id="video_control">
  <button id="playpause">play</button><!--play pause button-->
  <input id="seekbar" class="bar" type="range" min="0" max="100" value="0" step="1"><!-- seekSlider -->
  <span id="currentTimeText">0:00</span> / <span id="durationTimeText">0:00</span><!-- video time display -->
  <button id="mute">mute</button><!-- mute button-->
  <input id="volumeBar" class="bar" type="range" min="0" max="100" value="100" step="1"><!--volume control -->
  <button id="fullScreenBtn" title="fullscreen">[  ]</button><!-- fullscreen -->
 </div>
</div><!--main video wraper-->

JavaScript

the main thing is javascript you can control the video behaviour via JavaScript api to learn more visit HTML Audio/Video DOM Reference
//create golbal variables of the video controls ======================//
 var playbtn,
   seekbar,
   vid,
   currentTimeText,
   durationTimeText,
   muteBtn,
   volumeBar,
   fullScreenBtn;

 function GetPlayerReady(){
  //set object referance of video control ======================//
  vid = document.getElementById('my_vid');
  seekbar = document.getElementById('seekbar');
  playbtn = document.getElementById('playpause');
  currentTimeText = document.getElementById('currentTimeText');
  durationTimeText = document.getElementById('durationTimeText');
  muteBtn = document.getElementById('mute');
  volumeBar = document.getElementById('volumeBar');
  fullScreenBtn = document.getElementById('fullScreenBtn');

  //add event listener ======================//
  playbtn.addEventListener('click', playPause, false);
  seekbar.addEventListener('change', vidSeek, false);
  vid.addEventListener('timeupdate', seekTimeUpdate, false);
  muteBtn.addEventListener('click', vidMute, false);
  volumeBar.addEventListener('change', changeVolume, false);
  fullScreenBtn.addEventListener('click', fullScreen, false);
 }

 window.onload = GetPlayerReady; //call the main function when document is loaded

 //play pause function ======================//
 function playPause(){
  if(vid.paused){
   vid.play();
   playbtn.innerHTML = 'pause';
  }else{
   vid.pause();
   playbtn.innerHTML = 'play';
  }
 }

 //video seek slider function ======================//
 function vidSeek(){
  var seekTo = vid.duration * (seekbar.value / 100);
  vid.currentTime = seekTo;
 }

 //video time display function ======================//
 function seekTimeUpdate(){
  var newTime = (vid.currentTime / vid.duration) * 100;
  seekbar.value = newTime;
  //currentTime & duration display//
  var curSec = Math.floor(vid.currentTime / 60);
  var curMin = Math.floor(vid.currentTime - curSec * 60);
  var durSec = Math.floor(vid.duration / 60);
  var durMin = Math.floor(vid.duration - durSec * 60);
  if(curSec < 10){
   curSec = "0"+curSec;
  }
  if(curMin < 10){
   curMin = "0"+curMin;
  }
  if(durSec < 10){
   durSec = "0"+durSec;
  }
  if(durMin < 10){
   durMin = "0"+durMin;
  }
  currentTimeText.innerHTML = curSec+":"+curMin;
  durationTimeText.innerHTML = durSec+":"+durMin;
 }

 //mute function ======================//
 function vidMute(){
  if(vid.muted){
   vid.muted = false;
   muteBtn.innerHTML = 'mute';
   volumeBar.value = 100;
  }else{
   vid.muted = true;
   muteBtn.innerHTML = 'unmute';
   volumeBar.value = 0;
  }
 }

 //volume bar function ======================//
 function changeVolume(){
  vid.volume = (volumeBar.value / 100);
 }

 //full screen fnction ======================//
 function fullScreen(){
  if(vid.requestFullScreen){
   vid.requestFullScreen();
  }else if(vid.webkitRequestFullScreen){
   vid.webkitRequestFullScreen();
  }else if(vid.mozRequestFullScreen){
   vid.mozRequestFullScreen();
  }else{
   alert('Your Browser Does Not Support Fullscreen');
  }
 }


css

we are using css to make the video control pretty. i have not focused on making the control pretty because i was not having time but in next post i will make a jQuery plugin which u can include easily
div#video_container{width: 550px;background: #000;margin:0 auto;}
 div#video_control{background: #333;padding: 10px;color: #ccc;}
 input#seekbar{width: 180px;}
 input#volumeBar{width: 80px;}
 .bar{
  -webkit-appearance:none !important;
  -moz-appearance:none !important;
  appearance: none !important;
  background: #000;
  border: #666 1px solid;
  height: 5px;
  outline: none;
 }
 .bar::-webkit-slider-thumb{
  -webkit-appearance:none !important;
  -moz-appearance:none !important;
  appearance: none !important;
  background: #fff;
  height: 15px;
  width: 15px;
  border-radius: 100%;
 }
 .bar::-webkit-slider-thumb:hover{
  cursor: pointer;
  opacity: 0.7;
 }
 .bar::-moz-slider-thumb{
  -webkit-appearance:none !important;
  -moz-appearance:none !important;
  appearance: none !important;
  background: #fff;
  height: 15px;
  width: 15px;
  border-radius: 100%;
 }
 .bar::-moz-slider-thumb:hover{
  cursor: pointer;
  opacity: 0.7;
 }

Like Us on Facebook / Add Hunk As Friend


Thursday, February 20, 2014

Embed Youtube Videos With JavaScript

Guyz i got a request from a reader to make a post for dynamic youtube video embed script so its here. Embed youtube videos is very easy the main thing is a key(id) which i will be explaining up next.
Embed Youtube Videos
Embed Youtube Videos

Download Script View Demo 

what is key(id)

in simple words it is the id and every video has is separate id. And this id make every video differnet from each other 
key

html

html has nothing interesting in it. Their is a form with a textarea and a submit button and a preview div where videos are displayed
<div id="wrapper">
  <h3>Embed youtube videos</h3>
  <form action="yt.php" method="post" id="yt_form">
   <textarea id="yt-link" name="yt" placeholder="For ex-: http://www.youtube.com/watch?v=6Ai6K2VIEXM" required></textarea>
   <input type="submit" value="upload" name="upload">
   <div id="feedback"></div>
  </form>
  <div id="preview"></div>
 </div>

CSS

body{
 background: #ccc;
}
#wrapper{
 width: 500px;
 height: 100%;
 border: 1px solid #eee;
 margin-left: auto;
 margin-right: auto;
 top: 0;
 padding: 5px;
 background: #eee;
 box-shadow: 1px 0px 3px #000;
}
#feedback{
 height: 20px;
 width: 435px;
 float: right;
 text-align: center;
 color: black;
 font-family: verdana;
}
textarea{
 width:490px;
 height:40px;
 max-width: 490px;
 height: 40px;
}

JavaScript

in JavaScript file we taking the youtube url from the textarea and matching it with the regular experation and breaking the url and getting the key from the url.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
//select the form
document.querySelector('#yt_form').addEventListener('submit', function(e){
e.preventDefault();//prevent the form from submiting
var url = e.target[0];//select the textarea
var feedback = document.querySelector('#feedback'); //select the feedback div
var parse_url = /^(https?:\/\/)?(www\.)?youtube\.com\/watch(.*?)v=([^#&$]*)/; //pattern to match the youtube url
var parts = parse_url.exec(url.value);
 if(parts){
  var key = parts[4]; //video id
  var embedUrl = '<div><iframe width="500" height="300" src="//www.youtube.com/embed/'+key+'?feature=player_detailpage" frameborder="0" allowfullscreen></iframe></div>';
  $('#preview').append(embedUrl);
 }else{
  feedback.innerHTML = 'Wrong Url';
 }
}, false);
</script>

Like Us on Facebook / Add Hunk As Friend