Windows Media Player 11 SDK Embedding the Windows Media Player Control in a C# Solution
Previous | Next |
Embedding the Windows Media Player Control in a C# Solution
To use the functionality of Windows Media Player in a C# application, first add the component to a form as described in Using the Windows Media Player Control with Microsoft Visual Studio
The following sections describe how to create an application that plays video and uses custom play and stop buttons.
Add the Video Window
Add the Windows Media Player ActiveX 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. In C#, the following code will be displayed:
private void button1_Click(object sender, System.EventArgs e) { }
Add this line between the two curly braces:
axWindowsMediaPlayer1.URL = @"c:\mediafile.wmv";
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 item you want to play. Any valid file path can be used. The @ symbol instructs the compiler to not interpret backslashes as escape characters.
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:
axWindowsMediaPlayer1.currentPlaylist = axWindowsMediaPlayer1.mediaCollection.getByName("mediafile");
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. In C#, the following code will be displayed:
private void button2_Click(object sender, System.EventArgs e) { }
Add this line between the two curly braces:
axWindowsMediaPlayer1.Ctlcontrols.stop();
Note 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, it signals an event. Your application should handle error events sent by the Player.
To create an event handler, first open the Properties window for the Windows Media Player control. In the list of events, double-click MediaError. The following code is displayed:
private void Player_MediaError(object sender, _WMPOCXEvents_MediaErrorEvent e) { }
The following code could be inserted in the method to provide minimal error-handling capability. Note that information about the error can be retrieved from the _WMPOCXEvents_MediaErrorEvent argument.
try // If the Player encounters a corrupt or missing file, // show the hexadecimal error code and URL. { IWMPMedia2 errSource = e.pMediaObject as IWMPMedia2; IWMPErrorItem errorItem = errSource.Error; MessageBox.Show("Error " + errorItem.errorCode.ToString("X") + " in " + errSource.sourceURL); } catch(InvalidCastException) // In case pMediaObject is not an IWMPMedia item. { MessageBox.Show("Error."); }
See Also
Previous | Next |