Create your Video Playlist Using Kendo Media Player And List View

Introduction

In this article, I’ll describe how to create a playlist using Kendo Media Player, using ASP.NET Web API and Entity framework. The application is developed, using Entity framework "Database First" approach and ASP.NET Web API.

Prerequisites

Basic knowledge of ASP.NET MVC, Entity framework, and Kendo UI framework.

Create a Table

For demo purposes, I am going to create one table in MS SQL database. In my case, I named it Media. The script, given below, is used to create the tables with some records.

  1. CREATE TABLE Media  
  2. (  
  3.     VideoId INT IDENTITY(1,1),  
  4.        title VARCHAR(max),  
  5.     poster VARCHAR(max),  
  6.     source VARCHAR(max)  
  7. )  
  8. GO  
  9. INSERT INTO Media VALUES('Telerik Platform - Enterprise Mobility. Unshackled.','http://img.youtube.com/vi/N3P6MyvL-t4/1.jpg','https://www.youtube.com/watch?v=N3P6MyvL-t4')  
  10. INSERT INTO Media VALUES('Learn How York Solved Its Database Problem','http://img.youtube.com/vi/_S63eCewxRg/1.jpg','https://www.youtube.com/watch?v=_S63eCewxRg')  
  11. Insert INTO Media VALUES('Responsive Website Delivers for Reeves Import Motorcars''http://img.youtube.com/vi/DYsiJRmIQZw/1.jpg','https://www.youtube.com/watch?v=DYsiJRmIQZw')  
  12. GO  

The records in the table as shown in below figure 1.

  

Create ASP.NET WEB API application

Create a new empty ASP.NET WEB API Application, as per the figures given below.

Open Visual Studio -> File -> New Project -> ASP.NET Web Application.



Select Web API and click on OK.



Generate the Model

Now, we will create Entity framework models from the database tables.

Step 1

Right-click the Models folder, select Add -> ADO.NET Entity Data Model, or select Add->New Item in the "Add New Item" window.

Select data in the left pane and ADO.NET Entity Data Model from the center pane. Name the new model file (In my case, I made it as Media) and click Add.

Step 2

In the Entity Data Model wizard, select EF Designer from the database and click Next as shown in figure 4.

 

Step 3 

Click the "New Connection" button. The "Connection Properties" window will open. 

Step 4

In the "Connection Properties" window, provide the name of the local Server, where the database was created (in this case, (DESKTOP-585QGBN)). After providing the Server name, select demo from the available databases and click OK.

 

Step 5

You can use the default name for the connection to save the Web.Config file; In my case it is MediaPlayerEntities. Now, click Next.

Step 6

Select the table to generate the models for Media table and click Finish.



My database schema is similar to the image as shown in figure 7.



Create a Web API Controller

Create a new empty Controller. Right-click the Controllers folder and select Add –> New Empty Web API Controller, as shown in figure 8. In my case, I named it as MediaController.cs.

Write the code, given below, in the Controller.
  1. public class MediaController : ApiController  
  2.     {  
  3.         MediaPlayerEntities __context = new MediaPlayerEntities();  
  4.   
  5.       //  /api/Media/GetMediaPlaylist  
  6.         public HttpResponseMessage GetMediaPlaylist()  
  7.         {  
  8.             try  
  9.             {  
  10.                 var playlist = __context.Media;  
  11.                 return Request.CreateResponse(HttpStatusCode.OK, playlist, Configuration.Formatters.JsonFormatter);  
  12.             }  
  13.             catch(Exception ex)  
  14.             {  
  15.                 return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);  
  16.             }  
  17.         }  
  18.     }  

Now, our Web API is ready. The above method GetMediaPlaylist in media controller is used to return the list of media table records from the context as a JSON format. Let us test the response, using the POSTMAN.

API - /api/Media/GetMediaPlaylist

Response in POSTMAN

Title : This will be the title for the video.

Poster : Thumbnail Image link for the video.

source : Video source link.

Note

Please make sure that the property name (title, poster, source) should be as it is  because on client side, the kendo media player property is predefined so if anything is misspelled in the property name, the media player doesn't work.

Create a HTML page

Right click on project add-> new HTML page, in my case I named it as MediaPlayer.html

