This
article will show you how you can play your YouTube video in your asp.net mvc application
using c#.net from a playlist. This article you can use in MVC2, MVC3, MVC4,
MVC5 and MVC6.
Some of my
previous articles are as follows: Login
From With Active User Validation in Asp.Net MVC Using C#.Net, Enable
and Disable ADO.NET and OLEDB Connection Pooling In .Net, Access
Hidden Or HiddenFor Fields Value At Controller End In Asp.Net Mvc Using C#, Twitter,
Pinterest, Facebook, GooglePlus and Tumblr Social Sharing Buttons Using jQuery
In Asp.Net MVC, Fixed
Header Web Page Using CSS3 Without jQuery In Asp.Net MVC, CountDown
To Show Under Construction Page Using jQuery In Asp.Net MVC, Bootstrap
Style Dynamic jQuery Dropdowns Menu Using Asp.Net MVC In C#.Net, Blink
Title or Multiple Browser Title Using jQuery in Asp.Net, MVC, Social
Share Buttons of Facebook, Twitter, Google Plus and Pinterest Using jQuery in
Asp.Net MVC, Bootstrap
3 and jQuery Form Validation Plugin in Asp.Net MVC, MultiSelect
DropdownList Using jQuery in Asp.Net MVC and C#.Net, Three
Level Menus With Disable Menu Item Using jQuery In Asp.Net, jQuery
Digital Clock For Asp.Net, MVC and HTML Applications.
So for this
article first we will create a new asp.net application and create a model class
file into the model folder.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
MvcApplication7.Models
{
public class VideoPlayListModel
{
public List<VideoModel>
VideoCollection { get; set; }
}
public class VideoModel
{
public int Id { get; set; }
public string VideoURL { get; set; }
public string VideoTitle { get; set; }
}
}
|
After
creating model class file we will add a controller class file and add the below
code into the controller file.
using
MvcApplication7.Models;
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MvcApplication7.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult
Index(int
videoId = 0)
{
VideoPlayListModel
videoPlayListModel = new Models.VideoPlayListModel();
List<VideoModel>
videoModel = new List<VideoModel>();
videoModel = GetVideoCollection();
videoPlayListModel.VideoCollection = videoModel;
if (videoId == 0)
{
//your default video url
ViewBag.VideoURL = "https://www.youtube.com/embed/th0Bye2UmTU";
}
else
{
//Your playlist video selected by user
ViewBag.VideoURL = videoPlayListModel.VideoCollection.Where(m =>
m.Id == videoId).FirstOrDefault().VideoURL;
}
return
View(videoPlayListModel);
}
/// <summary>
/// You can prepare this
collection from your Entity table
/// </summary>
/// <returns></returns>
public List<VideoModel> GetVideoCollection()
{
List<VideoModel>
videoModel = new List<VideoModel>();
videoModel.Add(new VideoModel { Id = 1, VideoTitle = "Windows
Application In C#.Net", VideoURL = "https://www.youtube.com/embed/th0Bye2UmTU" });
videoModel.Add(new VideoModel { Id
= 2, VideoTitle = "Developing windows
application in C#.NET", VideoURL = "https://www.youtube.com/watch?v=Om6pCLE0Qpo" });
videoModel.Add(new VideoModel { Id = 3, VideoTitle = "How
to create C# Windows Form Application",
VideoURL = "https://www.youtube.com/watch?v=ploAZFL_R04" });
videoModel.Add(new VideoModel { Id = 4, VideoTitle = "Metro
or Modern UI Style Windows Application in C#", VideoURL = "https://www.youtube.com/watch?v=Y_NZy4bLHJY" });
return videoModel;
}
}
}
|
In above
controller file just check the videocollection() method. This method I have
used to prepare the data for the playlist. You can prepare your list from the
data base.
Now check
the Index ActionResult. In this I have taken a parameter int videoId = 0. This parameter will capture the video id when user
clicks on the playlist to play the video.
I have
stored the video url in viewbag. So if user has not selected any video on that
case a default video will be assign to play otherwise video will selected as
per the video id present in the video list.
Now we will
create the view and add the below code into the view.
@model MvcApplication7.Models.VideoPlayListModel
@{
ViewBag.Title
= "How To Play Youtube Video With
Play List In MVC Using C#.Net";
}
<style>
ul {
list-style: square outside none;
}
</style>
<h2>YouTube Video</h2>
<div style="float:left;width:42%;"><iframe allowfullscreen="" frameborder="0" height="315" src="@ViewBag.VideoURL" width="560"></iframe></div>
<div style="float:left;width:40%;background-color:gold;">
<div><b>Play List</b></div>
@{
<ul>
@foreach (var item in Model.VideoCollection)
{
<li><a href="/Home/Index?videoId=@item.Id">@item.VideoTitle</a></li>
}
</ul>
}
</div>
|
In above
code I have added the reference of the model class file. Now I have assign the
video url to the iframe to play the url video. After that I have prepared the
playlist.
Now we have
done. Run the application to check the output.
Youtube links must contain "/embed/" instead of "/watch?v=" on URLs.
ReplyDeleteHi
DeleteIt does not matter. I have used URL in Iframe.