Click here to Skip to main content
Email Password   helpLost your password?

Introduction

This is going to be a quite small article, and probably not that useful, but I think its quite cool all the same. Basically this article shows how to play streaming videos from YouTube in WPF.

Thats it.

So if this doesn't float your boat I'd suggest stop reading right here.

This article also reads from an RSS feed.....I know again, god you are always doing that Sacha. Well I promise I have other stuff, it's just that I like images and videos in WPF apps, and RSS feeds give you a nice diverse selection.

Prerequisities

This article does rely on 1 peice of software being installed, which is as follows:

Microsoft .NET Framework 3.5 Service pack 1 Beta

This is required for the new WebBrowser control that comes with the SP1 installation

Ok So What Does It Look Like

The app when running is yet another of my RSS feed search type apps. I promise this is the very last one of these. I just had to get this YouTube thing out of my system, so there you heard me say it "NO MORE RSS FEED WPF ARTICLES!!!!!". Never. Never. I'm done with those.

Here are a couple of screen shots assuming you clicked the Most Popular button.

You are free to drag the YouTube feed items around, providing you are currently in Drag mode. Where the Mode is changable using the right click context menu.

When you toggle out of Drag mode, you will be in Play mode, so will no longer be able to move the video items around. Instead when you move the mouse over an video item you will see a PLAY icon appear.

When you then click this PLAY icon a new video viewer will be shown where you can view the YouTube video. This window is animated into/out of view. You should be able to open the viewer window whenever you are in Play mode and click on a unique video item.

So that's the screen shots done with, so how does it all work.

Hows It Work

Well starting at the top, the RSS feed, this is actually just a bit of XLINQ, as follows

var xraw = XElement.Load(string.Format(SEARCH,keyWord));
var xroot = XElement.Parse(xraw.ToString());
var links = (from item in xroot.Element("channel").Descendants("item")
             select new YouTubeInfo
             {
                 LinkUrl = item.Element("link").Value,
                 EmbedUrl = GetEmbedUrlFromLink(item.Element("link").Value),
                 ThumnailUrl =
                    item.Elements().Where(
                            child => child.Name.ToString().Contains("thumbnail")
                        ).Single().Attribute("url").Value

             }).Take(20);

return links.ToList<YouTubeInfo>();

So this is used within the YouViewerMainWindow to create a bunch of YouTubeResultControl controls, which are then added to a DragCanvas. I can take no credit for the DragCanvas, I stole that straight from Josh Smith, using this code

There is nothing special to say about the YouTubeResultControl controls, they are fairly simply controls, that simply contain a single YouTubeInfo item which is used within an event that is raised when the user clicks the internal YouTubeResultControl controls play button. The YouViewerMainWindow uses the YouTubeInfo item to pass to the Viewer control, which in turn is resposible for playing the actual video.

So far nothing special right, all very easy stuff.

The only part that's a bit interesting is that we can play the YouTube video in the new WebBrowser control. This is neat.

How does this work. Normally WPF only lets you play Windows Media Player supported files that are loca,l or MMS prefixed streams.

YouTube is neither of these, so how does it work. Well luckily the RSS feed contains enough information for us to do some string manipulation to get a new Url, that points to something much more interesting.

Basically from the RSS feed we can get the following string

http://youtube.com/?v=FhZ-HsiS8aI

but if we mess around with it a bit we can get

http://www.youtube.com/v/FhZ-HsiS8aI&hl=en

Which is a link to a SWF (Flash) file, that will play directly in the new browser if you past this in to a browser address bar. Aha.

So we can use this new Url and use that as the Source property for the new .NET 3.5 SP1 WebBrowser control, and we get the usual YouTube player we are used to, for free. Neato. I also tried this with the .NET 2.0 WinForms WebBrowser (interop so WindowsIntegration Dll required) control, and the WPF Frame control but they didn't work like the new .NET 3.5 SP1 WebBrowser control.

I had originally wanted to use the .NET 3.5 SP1 WebBrowser control, within my latest 3D WPF article MarsaX, but that didn't work on account of the fact that the new .NET 3.5 SP1 WebBrowser control is actually a HWnd based control, and really not much like a WPF control. So it doesn't play nice like other WPF controls, which is a shame but it has led to this article.

And you know what that's it.

As I say this was a very quick and small article, so I don't expect too many votes for this one...But if you feel you want to vote, feel free, thanks.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralVB.NET ver
Bernard_Yanga
8:15 5 Feb '10  
is there a VB.NET version of this tnx Smile
GeneralRe: VB.NET ver
Sacha Barber
21:58 5 Feb '10  
no, but there are plenty of online code converters
Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralWindows 7 problem
fanoo
22:16 4 Aug '09  
When i ran you app in windows 7 build 7100 the app ask me to download the file.
GeneralRe: Windows 7 problem
Sacha Barber
22:46 4 Aug '09  
I dont have Windows 7 so cant test this, you will need to debug the code yourself. Have fun

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralRe: Windows 7 problem
fanoo
10:14 5 Aug '09  
No code to debug, it's an IE8 security warning when you paste
http://www.youtube.com/v/FhZ-HsiS8aI&hl=en
a message box that ask you if you want to download, open or cancel file. If you ask open no result... you need to revalidate url in the browser!

Do you know a solution to this issue ? or a work arround ?

Works fine in xp sp2 sp3, vista sp1 + ie7
GeneralRe: Windows 7 problem
Sacha Barber
10:34 5 Aug '09  
No work around Im afraid, see the next post down, they may have answer. Sorry

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralRe: Windows 7 problem
fanoo
3:08 22 Aug '09  
For 7 use NavigateToString and put in this string object and embed markup with swf url and no security warnigs apears.
Works fine now.
GeneralRe: Windows 7 problem
Sacha Barber
0:46 23 Aug '09  
Cool

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

Generalfile download security warning
Member 4739155
13:56 13 Jul '09  
when i click a video to play in this application i get a popup file download security warning with "Do you want to open or save this file?"

after i click on "run", the html page displayed reads "navigation to web page cancelled" with a link to refresh the page.

after click refresh then the youtube video is available, but you still have to click play.

i have the application set to full trust and the latest shock player download for IE.

Any suggestions very much appreciated.
GeneralRe: file download security warning
Sacha Barber
22:43 13 Jul '09  
Mmmmm the only thing I could think to try, is debug the code, see what Urls come back, copy one of them, and try that directly in browser, perhaps its a browser security thing.

Works for me though.

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralRe: file download security warning
Member 4739155
7:33 15 Jul '09  
Thanks very much for your reply. I did paste the e.Info.EmbedUrl directly in IE 8 and it produces the security popup warning for Flash; Even though youTube listed as trusted site with min security in IE security settings.

Not sure if its a bug or something freaky with my system, but i have seen a few posts around the web with people having similar problem and contemplating jumping in front of a moving train after some time.

cheers!
GeneralRe: file download security warning
Member 4739155
7:53 15 Jul '09  
don't know if you are running IE8,but i found this. others having same issue.

http://www.eggheadcafe.com/conversation.aspx?messageid=34246456&threadid=34230179[^]
GeneralRe: file download security warning
Sacha Barber
11:05 15 Jul '09  
OK, actually I use Chrome.

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralRe: file download security warning
Member 4739155
11:21 15 Jul '09  
sorry to be dense here, but i thought the web browser control was a wrapper around Internet Explorer and used the security settings from IE. If I could totally remove IE and still use the web browser control, that would be a good day.
GeneralRe: file download security warning
Sacha Barber
11:50 15 Jul '09  
No you are correct, I just thought you wanted to know browser I use, which is Chrome, but that WebBrowser control uses IE security I believe.

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

QuestionRenderTrasform on the WebBrowser containing the player
BLATEER
8:01 14 Jun '09  
Thanks, it's simple and works well except when I apply a RotatTransform to the browser. the player never rotates with its container. is there another solution?? I need all transforms cause I'm designing a tabletop interface. the player can be rotated to any direction.
AnswerRe: RenderTrasform on the WebBrowser containing the player
Sacha Barber
22:36 14 Jun '09  
Yeah that wont work the WPF Browser control is a HWnd based control and is only just a wrapper around the winforms one, as such it does not respect WPF things like transforms.

It sucks but there you go.

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralVista 64 IE7 problem
mfmanca
14:21 26 Feb '09  
When trying to use the Viewer alone in another xaml application, when I try to open a youtube video the webBrowser is asking to open/save the swf file...
What am I missing?
GeneralRe: Vista 64 IE7 problem
Sacha Barber
12:00 1 Mar '09  
mfmanca wrote:
What am I missing?


Nothing as far as I know, though I do not have the same Vista version, so could be something strange with that. I cant repeat it sorry.

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

Generalstreaming videos
Nicko Satria Utama
6:03 16 Jan '09  
I think you use your own codecs and embed it into WPF mechanism so it can play directly the flv file Smile
GeneralMerry Christmas
Abhijit Jana
4:11 24 Dec '08  
MERRY CHRISTMAS !!

------------------------------------------------------
Wishing A Merry Christmas to my GURU and his Family!!!
------------------------------------------------------

cheers,
Abhijit

GeneralRe: Merry Christmas
Sacha Barber
6:55 24 Dec '08  
Ah thanks man, thats very nice.

You have a great one and all.

Sacha Barber
  • Microsoft Visual C# MVP 2008
  • Codeproject MVP 2008
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralThanks Sacha
zues_bigglo
6:52 23 Dec '08  
I have used a few of your articles and all I have to say is thanks so much for sharing your work to allow others to learn from it!
GeneralRe: Thanks Sacha
Sacha Barber
23:06 23 Dec '08  
Hey no worries glad even the king of gods (zues) can learn from me, thats pretty cool.

To be honest I am kinda hyper active and NEED TO DO stuff ALL the time, so articles help me focus my wandering attention span.

Sacha Barber
  • Microsoft Visual C# MVP 2008
  • Codeproject MVP 2008
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralPlay SWF in WPF
ctrlnick
10:25 14 Nov '08  
Hi Sacha,
Can you suggest me how to play swf files in WPF code?

Thanks

Happy Programming!

Regards,
ctrlnick !


Last Updated 30 Jun 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010