- Using the Windows Media Player Control with Microsoft Visual Studio
- Adding the Windows Media Player Control
- Configuring Visibility of the Control
- Object Model Compatibility of the Control
- Command Line Parameters
- Command Line Parameters for Wmplayer
- Command Line Parameters for Wmpconfig
- Command Line Parameters for Wmpnscfg
- Embedding the Windows Media Player Control in a Visual Basic .NET Solution
- Add the Video Window
- Add Two Buttons and Adjust the Form
- Add the Play Code
- Add the Stop Code
- Add Error-handling
- Adding an Embedded Windows Media Player Control
- Using the Player control to display video in HTMLView content
- Using the Player object model
Using the Windows Media Player Control with Microsoft Visual Studio
You can add the Windows Media Player 9 Series or later ActiveX control to a .NET Framework application through the Toolbox in Visual Studio.
Adding the Windows Media Player Control
Before creating a new project, make sure that the latest version of Windows Media Player and the Windows Media Player SDK is installed on your computer.
Start Visual Studio, then create a new project.
In Visual Studio, open the Toolbox.
If Windows Media Player does not appear in the Components portion of the Toolbox, do the following:
Right-click within the Toolbox, and then select Choose Items. This opens the Customize Toolbox dialog box.
On the COM Components tab, select Windows Media Player.
If Windows Media Player does not appear in the list, click Browse, and then open Wmp.dll, which should be in the Windows\System32 folder.
Click OK. The Windows Media Player control will be placed on the current Toolbox tab.
You can now select Windows Media Player in the Toolbox and add it to a form.
Visual Studio gives the Windows Media Player control a default name such as «axWindowsMediaPlayer1». You may want to change the name to something more easily remembered, such as «Player».
Adding the Windows Media Player control from the Toolbox also adds references to two libraries created by Visual Studio, AxWMPLib and WMPLib. You can find them in the Solution Explorer under References.
To make using the objects in the Player namespace easier, you should include the namespace in the using or imports directives of your files, as follows:
The directive ensures that you can refer to Player objects without fully qualifying their names.
The Windows Media Player control is an AxWindowsMediaPlayer object from the AxWMPLib namespace. However, the AxWindowsMediaPlayer class uses data types, interfaces, and other elements from the WMPLib namespace.
Configuring Visibility of the Control
When you first add the Windows Media Player control to a form, it will be visible. If you do not want to use the visible image of the Player in your application, hide the default Player by setting any one of the following properties:
Property | Value |
---|---|
uiMode | «invisible» (See Player.uiMode.) |
Visible | «false» |
Size.Width | 0 |
Size.Height | 0 |
You can set these properties either in code or in the Properties window when the Windows Media Player control is selected in the form designer.
Object Model Compatibility of the Control
The object model for the Windows Media Player control is basically the same in the .NET Framework as in unmanaged code and script. However, there are differences in how elements are exposed:
- Most objects are exposed under the name of their underlying COM interface. For example, the Playlist object is exposed as IWMPPlaylist.
- Some interfaces have later versions. For example, IWMPMedia was given additional functionality in IWMPMedia2 and IWMPMedia3. If you declare an object as IWMPMedia, you usually have access to the functionality of all versions of the interface. However, IntelliSenseВ® will not recognize the methods or properties of the later-version interfaces, and the Visual Basic .NET editor will not automatically correct capitalization. To get the full benefit of IntelliSense and other features of Visual Studio, declare the object by using the latest version of the interface, such as IWMPMedia3.
- There are no indexed properties (C#) or default properties (Visual Basic .NET). For example, to retrieve a Playlist.item, you must call the IWMPlaylist.get_Item accessor method in C# or retrieve the IWMPlayist.Item property in Visual Basic .NET.
- Because of a naming conflict between the Windows Media Player Controls property and the Controls property exposed by every control, the Player version of this property is called CtlControls in the context of the ActiveX control. (However, this is not the case when you create the Player programmatically rather than as an ActiveX control.)
Use the Object Browser in Visual Studio to locate the correct API names for methods and objects in the AxWMPLib and WMPLib namespaces.
Command Line Parameters
Command Line Parameters for Wmplayer
Windows Media Player supports a set of command line parameters that specify how the Player behaves when it starts. The following table details the parameters and their behaviors.
Syntax | Behavior |
---|---|
«path\filename«(For example: wmplayer «c:\filename.wma» ) | Start the Player and play the file. |
«path\filename» /fullscreen(For example: wmplayer «c:\filename.wmv» /fullscreen ) | Play the specified file in full-screen mode.You must specify the path and file name of the content to play. |
/Device: | Play a DVD or audio CD. |
«path\filename?WMPSkin=skin name«For example: wmplayer «c:\filename.wma?wmpskin=headspace» | Open the Player, applying the specified skin. |
/Service:keyname | Open the Player showing the online store specified by keyname.Requires Windows Media Player 10 or later. |
/Task NowPlaying | Open the Player in the Now Playing feature. |
/Task MediaGuide | Open the Player in the Media Guide feature (current active online store in Windows Media Player 10 or later). |
/Task CDAudio | Open the Player in the Copy from CD feature (Rip feature in Windows Media Player 10 or Windows Media Player 11). This parameter is not supported in Windows Media Player 12. |
/Task CDWrite | Open the Player in the Burn feature.Requires Windows Media Player 10. |
/Task MediaLibrary | Open the Player in the Library feature. |
/Task RadioTuner | Open the Player in the Radio Tuner feature (current active online store in Windows Media Player 10 or later). |
/Task PortableDevice | Open the Player in the Copy to CD or Device feature (Sync feature in Windows Media Player 10 or later). |
/Task Services /Service servicename | Open the Player in the Premium Services feature, showing the service specified by the servicename parameter. This value is the unique name for the service. If the specified service has not been previously viewed, the servicename parameter is ignored. (Opens the specified online store in Windows Media Player 10 or later.) |
/Task ServiceTaskX | Open the Player in the online store service task pane specified by X. For example, /Task ServiceTask1 opens the Player in the first online store service task pane. |
/Task SkinViewer | Open the Player in the Skin Chooser feature. |
/Playlist PlaylistName | Open the Player and play the specified playlist. |
/Schema: | Open the Player, showing the specified media category. Requires Windows Media Player 11. |
Command Line Parameters for Wmpconfig
Wmpconfig.exe is used to execute certain commands in Windows Media Player that require administrator permission. Examples include the starting and stopping of browsing and sharing services and the enabling of exceptions in the Windows Firewall. The following table describes the possible values for the command line parameters.
Syntax | Behavior |
---|---|
DisableHMEDevice MAC | Disables the device specified by a Media Access Control (MAC) identifier. |
HMEOff Example: wmpconfig HMEOff | Disables the Windows Media Player Network Sharing Service. |
HMEOn Example: wmpconfig HMEOn | Enables sharing, browsing, and the firewall exception. |
RemoveHMEDevice MAC | Removes the device specified by a MAC identifier. |
RestoreHMEDevice MAC | Restores the device specified by a MAC identifier. |
SetDVDParentalLevel levelExample: wmpconfig SetDVDParentalLevel 3 | Sets the DVD parental control level. The level is specified as an integer. |
Command Line Parameters for Wmpnscfg
Microsoft Windows uses wmpnscfg.exe to alert users when media rendering devices are found on the network. Wmpnscfg starts the Windows Media Player Network Sharing Service (NSS) and then waits for notifications from the service. When wmpnscfg is notified that a new media device is available on the network, it displays a popup in the system tray that informs the user about the availability of the new device. If the user clicks the popup, wmpnscfg launches Windows Media Player, which displays a dialog box that asks the user to either allow or deny sharing with the new device.
Typically, Windows calls wmpnscfg with no command line parameters. However, there is one parameter available, described in the following table.
Embedding the Windows Media Player Control in a Visual Basic .NET Solution
To use the functionality of Windows Media Player 9 Series or later in a Visual Basic .NET application, first add the component to a form as described in Using the Windows Media Player Control with Microsoft Visual Studio
This section describes how to create an application that plays video and has custom play and stop buttons.
Add the Video Window
Add the Windows Media Player control to a form. Resize the control, and then place it where you want the video window to appear.
Select the Windows Media Player control, then change the uiMode property to «none». This setting hides the UI controls. When the user plays a video, it will appear in the window. For audio-only content, a visualization will appear.
Add Two Buttons and Adjust the Form
Now add two buttons to the form. Select the first button and change the Text property to «Play». Select the second button and change its Text property to «Stop».
Add the Play Code
Double-click the Play button to reveal the Code window. The following code is displayed:
Add this line to the subroutine:
In the preceding code example, «axWindowsMediaPlayer1» is the default name of the Windows Media Player control and «c:\mediafile.wmv» is a placeholder for the name of the media you want to play.
If you have added the digital media content from the Windows Media Player SDK to the library in Windows Media Player, you can use this code instead:
Because the autoStart property is true by default, Windows Media Player will start playing when you set the currentPlaylist or URL property.
Add the Stop Code
Double-click the Stop button to reveal the Code window. The following code is displayed:
Add this line to the subroutine:
The managed-code wrapper for the Windows Media Player control exposes the Controls object as Ctlcontrols to avoid collision with the Controls property inherited from System.Windows.Forms.Control.
Add Error-handling
The Windows Media Player control does not raise an exception when it encounters an error such as an invalid URL. Instead, the control signals an event. Your application should handle error events sent by the Player.
To create an event handler, open the code window for your form class. From the drop-down list at the top of the window, select the Windows Media Player control. A list of events appears in the drop-down list to the right. From that list, select MediaError. The following code is displayed:
The following code could be inserted in the subroutine to provide minimal error-handling capability. Note that information about the error can be retrieved from the _WMPOCXEvents_MediaErrorEvent argument.
Adding an Embedded Windows Media Player Control
There are two reasons you might consider adding an embedded instance of Windows Media Player to your HTMLView presentation. First, if you want to display video content, you need to use the Windows Media Player ActiveX control. Second, if you want to take advantage of features of the Windows Media Player object model from within your HTMLView webpage, you must use an instance the Player control to do so.
Using the Player control to display video in HTMLView content
Usually, Windows Media Player displays video using the Video and Visualization pane of the Now Playing feature. Since HTMLView uses this area to display your webpage, you must supply an additional video display area if you want the Player to play video. This is easy to do by using the Windows Media Player ActiveX control.
To use the Player control to display video, embed the control in your HTMLView webpage by using the OBJECT tag. This is the same technique you use to embed the Player control into any webpage in which you want to display video. The following example code shows the basic syntax for embedding the Player control in Internet Explorer:
The autoStart parameter ensures that content plays automatically whenever a new URL is specified. The value you specify for uiMode is up to you, but you will usually want to specify «none» when creating content for HTMLView presentations. When you embed the Windows Media Player control to display video in this manner, the user can control playback using the controls of the full-mode Player, so there’s no need to provide additional transport controls in the webpage. You can use the space you would usually allocate for transport controls to display more text, graphics, or links to other content.
Do not specify a URL parameter when embedding the Windows Media Player control in a webpage designed to display in an HTMLView presentation. Instead, specify the digital media files in the .asx file that opens the content.
Because you provide the video display region in your HTMLView webpage, you can decide where to position the video and how large you want the display region to be. For example, you can contain the Player object within an HTML DIV element and then specify the position for the DIV to situate the video display on the webpage. You can change the dimensions of the video display by specifying values for the height and width attributes of the OBJECT element. You can also specify these values using script code.
Using the Player object model
The Windows Media Player object model exposes properties, methods, and events that you can use in your HTMLView webpages. When you embed the Windows Media Player ActiveX control in your HTMLView webpage, you automatically have access to the Player object model.
If you embed the Windows Media Player control in your HTMLView webpage, do not use the Player object model to specify the digital media file to be played. For example, if you use script code to specify a value for the URL property of the embedded control, your HTMLView webpage will be unloaded from the Now Playing feature when the digital media file plays. To prevent this from happening, always open .asx files that include HTMLView parameters when you need to use script to open digital media content from your HTMLView webpage.