- 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
- Player.ScriptCommand event
- Syntax
- Parameters
- Return value
- Remarks
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.
Player.ScriptCommand event
The ScriptCommand event occurs when a synchronized command or URL is received.
Syntax
Parameters
String specifying the type of script command.
String specifying the script command.
Return value
This event does not return a value.
Remarks
Commands can be embedded among the sounds and images of a Windows Media file or stream. The commands are a pair of Unicode strings associated with a designated time in the stream. When the stream reaches the time associated with the command, the Windows Media Player control sends a ScriptCommand event with two parameters. One parameter specifies the type of command being sent, and the other parameter specifies the command. The type of parameter is used to determine how the command parameter is processed. Any type of command can be embedded in a file or stream to be handled by the ScriptCommand event.
The following table lists script command types that are automatically processed by Windows Media Player.
Type | Description |
---|---|
CAPTION | The control displays the associated text in the DIV specified by ClosedCaption.captioningID. |
EVENT | Tells the control to execute instructions defined for the specified event. |
FILENAME | The control resets its URL property, attempts to open the specified file, and begins playing the new stream immediately. |
OPENEVENT | Buffers the associated EVENT type command for timely execution of the EVENT script. |
SYNCHRONIZEDLYRICLYRIC | The Param parameter contains the synchronized lyric text. Windows Media Player displays the lyric text in the closed caption area of the Now Playing feature. |
TEXT | The control displays the associated text in the DIV specified by ClosedCaption.captioningID. |
URL | The control automatically opens the URL specified using the default Internet browser if the Settings.invokeURLs property is set to true. |
You can embed any other type of command as long as you provide reciprocal code to handle the command. Though unknown commands are ignored by the Windows Media Player control, they are still handed off to the ScriptCommand event.
URL commands received by the Windows Media Player control are invoked automatically in your default Web browser if the Settings.invokeURLs property is set to true. You can use the Settings.defaultFrame property to specify the target frame in which the webpage appears.
The URL sent to Windows Media Player is processed relative to the base URL specified by the Settings.baseURL property. The base URL is concatenated with the relatively specified URL, resulting in a fully specified URL that is passed as the command parameter by the ScriptCommand event.
The Windows Media Player control always processes incoming URL-type commands in the following manner:
- A URL-type command is received.
- Settings.baseURL is used to create a full URL from the relative URL specified in the script command.
- ScriptCommand is called.
- After ScriptCommand returns, Settings.invokeURLs is checked.
- If Settings.invokeURLs is true and the command is a URL-type, the specified URL is invoked. If Settings.invokeURLs is false or if the command is not a URL-type, the command is ignored.
When authoring a Windows Media file, you can specify which frame the new URL is displayed in by concatenating two ampersands and the name of the frame in the parameter field. The example below illustrates typical ScriptCommand parameters. It specifies that the URL mypage must be launched in the myframe frame.
The ScriptCommand event is not called if the file is being scanned (fast-forwarded or fast-reversed).
The value of event parameters is specified by Windows Media Player, and can be accessed or passed to a method in an imported JScript file using the parameter name given. This parameter name must be typed exactly as shown, including capitalization.