Creating a live video streaming page using HTML-5 and JavaScript

                                                Now a day live streaming is a very popular feature in many apps like Facebook, Instagram, and Hotstar, as well as many websites, and also provides online streaming of any live ongoing event on their site. Obviously broadcasting any live event is a tedious task and required broadcasting rights of that event, which causes you to pay a good amount to the event organizer. There are a bunch of organizations available that take the all responsibilities of broadcasting live events. But as a noob when you just try to set up things for testing purposes on your local as well as the live server then there come javascript and HTML 5 using which you can implement the live streaming page on your webpage.

You need to create only two files -

1. Your HTML page says index.html has the following code.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Home</title>
  5. <link rel="stylesheet" href="css/style.css">
  6. </head>
  7. <body>
  8.  <div class="booth">
  9.   <video id="video" width="400" height="300" autoplay></video>
  10.  </div>
  11. <script type="text/javascript" src="js/script.js"></script>
  12. </body>
  13. </html>
2. Here your self invoking Javascript code linked to an HTML file

  1. (function(){
  2. var video = document.getElementById('video'),
  3.  vendorUrl = window.URL || window.webkitURL;

  4. navigator.getMedia = navigator.getUserMedia ||
  5.  navigator.webkitGetUserMedia ||
  6.  navigator.mozGetUserMedia ||
  7.  navigator.msGetUserMedia;

  8.  //capture media

  9.  navigator.getMedia({
  10. video:true,
  11. audio:true
  12.  }, function(stream){
  13. video.src = vendorUrl.createObjectURL(stream);
  14. video.play();
  15.  },function(error){
  16. //error.code
  17.  });

  18. })();
        Now go ahead and tries to open the index.html page in any browser it will detect your webcam and asking the permission to play video. Allow browsee to play the video and you can get a live stream of your webcam on your page.

    In case you don't have a built-in webcam then you can use an external webcam or you can use your smartphone as a webcam.

I have posted a different post on "How to use your smartphone as a webcam". 

Here you can download a sample source code Download

Please do follow me on Google+ for more cool I.T. stuff

Comments

  1. Download sample source code used in tutorial from
    https://drive.google.com/open?id=1QrZT9uO0tL39rHG4oRD_mWOcMASG8zqn

    ReplyDelete

Post a Comment

Popular posts from this blog

Jasper report integration in Spring boot/Spring MVC.

FireBase Crud operation in Spring Boot

Xero Developer account setup with REST API auth flow.