Embed YouTube Video in Your Website Using URL in ASP.Net

Lets start the example.

.ASPX Page


 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PlayYouTubeVideo.aspx.cs" Inherits="YouTubeVideo.PlayYouTubeVideo" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div id="divVideo" runat="server">  
   </div>  
   </form>  
 </body>  
 </html>  


In the above markup, I have just take a Div control and give it an id and make it server control by adding runat="server" Now In code behind write the following function and call it on your desire location. I have called it on Page_Load event. C# Code

 /// <summary>  
 /// Function to set the video frame on Div control for play  
 /// </summary>  
 protected void SetVideoForPlay()  
 {  
   //YouTube Video URL  
   string youtubeUrl = "http://www.youtube.com/watch?feature=endscreen&v=gfedwjAOMZI&NR=1";  
  string vCode = youtubeUrl.Substring(youtubeUrl.LastIndexOf("v=") + 2);  
  if (vCode.Contains("&"))  
    vCode = vCode.Substring(0, vCode.LastIndexOf("&"));  
   string sHtml = @"<object width='{0}' height='{1}' data='http://www.youtube.com/v/{2}&autoplay=0' codetype='application/x-shockwave-flash'>  
  &ltparam name='movie' value='http://www.youtube.com/v/{2}&autoplay=0'></param></object>";  
  //define frame size  
  string sWidth = "500px";  
  string sHeight = "500px";  
  //insert code to the Div  
  divVideo.InnerHtml = String.Format(sHtml, sWidth, sHeight,vCode);  
 }  

Comments

Popular Posts