If you want to integrate your youtube videos to your C#.Net windows based application, we don’t need to know about youtube sdk or google api. When I see that google api is not very easy to handle, I needed to customize my own code.
The summary and the key of this post is; http://gdata.youtube.com/feeds/api/users/username/favorites
If you change your username with the link above and navigate via your browser you will achive the source of your favorite videos on youtube withouth sign in to your google account. It is very useful if you don’t need to add or remove any videos from your favorite list, but just wanna view them.
You need to add below to your reference via code.
using System.IO; using System.Net;
I need to get the source code from web. You know what I am talking about (http://gdata.youtube.com/feeds/api/users/username/favorites).
public string DownloadWebPage(string Url) { // Bağlantıyı Aç HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url); // Sunucudan cevap iste WebResponse Response = WebRequestObject.GetResponse(); // Veri akışı yarat Stream WebStream = Response.GetResponseStream(); StreamReader Reader = new StreamReader(WebStream); string PageContent = Reader.ReadToEnd(); // Tuvaletin ışığını kapat Reader.Close(); WebStream.Close(); Response.Close(); return PageContent; }
Before talking about other details, it is needed to udnerstand the windows design.When form is loaded, the buttons backgrounds will be the thumbnail images of your favorite videos. The listbox is not important for design. In normally its visibility will be false. I just using it to list the video ids of my favorite videos. And I am using this list programmatically add these video ids to my buttons.
All of action is over in the load event of project, just I pasted my code.
private void youtube_Load(object sender, EventArgs e) { int index = 0; string id = ""; string icerik = DownloadWebPage("http://gdata.youtube.com/feeds/api/users/KULLANICIADI/favorites"); for (int k = 0; k < 7; k++) { try { int baslangic = icerik.IndexOf(@"<media:player url='http://www.youtube.com/watch?v=", index); index = baslangic; int bitis = icerik.IndexOf(@"&feature=youtube_gdata", index); index = bitis; id = icerik.Substring(baslangic + 50, bitis - baslangic - 50); listBox1.Items.Add(id); } catch { } } WebClient indir = new WebClient(); for (int k = 0; k < listBox1.Items.Count; k++) { indir.DownloadFile("http://i.ytimg.com/vi/" + listBox1.Items[k] + "/0.jpg", k+".jpg"); if (k == 0 && k < listBox1.Items.Count) { button1.BackgroundImage = Image.FromFile(k + ".jpg"); button1.Enabled = true; } if (k == 1 && k < listBox1.Items.Count) { button2.BackgroundImage = Image.FromFile(k + ".jpg"); button2.Enabled = true; } if (k == 2 && k < listBox1.Items.Count) { button3.BackgroundImage = Image.FromFile(k + ".jpg"); button3.Enabled = true; } if (k == 3 && k < listBox1.Items.Count) { button4.BackgroundImage = Image.FromFile(k + ".jpg"); button4.Enabled = true; } if (k == 4 && k < listBox1.Items.Count) { button5.BackgroundImage = Image.FromFile(k + ".jpg"); button5.Enabled = true; } } }
By using the function “downloadwebpage”, we get the source of the webpage to a stream, and by using a reader to a string. After that by using string funtions I get all of the video ids of my favorite videos. Video id is just enough because, if you know the video id of any video on youtube, you can easily embed it and get the thumnail image of this video.
If you want to get a small thumnail image of a video with id 1234567, you can use the link below easliy to get it. ==> http://i.ytimg.com/vi/1234567/0.jpg
If you want to use the bigger version ==> http://i.ytimg.com/vi/1234567/1.jpg
private void Button1_Click(object sender, EventArgs e) { webBrowser1.Navigate("http://www.youtube.com/embed/" + listBox1.Items[0].ToString()); }
We filled the buttons background images with the thumbnail images. Now when we click them we need to navigate to video page without ads, other video links etc. The easiest way to achive it to use embed function of youtube. Again if you know the video id of a video you like to embed, you can easily use the link below;
https://youtube.com/watch?v=video_id
I need to clarify something that I didn’t mention above. To make a background image from web content for a button in your windows based project is not easy. First you need to download it from web then you can use that image from local. You can understand it by looking my code in form_load action for second.
Result is you get a basic youtube favorite video viewer by not using youtube or google apis easily.
2 Pingbacks