15 out of 19 rated this helpful - Rate this topic

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.");
} 



Related topics

Embedding the Windows Media Player Control in a .NET Framework Solution

 

 

Send comments about this topic to Microsoft

Build date: 2/15/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Helpful
Very helpful
Video is black - more detail.
To add some detail: on a Windows XP, dual display machine, it is black on the primary display, but works fine on the secondary display. Even stranger, but equally true. My suspicion, for what it is worth is it is related to hardware acceleration.
It ONLY works with default codecs
While having the Windows Media Player Control available to C# is great, I'm afraid there is a problem - it will only recognise a limited set of default codecs. $0If you use another codec, it will have no video, i.e. show black, so only the audio will play (if that is a default codec). $0 $0You can have that codec installed, and it will work in Windows Media Player, but it will still fail in the Windows Media Player Control. Strange but true.$0 $0This is even more strange because the ffdshow controls appear in the system tray, so it is getting routed through DirectShow, but something is going wrong - it is reporting as being YUY2 instead of MP42 (in my case).$0 $0And that is even more strange to me - Cinepac codec works (ugh), but Mpeg4Version2 (FourCC of MP42) fails, yet MP42 works on even fresh installs of Windows XP.$0 $0I am yet to discover a comprehensive list of what codecs will work and what codecs will fail. I am hoping some older wmv codecs will work.$0 $0It is a pity Microsoft did not make this limitation clear to people considering using this technique. At the least, telling us that codecs work would help.$0 $0Note: This applies to WindowsXP. This does not apply to Windows 7. I have not tested against Windows Vista.$0
Change video border color
Hi.. I' m using WMP en c# and need to fit the video to the size of the player but that just occurs when a make a click on the video.
So my first question is.. How can i adjust the video size to theplayer size.. I'm using strechtoFit = true and dont function.

My second question is how can i change the video border color to a white color in the wmp in c#