Embedding YouTube videos in Silverlight

Posted in Programming,Silverlight by ary on the September 11th, 2008

There's no straight way of embedding YouTube videos in Silverlight. What you can do, however, is to create a floating div over the Silverlight plugin, whose content will be the YouTube video control. For this purpose, I wrote a simple class to make it easier to do this.

The usage is simple:

// ZCcedd9EfHI is the id of the video to show
YouTubePlayer player = new YouTubePlayer("ZCcedd9EfHI")
{
    Top = 100,
    Left = 200,
    Width = 400,
    Height = 400
};

player.Embed();

// And, once you want to remove the video from the page...
player.Dispose();

That's it. Of course, you'll have to compute the Top, Left, Width and Height values if you want to center the video inside a particular Silverlight control. You can do this computation in the Loaded event of that control.

And the best thing is, you can save a reference to the YouTubePlayer instance and, in case the Silverlight plugin is resized or moved, changing the Top, Left, Width and Height properties will automatically execute javascript code to move/resize the player.

Here’s the full code in case someone finds it useful.

(+) Show Code

9 Responses to 'Embedding YouTube videos in Silverlight'

Subscribe to comments with RSS or TrackBack to 'Embedding YouTube videos in Silverlight'.

  1. Lance Morgan said,

    on December 30th, 2008 at 6:48 pm

    Hi Ary

    Fantasic post works for me, but I am unable to position it inside a silverlight control, currently just floats to bottom of screen.

    Lance

  2. Ary Borenszweig said,

    on December 30th, 2008 at 8:53 pm

    Hi Lance,

    Once I thought I got it working, it seems it doesn't work in IE. However, it works if you set the windowless parameter of the Silverlight control to true, and the background to transparent. The only drawback of doing this is a performance penalty.

  3. Eldar said,

    on February 18th, 2009 at 2:09 pm

    Unfortunately this is the only way to do this w/ silverlight. I did something similar... encapsulated it in a usercontrol and implemented youtube's api so I can work w/ it declaratively in xaml. Works great in IE; having minor dhtml inconsistency and incompatibility issues w/ firefox and chrome. Still under development but you can preview it's usage at http://youballer.com.

  4. slyi said,

    on April 18th, 2009 at 3:57 pm

    SL 3.0 it works without flash http://slyi.silverlight.googlepages.com/youtube2.html

  5. malla said,

    on April 21st, 2009 at 6:42 pm

    Hey Eldar,
    can you please tell me how you did that in your website, i mean embed youtube on silverlight. i am new to this platform , please do help me .
    looking forward for your reply .

    Thank you
    malla

  6. Sam said,

    on June 8th, 2009 at 11:54 am

    Slyi,

    Your post is great. Could you give me the XAML code and XAML.vb code for this one

  7. Ary Borenszweig said,

    on June 8th, 2009 at 5:17 pm

    Hi Sam,

    There's no XAML code for this. You embed the player with code. I don't know VB, but I'm sure there are tools out there to convert C# code to VB.

  8. slyi said,

    on July 22nd, 2009 at 5:54 am

    I just used http://www.silverlight-blog.it/ontheroad/videoplayer/minoplayer_ver1_2.html and used a youtube proxy eg: VideoSource= http://ipod5.livebuster.com/fix.php?v=iYGuMUdoFGM , as SL3 now natively support mp4.

  9. Eldar said,

    on January 6th, 2011 at 1:01 pm

    Hi Malla.

    Sorry for the late-late response. I worked on different things since my post and didn't come back to this development until now. The site is now called wusic (web music) and its source code, including my video and youtube controls, is available at http://wusic.codeplex.com.

    Hopefully this response finds you well.

    Cheers.

    Eldar

Leave a Reply