A cross-browser Windows Media Player component for ASP.NET 2.0
Dot net February 4th, 2008ASP.NET 2.0 is a very powerful, extensible web framework. Its component model allows for very efficient re-use of code and user interface designs.
As Microsoft created the entire framework, you might reasonably expect the default ‘Toolbox’ to offer you a wide range of components to integrate with other Microsoft technologies. Technologies such as Windows Media Player, MSN Messenger, Movie Maker or other client-side applications. Alas, this is not the case.
Out of the box, there is no support for adding a Windows Media Player to your ASP.NET website. When I was searching around the Internet, I managed to find a custom control that had been quickly put together. (Unfortunately I can not find the source, so if you know of it, please contact me and I’ll add an appropriate reference). That simple UserControl enabled the embedding of the Windows Media Player client into an ASP.NET web page.
However, the fatal flaw of this component is that it only works in Internet Explorer. Users of all other browsers are left in the dark, staring at a blank page.
With a few modifications, the component can be modified to output the necessary <embed> tags as well as the IE-only <object> tags.
Click to download the source code to WMVPlayer.cs
Example of use
The easiest way to use the control is to add it to your App_Code directory, rebuild your solution, and then right-click the Toolbox and select “add component”. In the dialog that appears, select the WindowsMediaPlayerComponent from the list.
You can then drag an embedded Windows Media Player onto your page in Design View. Don’t forget to adjust the width and height, and set the content URL using the normal properties view.
To change the content in a listener, simply manipulate the control’s “Filename” property. This is particularly useful when you have several movies or streams you want users to access from the same page. The following code shows the code behind for a simple use case:
public class MediaPage: System.Web.UI.Page {
private WMVPlayer player = new WMVPlayer();
public void Page_Load(object sender, System.EventArgs args)
{// Check for December (datetime months are zero-based)
if (DateTime.Now.Month == 11)
player.Filename = “http://www.christmas.com/made-up-url.wmv”;
else
player.Filename = “http://www.myfavourite.com/fake-movie.wmv”;
}
}
So download the component, and send me a link to your electronic programme guide when you’ve built it.
Other Articles:

February 18th, 2008 at 11:19 am
How do i add this item manually, without the toolbox panel?
What should I write in the .aspx file?
Nisim
February 25th, 2008 at 1:02 pm
Hi Nisim,
Once you have added the WMVPlayer.cs file to your App_Code directory (and rebuilt your website), you only need to add the following lines to your aspx to manually enable the media player:
< %@Register Namespace="Backbone.UserControls" TagPrefix="cc" %>
<cc:MediaPlayer ID=”Player1″ runat=”server” Filename=”test.wmv” Width=”500″ Height=”400″ />
The Register directive can go at the top of your ASPX file, the other line goes where you want the player to appear.
Lee.