Play Videos Using Kendo Media Player And ListView In ASP.NET MVC5

In this Article, I will demonstrate how to the play video(s) in Media Player Using Kendo UI Media Player and Listview in ASP.NET MVC5

Introduction
 
In some situations, we need to play the video(s) in our Web Application so, In this article, I will demonstrate how to load the videos to a media player, how to play the video(s) and create a playlist using Kendo Media Player and ListView in ASP.NET MVC5 with Entity framework. 
 
Prerequisites 
  • Visual Studio
  • SQL Server
  • Basic Knowledge of ASP.Net MVC
  • Basic Knowledge of Entity Framework
  • Basic Knowledge of JQuery
  • Basic Knowledge of CSS
Article Flow 
  • Create a table in database with values
  • Create ASP.NET MVC Empty project
  • Configure Entity Framework with database and application
  • Create a Controller and View
  • Enable Kendo UI Features
  • Load URLs and Create a playlist with Media Player and Listview
Create a table in the database
 
First, we will create a table in SQL Server to populate a kendo with video playlist in ASP.NET MVC.  I have created a table "Playlist" with the following design.  
 


Execute the below query to create a table with the above design.
  1. CREATE TABLE [dbo].[playlist]   
  2.   (   
  3.      [videoid] [INT] IDENTITY(1, 1) NOT NULL,   
  4.      [title]   [NVARCHAR](200) NULL,   
  5.      [poster]  [NVARCHAR](maxNULL,   
  6.      [source]  [NVARCHAR](maxNULL,   
  7.      CONSTRAINT [PK_Playlist] PRIMARY KEY CLUSTERED ( [videoid] ASC )WITH (   
  8.      pad_index = OFF, statistics_norecompute = OFF, ignore_dup_key = OFF,   
  9.      allow_row_locks = on, allow_page_locks = onON [PRIMARY]   
  10.   )   
  11. ON [PRIMARY]   
  12. textimage_on [PRIMARY]   
  13.   
  14. go   
And, insert a few youtube URLs as records like below. 
 
Execute the below query to get the same records, 
  1. SET IDENTITY_INSERT [dbo].[Playlist] ON   
  2.   
  3. go   
  4.   
  5. INSERT [dbo].[playlist]   
  6.        ([videoid],   
  7.         [title],   
  8.         [poster],   
  9.         [source])   
  10. VALUES (1,   
  11.         N'Telerik Platform - Enterprise Mobility. Unshackled.',   
  12.         N'http://img.youtube.com/vi/N3P6MyvL-t4/1.jpg',   
  13.         N'https://www.youtube.com/watch?v=N3P6MyvL-t4')   
  14.   
  15. go   
  16.   
  17. INSERT [dbo].[playlist]   
  18.        ([videoid],   
  19.         [title],   
  20.         [poster],   
  21.         [source])   
  22. VALUES (2,   
  23.         N'Learn How York Solved Its Database Problem',   
  24.         N'http://img.youtube.com/vi/_S63eCewxRg/1.jpg',   
  25.         N'https://www.youtube.com/watch?v=_S63eCewxRg')   
  26.   
  27. go   
  28.   
  29. INSERT [dbo].[playlist]   
  30.        ([videoid],   
  31.         [title],   
  32.         [poster],   
  33.         [source])   
  34. VALUES (3,   
  35.         N'Responsive Website Delivers for Reeves Import Motorcars',   
  36.         N'http://img.youtube.com/vi/DYsiJRmIQZw/1.jpg',   
  37.         N'https://www.youtube.com/watch?v=DYsiJRmIQZw')   
  38.   
  39. go   
  40.   
  41. INSERT [dbo].[playlist]   
  42.        ([videoid],   
  43.         [title],   
  44.         [poster],   
  45.         [source])   
  46. VALUES (4,   
  47.         N'Digital Transformation: A New Way of Thinking',   
  48.         N'http://img.youtube.com/vi/gNlya720gbk/1.jpg',   
  49.         N'https://www.youtube.com/watch?v=gNlya720gbk')   
  50.   
  51. go   
  52.   
  53. INSERT [dbo].[playlist]   
  54.        ([videoid],   
  55.         [title],   
  56.         [poster],   
  57.         [source])   
  58. VALUES (5,   
  59.         N'Take a Tour of the Telerik Platform',   
  60.         N'http://img.youtube.com/vi/rLtTuFbuf1c/1.jpg',   
  61.         N'https://www.youtube.com/watch?v=rLtTuFbuf1c')   
  62.   
  63. go   
  64.   
  65. INSERT [dbo].[playlist]   
  66.        ([videoid],   
  67.         [title],   
  68.         [poster],   
  69.         [source])   
  70. VALUES (6,   
  71.         N'Why Telerik Analytics - Key Benefits For Your Applications',   
  72.         N'https://i.ytimg.com/vi/CpHKm2NruYc/1.jpg',   
  73.         N'https://www.youtube.com/watch?v=CpHKm2NruYc')   
  74.   
  75. go   
  76.   
  77. SET IDENTITY_INSERT [dbo].[Playlist] OFF   
  78.   
  79. go   
Create ASP.NET MVC Empty project 
  1. Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it "KendoMediaPlayerINMVC5".
  2. Now, click OK.
  3. Then, select Empty Web API template and click OK to create the project.
  4. Once you click OK, the project will be created with the basic architecture of MVC.
  5. If you are not aware of how to create an Empty ASP.NET Web Application, please visit  Step1 and Step 2 to learn. Once you complete these steps you will get the screen as below
 
Configure Entity Framework with database and application
 
Here, I have already discussed how to configure and implement the database-first approach. In the meantime, choose your created table with Entity Framework. Once we are done with our configuration with SQL table "Playlist" from CSharpCorner database, we will get the below screen as a succeeding configuration.  
 
 
Create a Controller and View 
 
Now create an empty controller and view, here I created a controller with the name of "KenoMediaPlayerController".Whenever we create an empty Controller, it is created with empty Index action method. And create an empty view to this action method "Index".
 
Enable Kendo UI Features
 
Here, we going to enable the Kendo UI  features with our application by adding the below CSS and JS in our shared _Layout page or view. 
  1. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.common-material.min.css" />  
  2. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.min.css" />  
  3. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.mobile.min.css" />  
  4. <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/jquery.min.js"></script>  
  5. <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/kendo.all.min.js"></script>  
And now create a div which will act as Media Player
  1. <div id="example">  
  2.     <div class="demo-section k-content wide" style="max-width: 644px;">  
  3.         <div id="mediaplayer" style="height:360px"></div>  
  4.     </div>  
  5. </div>  
And, add the script given below to enable the kendoMediaPlayer plugin to the respective control.  In below code, you can see that autoPlay property has mentioned value true to play the video automatically, and the media will help us load the video from the source, in meantime title will be shown the top of the video and source represents where the video source from it is.Here we mentioned the static youtube URL on source property.
  1. <script type="text/javascript">  
  2.     $(document).ready(function() {  
  3.         $("#mediaplayer").kendoMediaPlayer({  
  4.             autoPlay: true,  
  5.             media: {  
  6.                 title: "C# Corner Annual Conference 2014",  
  7.                 source: "https://www.youtube.com/watch?v=tGRdMMLIUHE"  
  8.             }  
  9.         });  
  10.     });  
  11. </script>  
Okay now change the controller name in RouteConfig.cs file as below
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace KendoMediaPlayerINMVC5 {  
  8.     public class RouteConfig {  
  9.         public static void RegisterRoutes(RouteCollection routes) {  
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  12.                 controller = "KenoMediaPlayer", action = "Index", id = UrlParameter.Optional  
  13.             });  
  14.         }  
  15.     }  
  16. }  
Run your application
 
 
Now we will create a playlist to play video(s) before that we should below add logic in controller to load the playlist URLs from database using entity framework
  1. public JsonResult CreatePlaylist() {  
  2.     CSharpCornerEntities entity = new CSharpCornerEntities();  
  3.     var result = entity.Playlists.ToList();  
  4.     return Json(result, JsonRequestBehavior.AllowGet);  
  5. }  
Now add two tags or divs which will act as Media Player and Listview in view
  1. <div id="example">  
  2.     <div class="demo-section k-content wide" style="max-width: 925px;">  
  3.         <div id="mediaplayer" style="height:360px"></div>  
  4.         <div class="k-list-container playlist">  
  5.             <ul id="listView" class="k-list"></ul>  
  6.         </div>  
  7.     </div>  
And enable the Kendo Media Player and Listview to those controls
  1. $("#mediaplayer").kendoMediaPlayer({  
  2.     autoPlay: true,  
  3. });  
  4. $("#listView").kendoListView();  
Load videos and Create a playlist with Media Player and Listview
 
Add the below codes in your view script
  1. <script type="text/javascript">  
  2.     $(document).ready(function() {  
  3.         $("#mediaplayer").kendoMediaPlayer({  
  4.             autoPlay: true,  
  5.         });  
  6.         var videos = new kendo.data.DataSource({  
  7.             transport: {  
  8.                 read: {  
  9.                     url: "http://localhost:1583/KenoMediaPlayer/CreatePlaylist",  
  10.                     dataType: "json"  
  11.                 }  
  12.             },  
  13.         });  
  14.         var listView = $("#listView").kendoListView({  
  15.             dataSource: videos,  
  16.             selectable: true,  
  17.             scrollable: true,  
  18.             template: kendo.template($("#template").html()),  
  19.             change: onChange,  
  20.             dataBound: onDataBound  
  21.         });  
  22.   
  23.         function onChange() {  
  24.             var index = this.select().index();  
  25.             var dataItem = this.dataSource.view()[index];  
  26.             $("#mediaplayer").data("kendoMediaPlayer").media(dataItem);  
  27.         }  
  28.   
  29.         function onDataBound() {  
  30.             this.select(this.element.children().first());  
  31.         }  
  32.     });  
  33. </script>  
  34. <script type="text/x-kendo-template" id="template">  
  35.     <li class="k-item k-state-default" onmouseover="$(this).addClass('k-state-hover')" onmouseout="$(this).removeClass('k-state-hover')"> <span>  
  36. <img src="#:poster#" />  
  37. <h5>#:title#</h5>  
  38. </span> </li>  
  39. </script>  
Detailed Description 
 
In the above code, you can see that two Kendo Widgets have been added to the view such as Media Player and Listview.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.Here, we just stored the URL of videos in Database.This list view is used to display a list of media files which we got from the data source and act as a playlist. The list view consists of two events such as Databound and change.In the databound event, the first data item of the listview data source 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. If you are not aware to work with Kendo Listview means Please visit Here to learn. 
 
Now add the below CSS to give good appearance to those controls
  1. <style>  
  2.     .k-mediaplayer {  
  3.         float: left;  
  4.         box-sizing: border-box;  
  5.         width: 70%;  
  6.     }  
  7.   
  8.     .playlist {  
  9.         float: left;  
  10.         height: 360px;  
  11.         overflow: auto;  
  12.         width: 30%;  
  13.     }  
  14.   
  15.     .playlist ul,  
  16.     .playlist li {  
  17.         list-style-type: none;  
  18.         margin: 0;  
  19.         padding: 0;  
  20.     }  
  21.   
  22.     .playlist .k-item {  
  23.         border-bottom-style: solid;  
  24.         border-bottom-width: 1px;  
  25.         padding: 14px 15px;  
  26.     }  
  27.   
  28.     .playlist .k-item:last-child {  
  29.         border-bottom-width: 0;  
  30.     }  
  31.   
  32.     .playlist span {  
  33.         cursor: pointer;  
  34.         display: block;  
  35.         overflow: hidden;  
  36.         text-decoration: none;  
  37.     }  
  38.   
  39.     .playlist span img {  
  40.         border: 0 none;  
  41.         display: block;  
  42.         height: 56px;  
  43.         object-fit: cover;  
  44.         width: 100px;  
  45.         float: left;  
  46.     }  
  47.   
  48.     .playlist h5 {  
  49.         display: block;  
  50.         font-weight: normal;  
  51.         margin: 0;  
  52.         overflow: hidden;  
  53.         padding-left: 10px;  
  54.         text-align: left;  
  55.     }  
  56. </style>  
Run your application
 
 
 
Complete View
 
1._Layout.cshtml 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  7.     <title>@ViewBag.Title - Media Player</title>  
  8.     <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />  
  9.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />  
  10.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.common-material.min.css" />  
  11.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.min.css" />  
  12.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.mobile.min.css" />  
  13.     <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/jquery.min.js"></script>  
  14.     <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/kendo.all.min.js"></script>  
  15. </head>  
  16.   
  17. <body>  
  18.     <div class="navbar navbar-inverse navbar-fixed-top">  
  19.         <div class="container">  
  20.             <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  21. <span class="icon-bar"></span>  
  22. <span class="icon-bar"></span>  
  23. <span class="icon-bar"></span>  
  24. </button> @Html.ActionLink("Kendo Media Player with ASP.NET MVC5""Index""Home"nullnew { @class = "navbar-brand" }) </div>  
  25.             <div class="navbar-collapse collapse">  
  26.                 <ul class="nav navbar-nav"> </ul>  
  27.             </div>  
  28.         </div>  
  29.     </div>  
  30.     <div class="container body-content"> @RenderBody()  
  31.         <hr /> </div>  
  32. </body>  
  33.   
  34. </html>  
Index.cshtml 
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3. } < br / > < div id = "example" > < div class = "demo-section k-content wide"  
  4. style = "max-width: 925px;" > < div id = "mediaplayer"  
  5. style = "height:360px" > < /div> < div class = "k-list-container playlist" > < ul id = "listView"  
  6. class = "k-list" > < /ul></div > < /div> < script type = "text/javascript" > $(document).ready(function() {  
  7.     $("#mediaplayer").kendoMediaPlayer({  
  8.         autoPlay: true,  
  9.     });  
  10.     var videos = new kendo.data.DataSource({  
  11.         transport: {  
  12.             read: {  
  13.                 url: "http://localhost:1583/KenoMediaPlayer/CreatePlaylist",  
  14.                 dataType: "json"  
  15.             }  
  16.         },  
  17.     });  
  18.     var listView = $("#listView").kendoListView({  
  19.         dataSource: videos,  
  20.         selectable: true,  
  21.         scrollable: true,  
  22.         template: kendo.template($("#template").html()),  
  23.         change: onChange,  
  24.         dataBound: onDataBound  
  25.     });  
  26.   
  27.     function onChange() {  
  28.         var index = this.select().index();  
  29.         var dataItem = this.dataSource.view()[index];  
  30.         $("#mediaplayer").data("kendoMediaPlayer").media(dataItem);  
  31.     }  
  32.   
  33.     function onDataBound() {  
  34.         this.select(this.element.children().first());  
  35.     }  
  36. }); < /script> < script type = "text/x-kendo-template"  
  37. id = "template" > < li class = "k-item k-state-default"  
  38. onmouseover = "$(this).addClass('k-state-hover')"  
  39. onmouseout = "$(this).removeClass('k-state-hover')" > < span > < img src = "#:poster#" / > < h5 > #: title# < /h5> < /span> < /li> < /script> < style > .k - mediaplayer {  
  40.     float: left;  
  41.     box - sizing: border - box;  
  42.     width: 70 % ;  
  43. }.playlist {  
  44.     float: left;  
  45.     height: 360 px;  
  46.     overflow: auto;  
  47.     width: 30 % ;  
  48. }.playlist ul, .playlist li {  
  49.     list - style - type: none;  
  50.     margin: 0;  
  51.     padding: 0;  
  52. }.playlist.k - item {  
  53.     border - bottom - style: solid;  
  54.     border - bottom - width: 1 px;  
  55.     padding: 14 px 15 px;  
  56. }.playlist.k - item: last - child {  
  57.     border - bottom - width: 0;  
  58. }.playlist span {  
  59.     cursor: pointer;  
  60.     display: block;  
  61.     overflow: hidden;  
  62.     text - decoration: none;  
  63. }.playlist span img {  
  64.     border: 0 none;  
  65.     display: block;  
  66.     height: 56 px;  
  67.     object - fit: cover;  
  68.     width: 100 px;  
  69.     float: left;  
  70. }.playlist h5 {  
  71.     display: block;  
  72.     font - weight: normal;  
  73.     margin: 0;  
  74.     overflow: hidden;  
  75.     padding - left: 10 px;  
  76.     text - align: left;  
  77. } < /style> < /div>  
