This post describes how to build an object tag for Windows Media Player that will work on multiple browsers. Recently while writing some code for Windows Media Player, I took a walk down Google lane and came across an article on A List Apart by Elizabeth Castro describing how to code video player Object tags without resorting to the non-standards compliant embed tag. Starting with the code in the article and then tweaking some parameters to generate 1 set of output for Internet Explorer and 1 set for “everyone else”, I was able to generate an object tag that worked in the following browsers: Internet Explorer 7, Firefox 3.0.10, Opera 9.64, Safari 4.0, Google Chrome 2.0 and Flock 2.5 (all running on Windows XP, sorry – I am too poor to buy a Mac). This MSDN article describing how Windows Media Player PARAM tags work in Firefox and Internet Explorer was also helpful, and would be useful reference if you use this code as the basis for more complex coding.
Click here to see the Live Example
Here’s the code, it’s just a page and code-behind based on the conditional display of Panels to render the Object tag depending on the browser type (i.e. if it’s Internet Explorer or not).
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WindowsMediaPlayerExample.aspx.cs" Inherits="WindowsMediaPlayerExample" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Mike Knowles - Windows Media Player Multiple Browser Example</title> <style type="text/css"> body { background-color:#EFEFEF; color:#01010F; font-family:Verdana, "lucida grande", sans-serif; font-size:1em; padding-left:54px; line-height:1.4em; } </style> </head> <body> <form id="form1" runat="server"> <div style="width:540px; padding-top:18px;"> <p>This example demonstrates how to build a Windows Media Player object tag that plays on multiple browsers.</p> <p>For more detail, refer to the <a href="">blog post on this topic</a>.</p> <asp:Panel ID="InternetExplorerPanel" Visible="false" runat="server"> <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-ms-wmp" width="500" height="330"> <param name="url" value="http://silverlight.services.live.com/60107/HD%20Future%20Markers/video.wmv" /> <param name="autoStart" value="true" /> <p>You must install <a href="http://www.microsoft.com/windows/windowsmedia/player/download/" target="_blank">Windows Media Player</a> to view this content.</p> </object> </asp:Panel> <asp:Panel ID="DefaultPanel" Visible="false" runat="server"> <object type="application/x-ms-wmp" width="500" height="330" data="http://silverlight.services.live.com/60107/HD%20Future%20Markers/video.wmv"> <param name="src" value="http://silverlight.services.live.com/60107/HD%20Future%20Markers/video.wmv" /> <param name="autoStart" value="true" /> <p>You must install <a href="http://www.microsoft.com/windows/windowsmedia/player/download/" target="_blank">Windows Media Player</a> to view this content.</p> </object> </asp:Panel> </div> </form> </body> </html>
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class WindowsMediaPlayerExample : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.UserAgent.Contains("MSIE")) { this.InternetExplorerPanel.Visible = true; this.DefaultPanel.Visible = false; } else { this.DefaultPanel.Visible = true; this.InternetExplorerPanel.Visible = false; } } }