Tuesday, 21 June 2016

How To Play YouTube Video With Play List In MVC Using C#.Net

6/21/2016 - By Pranav Singh 2


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.


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.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. Youtube links must contain "/embed/" instead of "/watch?v=" on URLs.

    ReplyDelete
    Replies
    1. Hi
      It does not matter. I have used URL in Iframe.

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top