Home > Articles > Programming > Windows Programming

Using Windows Media Player with .NET

Toggle Open Article Table of ContentsContents

Close Table of ContentsContents

  1. What Do You Want To Watch Today?
  2. Controlling the Player
  3. Conclusion
Close Table of Contents
  • Print Print
  • Share ThisShare This
  • DiscussDiscuss
Create vibrant applications to entertain, educate, or titillate users. With just a little .NET work, you can plug into the Windows Media Player to create a rich experience for your users.

The short attention span of television watchers is as important to your applications as it is to the TV networks, with multimedia applications becoming more the rule than the exception. Users increasingly expect slick, media-rich applications. So how do you jump on the media train without becoming an expert in codecs, streaming, and compression technologies? Well, let me show you.

What Do You Want To Watch Today?

To get started, you need Windows Media Player 9.0 installed on your development machine. You also need the Windows Media Player SDK.

NOTE

Be sure to get the Windows Media Player SDK, not the Windows Media Services SDK; they're very different beasts.

After you download the SDK, install it anywhere that's convenient. The SDK installs a couple of interesting features:

  • Primary Interop Assemblies (PIA) for the Windows Media Player

  • Windows Media Player control for use in Visual Studio

Now that the stage is set, let's create your first media-savvy application.

Writing Your First Media Application

We'll start with a blank Windows application project and drop the Windows Media Player control on the form. The Windows Media Player control should be in the Windows Forms section of the toolbox (see Figure 1). If it isn't there, right-click the toolbox and select Add/Remove Items. In the Customize Toolbox dialog box, click the COM Components tab, scroll down to the end of the list, and select the control to add it to the toolbox (see Figure 2).

Figure 1Figure 1 The Windows Media Player control.

Figure 2Figure 2 The Customize Toolbox dialog box.

Now that the control is available, drag it onto the form so you can get started working with it. When you add the control to the form, AxWMPLib (the control) and WMPLib (the core library) are added as references. Both are required.

Not surprisingly, the control looks like the guts of the Windows Media Player, as shown in Figure 3.

Figure 3Figure 3 Our form with the control in place.

For a simple example, let's play an arbitrary media file in the form's Load event. To add a handler for this event, double-click the form's title bar. In the handler, write a single line to play the file:

void Form1_Load(object sender, EventArgs e)
{
 axWindowsMediaPlayer1.URL = @"c:\writing\wmp .net\sample\sample.avi";
}

Setting the URL property causes the control to load and play the selected file or URL. Our new media player application looks like Figure 4.

Figure 4Figure 4 The player in action.

TIP

A complete reference to the control and all its methods, events, and properties is available on MSDN.

  • Share ThisShare This
  • Save To Your Account

Discussions

comments powered by Disqus