In
this article I am going to explain how to play YouTube video using video url in
asp.net web application
Description:
In
the previous articles I have explained How to fill Country, State and Citydropdown list using linq to sql and how to set the video autoplay and disablethe related video option.
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'>
<param 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
& "
<param 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:
No comments:
Post a Comment