Complete Controller
  1. using KendoMediaPlayerINMVC5.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace KendoMediaPlayerINMVC5.Controllers {  
  8.     public class KenoMediaPlayerController: Controller {  
  9.         //  
  10.         // GET: /KenoMediaPlayer/  
  11.         public ActionResult Index() {  
  12.             return View();  
  13.         }  
  14.         public JsonResult CreatePlaylist() {  
  15.             CSharpCornerEntities1 entity = new CSharpCornerEntities1();  
  16.             var result = entity.Playlists.ToList();  
  17.             return Json(result, JsonRequestBehavior.AllowGet);  
  18.         }  
  19.     }  
  20. }  
RouteConfig.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace KendoMediaPlayerINMVC5 {  
  8.     public class RouteConfig {  
  9.         public static void RegisterRoutes(RouteCollection routes) {  
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  12.                 controller = "KenoMediaPlayer", action = "Index", id = UrlParameter.Optional  
  13.             });  
  14.         }  
  15.     }  
  16. }  
Here, we have created an empty project so it can not get build in CSS. So, I have attached the respective CSS under Content folder and referred to a project. Refer the attached project for reference and I did attach the demonstrated project without package for Entity Framework 6.0 due to the size limit.

Summary 
 
In this article, we have seen how to create a video playlist in our ASP.NET MVC5 web application with Kendo Media player and list View using Entity Framework.

I hope you enjoyed this article. Your valuable feedback and comments about this article are always welcome.