Embedding Windows Media and Quicktime into ASP.NET pages

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).

15 thoughts on “Embedding Windows Media and Quicktime into ASP.NET pages

  1. Sahil Malik

    Y’know we shudda called the project Margaret. That way we could say,
    <br>
    <br>Margaret is such a bitch!!! Margaret has been throwing tantrums lately. Margaret has been keeping me awake for long hours. Sorry darling I’ll be late from work – Margaret needs me.
    <br>
    <br>.. and so on.

  2. http://

    Umm…. uiMode mini doesn’t work for embed in FF 2.0 or Opera, any way to fix it?

  3. robgarrett

    Sergei,
    Since writing this code I implemented a version using control adapters (unfortunately I cannot post it because it’s copyrighted by my employer), you might want to try that route with the code above to implement control versions to target different platforms.

    R.

  4. Lee

    Hi Rob

    In your sample code, what is the object called bo, and where does it get initialized?

    “The name ‘bo’ does not exist in the current context”

    Thanks

    Lee.

  5. robgarrett

    Lee,
    Oops, BO is a business object I used in the application in which I pulled this code. Ignore it in the context of this example and pass a string for the mime-type.

    R.

  6. http://

    Thanks Rob but I’d like to ask how could you modify the code so that the windows media player could display visualization?

  7. http://

    nice … How i can pre-activate media player or quick time player in internet explorer since IE requires a user to click on it?

  8. http://

    Thanks! solved my problem with a client that is not ready for Silverlight- In fact, he liked the setup so well he wanted more. Now I need to use the “SendPlayStateChangeEvents” to put some time based verbage out to the side of the video. So, where/how can I pick it up when it fires? I used the test code for “playing”-“not playing” , but I keep getting the error -‘System.Web.UI.AttributeCollection’ does not contain a definition for ‘playState’…..
    Any obvious ideas? I already have a day messing around with it, any help is GREATLY appreciated

  9. roy

    i know thisis pretty old but im trying to use it using .net 3.5 and i get the player to load but i get the qplayer icon with a question mark in the middle of the player .. ive tried both absolute and relative links .. what am i doing wrong? can anyone give me an example url of someting that should work?

Comments are closed.