| « Silverlight + MediaSuite = SilverSuite | MediaSuite.NET - What's new » |
I thought I'd share a simple way to play Youtube videos from the MediaSuite FilePlayer demo application.
When opening a video on youtube the url has following form:
http://www.youtube.com/watch?v=[uniqueID]
From that the server generates an HTML page that includes the parameters to be passed to the video players swf file. Using those parameters we can create a new URL to www.youtube.com that will retrieve an FLV file for us.
Simply add another button to the toolbar:
Double-Click it to create an event and insert following code:
Code:
System.IO.Stream stream; | |
System.Net.WebClient webclient; | |
webclient = new System.Net.WebClient(); | |
webclient.UseDefaultCredentials = true; | |
webclient.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;)"); | |
try | |
{ | |
stream = webclient.OpenRead(toolStripTextBox2.Text); | |
} | |
catch (System.Exception) | |
{ | |
return; | |
} | |
| |
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream); | |
string httpTxt = ""; | |
byte[] buffer = new byte[1024]; | |
while(true) | |
{ | |
int read = reader.Read(buffer, 0, 1024); | |
| |
if(read == 0) | |
break; | |
| |
httpTxt += System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, read); | |
| |
} | |
| |
if (string.IsNullOrEmpty(httpTxt)) | |
return; | |
string par ="watch_fullscreen?"; | |
int fullscrPos = httpTxt.IndexOf(par); | |
if (fullscrPos == -1) | |
return; | |
| |
int endQuote = httpTxt.Substring(fullscrPos + par.Length).IndexOf("';"); | |
| |
if (endQuote == -1) | |
return; | |
| |
int startpos = fullscrPos + par.Length; | |
string newurl = string.Format("http://www.youtube.com/get_video.php?{0}", httpTxt.Substring(startpos, endQuote + 1)); | |
| |
toolStripTextBox2.Text = newurl; | |
toolStripButton2_Click(sender, e); |
Here's the end-result for Steve Ballmer going crazy:
