Solved

How do I play a YouTube Video in Streaming format using a Windows Forms Application using C#?

Posted on 2010-09-04
11
2,087 Views
Last Modified: 2013-12-17
I have tried GetResponseStream() method of WebRequest Class.. I am looking for a code using System.Net Namespace..
0
Comment
Question by:starlite551
11 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 33604367
What about using the web browser control? http://www.codeproject.com/KB/WPF/YouViewer.aspx
0
 
LVL 12

Author Comment

by:starlite551
ID: 33604499
Sorry But I was looking for Non - WPF version of code.. Is there a way to do it using file streams ?? Youtube videos are in .flv format so if we can convert a youtube url into a .flv file then we need some player to play that file.. but its a big procedure and very complex.. I dont think pure c# code can work out on this issue.. Do you have any other suggestion or any idea?? using WebRequest and WebResponse Classes of System.Net namespace we can only extract the html contents of the youtube video page.. how do we extract only video..??
0
 
LVL 22

Expert Comment

by:ambience
ID: 33614327
You can generate a small wrapper html page and load it in the web-browser control. The html page would only contain the <object> tag that is used by youtube page to host Flash player plugin.
As for the actual video data, i suggest that you use Wireshark to capture sample video download session and it would tell you the format of the url that the flash player uses to fetch the data. Opening that url and saving data using WebRequest should give you the flv file.
In short, all you need is to figure out the right url to download.
0
 
LVL 12

Author Comment

by:starlite551
ID: 33624200
Can You Ellaborate your point.. Can u explain it in code please..? I need the source code for doing it..
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33630387
This is along the lines of what abmience suggested above:
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace WindowsFormsApplication1

{

	public class Form1 : Form

	{

		private WebBrowser browser;

		private System.ComponentModel.IContainer components = null;



		protected override void Dispose(bool disposing)

		{

			if (disposing && (components != null))

			{

				components.Dispose();

			}

			base.Dispose(disposing);

		}



		private void InitializeComponent()

		{

			this.browser = new System.Windows.Forms.WebBrowser();

			this.SuspendLayout();

			// 

			// browser

			// 

			this.browser.Dock = System.Windows.Forms.DockStyle.Fill;

			this.browser.Location = new System.Drawing.Point(0, 0);

			this.browser.MinimumSize = new System.Drawing.Size(20, 20);

			this.browser.Name = "browser";

			this.browser.Size = new System.Drawing.Size(284, 262);

			this.browser.TabIndex = 0;

			// 

			// Form1

			// 

			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

			this.ClientSize = new System.Drawing.Size(284, 262);

			this.Controls.Add(this.browser);

			this.Name = "Form1";

			this.Text = "Form1";

			this.Load += new System.EventHandler(this.Form1_Load);

			this.ResumeLayout(false);



		}



		public Form1()

		{

			InitializeComponent();

		}



		private void Form1_Load(object sender, EventArgs e)

		{

			browser.DocumentText =@"<html><head><body>

<object style=""height: 344px; width: 425px""><param name=""movie"" value=""http://www.youtube.com/v/trmG0mgrkM8?version=3""><param name=""allowFullScreen"" value=""true""><param name=""allowScriptAccess"" value=""always""><embed src=""http://www.youtube.com/v/trmG0mgrkM8?version=3"" type=""application/x-shockwave-flash"" allowfullscreen=""true"" allowScriptAccess=""always"" width=""425"" height=""344""></object>

			</body></html>";

		}

	}

}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33630396
...note that I just dropped a WebBrowser control named "browser" on my form, then added the code in Form1_Load.
0
 
LVL 12

Author Comment

by:starlite551
ID: 33633839
In this example how do u load a different youtube video.. I tried to copy and paste some youtube links in the html code but it didn`t work.. Is there anything special i need to do with the links?
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 33641341
1) There's a couple places the URL appears in the HTML code.
2) These URL's are specifically for embedding the YouTube player in HTML; the URL you get from Internet Explorer when you visit the YouTube web pages is different.
3) You probably want some function, like below, to accept an easy to provider parameter - like the video's ID - and return the complete HTML.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace WindowsFormsApplication1

{

	public class Form1 : Form

	{

		private WebBrowser browser;

		private System.ComponentModel.IContainer components = null;



		protected override void Dispose(bool disposing)

		{

			if (disposing && (components != null))

			{

				components.Dispose();

			}

			base.Dispose(disposing);

		}



		private void InitializeComponent()

		{

			this.browser = new System.Windows.Forms.WebBrowser();

			this.SuspendLayout();

			// 

			// browser

			// 

			this.browser.Dock = System.Windows.Forms.DockStyle.Fill;

			this.browser.Location = new System.Drawing.Point(0, 0);

			this.browser.MinimumSize = new System.Drawing.Size(20, 20);

			this.browser.Name = "browser";

			this.browser.Size = new System.Drawing.Size(284, 262);

			this.browser.TabIndex = 0;

			// 

			// Form1

			// 

			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

			this.ClientSize = new System.Drawing.Size(284, 262);

			this.Controls.Add(this.browser);

			this.Name = "Form1";

			this.Text = "Form1";

			this.Load += new System.EventHandler(this.Form1_Load);

			this.ResumeLayout(false);



		}



		public Form1()

		{

			InitializeComponent();

		}



		private void Form1_Load(object sender, EventArgs e)

		{

			ShowVideo("Hf5cJDUwMp8");

		}



		private void ShowVideo(string VideoId)

		{

			browser.DocumentText = String.Format(@"<html><head><body>

<object style=""height: 344px; width: 425px""><param name=""movie"" value=""http://www.youtube.com/v/{0}?version=3""><param name=""allowFullScreen"" value=""true""><param name=""allowScriptAccess"" value=""always""><embed src=""http://www.youtube.com/v/{0}?version=3"" type=""application/x-shockwave-flash"" allowfullscreen=""true"" allowScriptAccess=""always"" width=""425"" height=""344""></object>

			</body></html>", VideoId);

		}

	}

}

Open in new window

0
 
LVL 12

Author Closing Comment

by:starlite551
ID: 33659770
Great this thing works 4 me.. Can we get a list of such Video Ids on You Tube on a ListBox Control? I think it would be a good Idea to populate many Video Ids on a list box control and then play a video which we want by selecting it from the listbox control.. Overall it was a nice experience for me to remember.. Thanks a lot man! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to truncate multiple tables from SQL Server Database using Stored procedure 8 49
Problem with Filesystemobject or .NET version 3 35
SQL Login 17 40
Entity Framework 7 32
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video will demonstrate how to customize windows, tools, and control bars, and save them as screen sets. Open and resize windows: Customize the toolbar: Customize the control bar: Customize your tool selections: Your screen set is alread…
Viewers will learn how to include realistic velocity sensitivity to their Sampler instruments. Set the Vol<Vel parameter in the Filter/Global tab to your desired setting: Gather samples of hits of various intensity, and drag/drop into Velocity zon…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now