MediaPlayer.html

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.       
  5.     <style>  
  6.         html {  
  7.             font-size: 14px;  
  8.             font-family: Arial, Helvetica, sans-serif;  
  9.         }  
  10.     </style>  
  11.     <title></title>  
  12.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css" />  
  13.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css" />  
  14.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css" />  
  15.     <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css" />  
  16.   
  17.     <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>  
  18.     <script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>  
  19. </head>  
  20. <body>  
  21.     <div id="example">  
  22.         <h3 style="font-style:italic"> My Playlist</h3>  
  23.         <br/>  
  24. <br/>  
  25.         <div class="demo-section k-content wide" style="width: 925px;">  
  26.             <div id="mediaplayer" style="height:360px; width:640px"></div>  
  27.             <div class="k-list-container playlist"><ul id="listView" class="k-list"></ul></div>  
  28.         </div>  
  29.         <script type="text/javascript">  
  30.         $(document).ready(function () {  
  31.   
  32.             $("#mediaplayer").kendoMediaPlayer({  
  33.   
  34.                 autoPlay: true
  35.                   
  36.             });  
  37.   
  38.             var videos = new kendo.data.DataSource({  
  39.                 transport: {  
  40.                     read: {  
  41.                         url: "api/Media/GetMediaPlayerlist" 
  42.                         dataType: "json"  
  43.                     }  
  44.                 },  
  45.                 
  46.             });  
  47.   
  48.             var listView = $("#listView").kendoListView({  
  49.                 dataSource: videos,  
  50.                 selectable: true,  
  51.                 scrollable: true,  
  52.                 template: kendo.template($("#template").html()),  
  53.                 change: onChange,  
  54.                 dataBound: onDataBound  
  55.             });  
  56.   
  57.             function onChange() {  
  58.                 var index = this.select().index();  
  59.                 var dataItem = this.dataSource.view()[index];  
  60.                 $("#mediaplayer").data("kendoMediaPlayer").media(dataItem);  
  61.             }  
  62.   
  63.             function onDataBound() {  
  64.                 this.select(this.element.children().first());  
  65.             }  
  66.   
  67.         });  
  68.         </script>  
  69.         <script type="text/x-kendo-template" id="template">  
  70.             <li class="k-item k-state-default" onmouseover="$(this).addClass('k-state-hover')"  
  71.                 onmouseout="$(this).removeClass('k-state-hover')">  
  72.                 <span>  
  73.                     <img src="#:poster#" />  
  74.                     <h5>#:title#</h5>  
  75.                 </span>  
  76.             </li>  
  77.         </script>  
  78.         <style>  
  79.             .k-mediaplayer {  
  80.                 float: left;  
  81.             }  
  82.   
  83.             .playlist {  
  84.                 float: left;  
  85.                 width: 280px;  
  86.                 height: 360px;  
  87.                 overflow: auto;  
  88.             }  
  89.   
  90.                 .playlist ul, .playlist li {  
  91.                     list-style-type: none;  
  92.                     margin: 0;  
  93.                     padding: 0;  
  94.                 }  
  95.   
  96.                 .playlist .k-item {  
  97.                     border-bottom-style: solid;  
  98.                     border-bottom-width: 1px;  
  99.                     padding: 14px 15px;  
  100.                 }  
  101.   
  102.                     .playlist .k-item:last-child {  
  103.                         border-bottom-width: 0;  
  104.                     }  
  105.   
  106.                 .playlist span {  
  107.                     cursor: pointer;  
  108.                     display: block;  
  109.                     overflow: hidden;  
  110.                     text-decoration: none;  
  111.                 }  
  112.   
  113.                     .playlist span img {  
  114.                         border: 0 none;  
  115.                         display: block;  
  116.                         height: 56px;  
  117.                         object-fit: cover;  
  118.                         width: 100px;  
  119.                         float: left;  
  120.                     }  
  121.   
  122.                 .playlist h5 {  
  123.                     display: block;  
  124.                     font-weight: normal;  
  125.                     margin: 0;  
  126.                     overflow: hidden;  
  127.                     padding-left: 10px;  
  128.                     text-align: left;  
  129.                 }  
  130.         </style>  
  131.     </div>  
  132.   
  133. </body>  
  134. </html>   

The above code consists of two Kendo widgets.

  1. Media player
  2. List view

Media Player

The Kendo UI Media player widget plays video files from static source or stream online youtube videos and enriches your website with dynamic content in a user-friendly interface.

List View

Please refer to my previous
article to get how to work with kendo list view using ASP.NET WEB API.

This list view is used to display a list of media files  which we got from the datatsource and act as a playlist. The list view consists of two events i) Databound ii) change.

In the databound event, the first data item of the listview datasource will be assigned to kendo media player using media function. On change event, the video which is clicked to play in list view will be assigned to Kendo media player using media function.

Result

 
Conclusion

From this article, we have seen how to create a video playlist in our web application with Kendo widgets (Media player and list View) using ASP.NET Web API. Will see more about Kendo media player from my future articles.

I hope you enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all