Wednesday, October 14, 2015

Play Youtube video using video url in Asp.net

In this article I am going to explain how to play YouTube video using video url in asp.net web application

Description:

I want to play the youtube video in asp.net application. To play the video the simply enter the particular youtube video url.

Implementation:
Add a webform to asp.net application. Add the below given HTML markup to webform.

HTML Markup:
<table>
   <tr>
   <td>Enter Youtube Video URL :</td>
   <td> <asp:TextBox ID="txturl" runat="server" Width="450px"></asp:TextBox></td>
   <td> <asp:Button ID="btnsubmit"
            runat="server" Text="Play"/></td>
   </tr>
   <tr>
   <td colspan="3" id="video" runat="server"></td>
   </tr>
   </table>

On button click write the below given code


C# code:
protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string VideoUrl = txturl.Text;
        string vCode = VideoUrl.Substring(VideoUrl.LastIndexOf("v=") + 2);
        if (vCode.Contains("&"))
            vCode = vCode.Substring(0, vCode.LastIndexOf("&"));
        string frame = @"<object width='{0}' height='{1}' data='http://www.youtube.com/v/{2}&autoplay=1' codetype='application/x-shockwave-flash'>
  &ltparam name='movie' value='http://www.youtube.com/v/{2}&autoplay=1' ></param></object>";       
        string fWidth = "600px";
        string fHeight = "500px";
        video.InnerHtml = String.Format(frame, fWidth, fHeight, vCode);
    }

VB code:
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
        Dim VideoUrl As String = txturl.Text
        Dim vCode As String = VideoUrl.Substring(VideoUrl.LastIndexOf("v=") + 2)
        If vCode.Contains("&") Then
            vCode = vCode.Substring(0, vCode.LastIndexOf("&"))
        End If
        Dim frame As String = "<object width='{0}' height='{1}' data='http://www.youtube.com/v/{2}&autoplay=1' codetype='application/x-shockwave-flash'>" & vbCr & vbLf & "  &ltparam name='movie' value='http://www.youtube.com/v/{2}&autoplay=1' ></param></object>"
        Dim fWidth As String = "600px"
        Dim fHeight As String = "500px"
        video.InnerHtml = [String].Format(frame, fWidth, fHeight, vCode)
    End Sub

Build the application and run it. To test the code enter the any youtube video URL.

 Demo:
Play Youtube video using video url in Asp.net

  In this article we have learn to how to play youtube video using video url in asp.net. I hope you enjoyed this article. 

No comments:

Post a Comment