Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#

Silverlight 4 : YouTube – Standard Feed, Search and Play videos – Tip of the day

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
28 Jun 2010CPOL4 min read 17.6K   8   2
Download: Source YouTube is the most popular and largest video sharing website on which users can upload, view and share videos.  One of the key features of YouTube is the ability to view its videos on other web pages and client applications. The YouTube APIs allows us to bring the YouTube experi

Download: Source

YouTube is the most popular and largest video sharing website on which users can upload, view and share videos.  One of the key features of YouTube is the ability to view its videos on other web pages and client applications.

The YouTube APIs allows us to bring the YouTube experience to web pages, applications and devices.

YouTube .NET client APIs can be used by authenticated and anonymous users.

  • Anonymous user : Read only standard feeds.(Most Viewed,Top Rated,Watch On Mobile, Most Discussed,Top Favourites,Most Responded, Most Recent etc)
  • Authenticated user : Public feeds, private feeds (user's inbox feed), upload, delete and update operations.

In order to user YouTube SDK, we have to perform the following initial steps

  1. Sign up for the Developer key. A Developer key uniquely identifies a product that is submitting the request. This Key should be provided in every YouTube API request
  2. Download and install .NET YouTube SDK

Default Location for the installed YouTube dlls

youtube_youtube_dll_location

The following is a sample feed that we can parse manually to get video related information or this could be done easily by using YouTube .NET client APIs (there are three dlls in the package).

youtube_atom

Example: Standard Feed - http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed

In this article i am going to walk through a silverlight out of browser app, called silverTube.

This app got following abilities:

  • Fetching standard and private feeds,
  • Perform keyword search,
  • Playing videos, and
  • Hint of pagination capabilities.

Before stating the project, lets have a look at the end product.

youtube_weding

Open the Visual Studio 2010 or Visual Web Developer 2010 Express with the Silverlight 4 tools installed.

Create a new silverlight project and name whatever you like. (here silverTube)

youtube_initial_solution_art

We already installed the YouTube SDK and now we need to have reference to them in our silverlight application. But silverlight runtime doesn’t allow to include those YouTube dlls in the project!

youtube_cant_ref_dll

There is a workaround about this issue. We will add those dlls as well as a new WCF Service to our silverTube.Web project, and then our silverlight app can easily call that web service.

Add another reference to System.Runtime.Serialization.dll. The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects.

Now add the following classes to the silverTube.Web project. Those classes will hold video meta data information.

youtube_three_classes

We will now add a Silverlight-enabled WCF Service to this project and name it as svcSilverTubeServer.svc. Here i am using Silverlight-enabled WCF service template over the standard WCF Service because it is already pre configured and ready to use with Silverlight.

Insert the following namespaces

youtube_using_namespace

 

Before start coding lets have a look at the following diagram that will explain the base concept

youtube_request_settings

 

Now we will write a private function called InitializeRequest that takes two parameters

  • prmUserName
  • prmPassword

If we provide valid Username and Password, then our intention is to get private feeds and also to perform upload, delete and update operation. Otherwise we could only get public feeds.

youtube_initialize_request

 

The following is the first public facing web service function for this project.

youtube_get_standard_youtube_feed

Our PrepareFeed method takes Atom feed collection and populate our custom collection for the later use

youtube_PrepareFeed

 

The GetYouTubeByKeyword method allows us to search videos by:

  • prmKeyword: a search query term
  • prmSafeSearch: Our intention to include all types of videos
  • prmOrderBy: Sorting our entries.
  • prmVideosBetweenTimeRange: A time period to limit standard feed results.
  • prmVideoCategory: Videos by categories.
  • prmCurrentPage: Current page no is the starting entry no.
  • prmPageSize: Maximum number of entries to return for the current page.

Have look at the method implementation. I guess its well commented.

youtube_getyoutubebykeyword

Now its time to get private feed. Authentication is required for this purpose.

 

youtube_GetPrivateFeed

At this point we done with our WCF service. Now its time to consume the service in our Silverlight application.

Right-click the silverTube project in Solution Explorer > Select Add Service Reference > Click Discover to get following screen that lists all the supported operations and give a namespace as clientYouTube

youtube_add_ref

Following XMAL is for our app that could be modified and extended by you. Based on the requirements your XMAL could be different than my one.

 youtube_xml_silvertube

ImagePathConverter class:

youtube_ivalue_converter

Code behind for this app is very straight forward. So i am not going to explain here, you better please download the source.

Now we are going to configure the application so that it can be installed for out of browser use.

Right-click the silverTube project in Solution Explorer > Silverlight > Check Enable running applications out of the browser

youtube_oob

There is one last thing we need to do is to add clientaccesspolicy.xml file in the silverTube.Web project. This configures the service to allow cross-domain access.

 

youtube_clientaccesspolicy

 

Throughout this article I tried to show you how searching and playing of YouTube videos could be done. In future i will look more into uploading and downloading YouTube videos.

Ref: http://code.google.com/apis/youtube/2.0/developers_guide_dotnet.html 

Until than Happy coding.
Sumon Barua

 Download: Source

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) Lavender
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVery nice article...very well explained Pin
Tbhavesh26-Oct-12 5:08
Tbhavesh26-Oct-12 5:08 
GeneralMy vote of 5 Pin
005vinay13-Jun-12 1:59
005vinay13-Jun-12 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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