Click here to Skip to main content
13,589,343 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 21 Nov 2012
Licenced CPOL

Using Windows Media Player Control in WPF

, 21 Nov 2012
Rate this:
Please Sign up or sign in to vote.
This article shows how to use Windows Media Player control in a WPF application to avoid the performance degradation of MediaElement


WPF provides a MediaElement control which is a very convenient tool to play all types of videos supported by Windows Media Player. The performance of MediaElement, however, is not very convincing all the time. Particularly in full-screen mode the performance of MediaElement can be easily graded as less than average. This is due to the way WPF rendering pipeline has been implemented.

When you do not require the fancy WPF frills like semi-transparency, bizarre clipping and the like, you can safely bypass the MediaElement altogether and use the old Windows Media Player directly in your application to get a smooth running video with great performance.

Windows Forms Host

The Windows Media Player (WMP) control is a COM based component. As we all know, COM controls cannot be embedded in a WPF application. So the workaround is to first embed the WMP control in a WinForms control and then host that control inside your WPF application using WindowsFormsHost. This is actually not as bad as it sounds. The process is pretty straightforward and works without a hitch.

I have created a class library callled "WpmFormsLib" which contains a WinForms UserControl with WMP embedded inside it. So you do not even need to perform the above step. Simply download the code provided with this article and use the class library and COM Interop assemblies in your application.

In case you wanted to do the whole process on your own, just follow the steps given in this MSDN article.

Using the Player

Now that the player is ready to use inside the WinForms library, we are good to give it a shot in our WPF application. First add references in your WPF project to System.Windows.Forms and the three 'dll' files made available for download with this article: AxInterop.WMPLib, Interop.WMPLib and WpmFormsLib. You will have to make sure that all these 3 files are present inside your 'bin' folder (or whatever folder the exe resides in) before running the WPF application.

In order to make use of the class library, add these lines in the root of your XAML file:

Now put the created control in your Window like this:
<windowsformshost panel.zindex="200" name="formsHost">
	<ax:axwindowsmediaplayer x:name="axWmp">

By now we have everything that was needed in our XAML file. Now we need to write some code.

The WMP controls provides a host of properties, methods and events you can use to control its behavior and look & feel. You can refer to the WMP ActiveX control documenation on MSDN for complete details. In this example we will allow the user to browse to a video file. Then we will go in full-screen mode to play the file. We will finally return to normal screen after the video has finished palying. Here goes the code:

AxWMPLib.AxWindowsMediaPlayer player = null;
public MainWindow()
void InitMediaPlayer()
    player = formsHost.Child as AxWMPLib.AxWindowsMediaPlayer;                        
    player.uiMode = "none";
    player.settings.setMode("loop", false);
    player.stretchToFit = true;
    player.enableContextMenu = false;
    player.ErrorEvent += new EventHandler(player_ErrorEvent);
    player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange);
    player.ClickEvent += new AxWMPLib._WMPOCXEvents_ClickEventHandler(player_ClickEvent);

//this method is called by the WMP on imortance events like when the video is started, finished etc.
void player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
	if (e.newState == (int)WMPLib.WMPPlayState.wmppsMediaEnded)
        this.WindowStyle = WindowStyle.SingleBorderWindow;
        this.WindowState = System.Windows.WindowState.Normal;
    else if (e.newState == (int)WMPLib.WMPPlayState.wmppsPlaying)
        this.WindowStyle = WindowStyle.None;
        this.WindowState = System.Windows.WindowState.Maximized;

void player_ErrorEvent(object sender, EventArgs e)
    //deal with error here
    messageLabel.Content = "An error occured while trying to play the video.";

void player_ClickEvent(object sender, AxWMPLib._WMPOCXEvents_ClickEvent e)
    //deal with player click event here


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Technical Lead
Pakistan Pakistan
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

You may also be interested in...


Comments and Discussions

Questionnot working Pin
Member 1331979930-Jul-17 20:00
memberMember 1331979930-Jul-17 20:00 
Questionnot working Pin
Member 1331979927-Jul-17 21:00
memberMember 1331979927-Jul-17 21:00 
Questiongetting exception Pin
Rawat JI26-Jun-17 0:41
memberRawat JI26-Jun-17 0:41 
GeneralHow to design Windows media player in wpf source code in adavance level Pin
Keerthi Viswanathan21-Feb-17 1:25
memberKeerthi Viswanathan21-Feb-17 1:25 
QuestionZIndez and new image Pin
Member 1107465711-Sep-14 1:22
memberMember 1107465711-Sep-14 1:22 
AnswerRe: ZIndez and new image Pin
Kashif_Imran11-Sep-14 1:34
memberKashif_Imran11-Sep-14 1:34 
GeneralRe: ZIndez and new image Pin
tylocook2-Jul-15 15:40
membertylocook2-Jul-15 15:40 
GeneralRe: ZIndez and new image Pin
Kashif_Imran3-Jul-15 23:48
memberKashif_Imran3-Jul-15 23:48 
QuestionZIndez and new image Pin
Member 1107465711-Sep-14 0:47
memberMember 1107465711-Sep-14 0:47 
AnswerRe: ZIndez and new image Pin
Kashif_Imran11-Sep-14 1:23
memberKashif_Imran11-Sep-14 1:23 
GeneralRe: ZIndez and new image Pin
Member 1107465711-Sep-14 22:13
memberMember 1107465711-Sep-14 22:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180615.1 | Last Updated 21 Nov 2012
Article Copyright 2012 by Kashif_Imran
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid