Embedding Windows Media Player in Windows Form

1. Need to add reference to WMPLib and AxWMPLib

On the Toolbox right click on Components and select Choose Items….
Select COM Components tab.
Find Windows Media Player, enable and click on OK.
In the Toolbox the Windows Media Player should appear. Drag that item to your form. This should create the required references.

2. Code example

using WMPLib;
using AxWMPLib;

namespace test
{
   public partial class Test : Form
   {
     AxWindowsMediaPlayer movies;
    
    private void Test_Load(object sender, EventArgs e)
    {
           movies = new AxWindowsMediaPlayer();
           movies.PlayStateChange +=
              new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(movies_PlayStateChange);
           movies.Enabled = true;
           movies.Dock = DockStyle.Fill;
           this.Controls.Add(movies);
           movies.uiMode = "none";
           movies.settings.mute = true;

           movies.Enabled = true;
           movies.URL = @"c:\movie.avi";
           movies.Ctlcontrols.play();
    }

    private void movies_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
    {
        // 1 = Stopped
        // 2 = Paused
        // 3 = Playing
        // 8 = Media ended
        switch (e.newState)
        {
            case (int)WMPLib.WMPPlayState.wmppsStopped: // Console.WriteLine("Stopped");
                break;
            case (int)WMPLib.WMPPlayState.wmppsPaused: // Console.WriteLine("Paused");
                break;
            case (int)WMPLib.WMPPlayState.wmppsPlaying: // Console.WriteLine("Playing");
                break;
            case (int)WMPLib.WMPPlayState.wmppsMediaEnded: // Console.WriteLine("Media ended");
                break;
        }
    }

   }
}
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *