I know there are numerous web sites and blogs about this subject. Since I had to write some code for “Fred” to render on-line video, and embedding video in web pages can be somewhat fiddly to get right, I figured I’d blog the C# code for my future reference. If hosting on-line video in your web site interests you, be sure to check out Sahil Malik’s post on the subject.
Below are two examples, which create the HTML for embedding Windows Media and Quicktime sources. To use this code, compile either or both examples into your ASP.NET project (or into a referenced class library assembly), bung a PlaceHolder on your page and during the load event of your page pass the instance of your PlaceHolder control to the desired method. The location should either be a relative URL or absolute URL to the hosted video file, which can be a streaming location or an HTTP location.
Embedding Windows Media:
private void CreateMediaPlayerEmbeddedControl(PlaceHolder ph, string location, int width, int height)
{
HtmlGenericControl result = new HtmlGenericControl();
ph.Controls.Add(result);
HtmlGenericControl embed = new HtmlGenericControl();
result.Controls.Add(embed);
// Embed tag.
embed.TagName = “embed”;
embed.Attributes.Add(“type”, “application/x-mplayer2″);
embed.Attributes.Add(“pluginspage”, “http://www.microsoft.com/netshow/download/player.htm”);
embed.Attributes.Add(“name”, “mediaPlayer”);
embed.Attributes.Add(“width”, width.ToString());
embed.Attributes.Add(“height”, height.ToString());
embed.Attributes.Add(“AllowChangeDisplaySize”, “1″);
embed.Attributes.Add(“AllowSize”, “0″);
embed.Attributes.Add(“AutoSize”, “0″);
embed.Attributes.Add(“AutoStart”, “1″);
embed.Attributes.Add(“DisplaySize”, “4″);
embed.Attributes.Add(“EnableContextMenu”, “0″);
embed.Attributes.Add(“Enabled”, “1″);
embed.Attributes.Add(“InvokeURLs”, “0″);
embed.Attributes.Add(“ShowCaptioning”, “0″);
embed.Attributes.Add(“ShowStatusBar”, “0″);
embed.Attributes.Add(“ShowControls”, “1″);
embed.Attributes.Add(“WindowlessVideo”, “1″);
embed.Attributes.Add(“uiMode”, “None”);
embed.Attributes.Add(“src”, location);
// Object tag
result.TagName = “object”;
result.Attributes.Add(“classid”, “clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95″);
result.Attributes.Add(“standby”, “Loading Microsoft Windows Media Player components…”);
result.Attributes.Add(“codebase”, “http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701″);
result.Attributes.Add(“type”, “application/x-oleobject”);
result.Attributes.Add(“width”, width.ToString());
result.Attributes.Add(“height”, height.ToString());
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
}
Embedding Quicktime:
private void CreateQuickTimeEmbeddedControl(PlaceHolder ph, string location, int width, int height)
{
HtmlGenericControl result = new HtmlGenericControl();
ph.Controls.Add(result);
HtmlGenericControl embed = new HtmlGenericControl();
result.Controls.Add(embed);
// Embed tag.
embed.TagName = “embed”;
embed.Attributes.Add(“width”, width.ToString());
embed.Attributes.Add(“height”, height.ToString());
embed.Attributes.Add(“scale”, “tofit”);
embed.Attributes.Add(“autoplay”, “true”);
embed.Attributes.Add(“controller”, “true”);
embed.Attributes.Add(“target”, “myself”);
embed.Attributes.Add(“type”, bo.MimeType);
embed.Attributes.Add(“pluginspage”, “http://www.apple.com/quicktime/download/index.html”);
embed.Attributes.Add(“src”, location);
// Object tag
result.TagName = “object”;
result.Attributes.Add(“classid”, “clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B”);
result.Attributes.Add(“codebase”, “http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0″);
result.Attributes.Add(“width”, width.ToString());
result.Attributes.Add(“height”, height.ToString());
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
result.Controls.Add(new LiteralControl(“”));
}
That’s it…. now if you’re really good, you can detect the client operating system, by looking for the word “Windows” in the user agent string (hint:Request.UserAgent) and calling the correct method above.
I tested the above code using FireFox and IE (Windows and Mac). Windows Media will play in FireFox if you have the Windows Media Plugin (Windows version ships with latest version of Windows Media Player) installed, and the rendering Quicktime requires the plugin from Apple (duh).
Like this:
Like Loading...