Click here to Skip to main content
15,895,799 members
Articles / Desktop Programming / WPF

Stream YouTube Videos in WPF

Rate me:
Please Sign up or sign in to vote.
4.91/5 (68 votes)
30 Jun 2008CPOL4 min read 344.1K   12K   163  
WPF: A simple article on using WebBrowser to stream YouTube videos.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace YouViewer
{
    /// <summary>
    /// Contains a new .NET 3.5 SP1 WebBrowser control
    /// that simply navigates to the YouTube SWF file
    /// Uri
    /// </summary>
    public partial class Viewer : UserControl
    {
        #region Data
        private string videoUrl = string.Empty;
        private static bool IsExpanded = false;
        public event EventHandler ClosedEvent;
        #endregion

        #region Ctor
        public Viewer()
        {
            InitializeComponent();
        }
        #endregion

        #region Events
        /// <summary>
        /// Raised when the close button is clicked. This event
        /// is used by YouViewerMainWindow to set Opacity on its
        /// contained DragCanvas back to fully viewable Opacity
        /// </summary>
        protected virtual void OnClosedEvent(EventArgs e)
        {
            if (ClosedEvent != null)
            {
                //Invokes the delegates.
                ClosedEvent(this, e);
            }
        }
        #endregion

        #region Properties

        public string VideoUrl
        {
            set 
            {
                if (videoUrl != value)
                {
                    if (!IsExpanded)
                    {
                        videoUrl = value;
                        browser.Source = new Uri(videoUrl, UriKind.Absolute);
                        IsExpanded = true;
                        Storyboard sbEnter = this.TryFindResource("OnMouseEnter") as Storyboard;
                        if (sbEnter != null)
                        {
                            sbEnter.Completed += new EventHandler(sbEnter_Completed);
                            sbEnter.Begin(this);
                        }
                    }
                }
            }

        }
        #endregion

        #region Private Methods

        private void sbEnter_Completed(object sender, EventArgs e)
        {
            browser.Visibility = Visibility.Visible;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (IsExpanded)
            {
                browser.Visibility = Visibility.Collapsed;
                IsExpanded = false;
                browser.Source = null;
                Storyboard sbLeave = this.TryFindResource("OnMouseLeave") as Storyboard;
                if (sbLeave != null)
                {
                    sbLeave.Completed += sbLeave_Completed;     
                    sbLeave.Begin(this);
                }
            }
        }

        private void sbLeave_Completed(object sender, EventArgs e)
        {
            OnClosedEvent(new EventArgs());
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
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 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • 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

Comments and Discussions