Play Video From YouTube/Office 365 In Popup Dynamically

In my earlier post 'Bootstrap Modal Popup Video' , I explained how we can play static video from Youtube and play in bootstrap modal popup. But many of the time we have requirement where we render data dynamically and play in modal popup.
 
Overview
 
In this blog I am going to fetch videos from Office 365 video and consume all videos and thumbnails and bind thumbnail with HTML element and whenever any image is clicked, the corresponding video will play in modal popup. If we want to play from Youtube it can be done at the same time we just need to change the api url in AJAX call.
 
We can directly bind with the video URL to the Iframe but doing so will impact page performance, so I used Thumbnail to display video image and actual playing of video will happen in popup on click of image.
The api to make call,
 
/portals/hub/_api/VideoService/Channels('Your Channel ID')/Videos         //It provides all videos inside your channel
 

HTML 
  1. <body>  
  2.     <div id="videoContent"></div>  
  3.     <!-- Modal HTML -->  
  4.     <div id="myModal" class="modal fade">  
  5.         <div class="modal-dialog">  
  6.             <div class="modal-content">  
  7.                 <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>  
  8.                     <h4 class="modal-title">Office 365 Video</h4>  
  9.                 </div>  
  10.                 <div class="modal-body"> <iframe id="videoPopUp" autoplay width="560" height="315" frameborder="0" allowfullscreen></iframe> </div>  
  11.             </div>  
  12.         </div>  
  13.     </div>  
  14. </body>  
Scripts
  1. <script type="text/javascript">  
  2.     var VideoUrl = '';  
  3.     $(document).ready(function() {  
  4.         //Here we are getting video from only one channel we created  
  5.         var stringSource = '';  
  6.         $.ajax({  
  7.             url: "/portals/hub/_api/VideoService/Channels('Your channel ID')/Videos",  
  8.             type: "GET",  
  9.             async: false,  
  10.             headers: {  
  11.                 "accept""application/json;odata=verbose"  
  12.             },  
  13.             success: function(data) {  
  14.                 var videoInfo = data.d.results;  
  15.                 $.each(videoInfo, function(key, value) {  
  16.                     stringSource += '<a href="#myModal" id="a' + key + '" class="btn btn-lg btn-primary" data-toggle="modal"><img onClick="getVideoUrl(' + key + ')" id="img' + key + '" src="' + value.ThumbnailUrl + '" alt="' + value.Url + '" height="100" width="100"></img></a><p>Title is "' + value.Title + '"</p><p>Description is "' + value.Description + '"</p><br/>';  
  17.                 });  
  18.             },  
  19.             error: function(data) {  
  20.                 alert('Error');  
  21.             }  
  22.         }).then(function(data) {  
  23.             //alert('Success');  
  24.             $('#videoContent').append(stringSource);  
  25.         });  
  26.         $("#myModal").on('hide.bs.modal'function(e) {  
  27.             $("#videoPopUp").attr('src''');  
  28.         });  
  29.         $("#myModal").on('show.bs.modal'function(e) {  
  30.             if ((VideoUrl !== '') || (VideoUrl != 'undefined')) {  
  31.                 $("#videoPopUp").attr('src', VideoUrl);  
  32.             } else {  
  33.                 alert('No Url Defined.');  
  34.             }  
  35.         });  
  36.     });  
  37.   
  38.     function getVideoUrl(id) {  
  39.         VideoUrl = '';  
  40.         var imageId = "img" + id;  
  41.         var imgUrl = $("#" + imageId).attr('alt');  
  42.         this.VideoUrl = imgUrl;  
  43.     };  
  44. </script>   
External Reference
 
I have used url from bootstrap website, however you can embed url at your end.
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">  
  3. <!-- Optional theme -->  
  4. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">  
  5. <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>  
  6. <!-- Latest compiled and minified JavaScript -->  
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>   
Happy Coding!!! 
Ebook Download
View all
Learn
View all