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
- 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
- Download and install .NET YouTube SDK
Default Location for the installed YouTube dlls
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).
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.
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)
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!
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.
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
Before start coding lets have a look at the following diagram that will explain the base concept
Now we will write a private function called InitializeRequest that takes two parameters
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.
The following is the first public facing web service function for this project.
Our PrepareFeed method takes Atom feed collection and populate our custom collection for the later use
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.
Now its time to get private feed. Authentication is required for this purpose.
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
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.
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
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.
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.
Until than Happy coding.