Click here to Skip to main content
6,915,647 members and growing! (17,328 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » General     Beginner License: The Code Project Open License (CPOL)

Stream YouTube Videos In WPF

By Sacha Barber

WPF : A simple article on using WebBrowser to Stream YouTube videos
C# (C#3.0), .NET (.NET3.5), WPF, Architect, Dev, Design
Posted:19 Jun 2008
Updated:30 Jun 2008
Views:51,524
Bookmarked:85 times
Unedited contribution
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
39 votes for this article.
Popularity: 7.63 Rating: 4.80 out of 5
1 vote, 2.6%
1

2

3
2 votes, 5.1%
4
36 votes, 92.3%
5

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sacha Barber


Member
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 74 (Total in Forum: 74) (Refresh)FirstPrevNext
GeneralVB.NET ver PinmemberBernard_Yanga8:15 5 Feb '10  
GeneralRe: VB.NET ver PinmvpSacha Barber21:58 5 Feb '10  
GeneralWindows 7 problem Pinmemberfanoo22:16 4 Aug '09  
GeneralRe: Windows 7 problem PinmvpSacha Barber22:46 4 Aug '09  
GeneralRe: Windows 7 problem Pinmemberfanoo10:14 5 Aug '09  
GeneralRe: Windows 7 problem PinmvpSacha Barber10:34 5 Aug '09  
GeneralRe: Windows 7 problem Pinmemberfanoo3:08 22 Aug '09  
GeneralRe: Windows 7 problem PinmvpSacha Barber0:46 23 Aug '09  
Generalfile download security warning PinmemberMember 473915513: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 PinmvpSacha Barber22:43 13 Jul '09  
GeneralRe: file download security warning PinmemberMember 47391557:33 15 Jul '09  
GeneralRe: file download security warning PinmemberMember 47391557:53 15 Jul '09  
GeneralRe: file download security warning PinmvpSacha Barber11:05 15 Jul '09  
GeneralRe: file download security warning PinmemberMember 473915511:21 15 Jul '09  
GeneralRe: file download security warning PinmvpSacha Barber11:50 15 Jul '09  
QuestionRenderTrasform on the WebBrowser containing the player PinmemberBLATEER8:01 14 Jun '09  
AnswerRe: RenderTrasform on the WebBrowser containing the player PinmvpSacha Barber22:36 14 Jun '09  
GeneralVista 64 IE7 problem Pinmembermfmanca14:21 26 Feb '09  
GeneralRe: Vista 64 IE7 problem PinmvpSacha Barber12:00 1 Mar '09  
Generalstreaming videos PinmemberNicko Satria Utama6:03 16 Jan '09  
GeneralMerry Christmas PinmemberAbhijit Jana4:11 24 Dec '08  
GeneralRe: Merry Christmas PinmvpSacha Barber6:55 24 Dec '08  
GeneralThanks Sacha Pinmemberzues_bigglo6:52 23 Dec '08  
GeneralRe: Thanks Sacha PinmvpSacha Barber23:06 23 Dec '08  
GeneralPlay SWF in WPF Pinmemberctrlnick10:25 14 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jun 2008
Editor:
Copyright 2008 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project