How to Interact with Windows Media Player in C#

How to interact with Windows Media Player in C#

I had this https://social.msdn.microsoft.com/Forums/vstudio/en-US/dbd43d7e-f3a6-4087-be06-df17e76b635d/windows-media-player-remoting-in-c?forum=clr in my bookmarks but have NOT tested it in anyway. Just a pointer in the right direction. It's nothing official and will require a bit of digging, but you should get a fairly simple wrapper (which will still use PInvoke under the hood - but you won't see it) around Windows Media Player.

Hope that helps.

Oh, I misunderstood. I thought you were talking about controlling the currently running Windows Media Player instance. If you are hosting Windows Media Player yourself then WMPLib is certainly the better solution.

How to interface with Windows Media Player in C#

A bit of sniffing around in Google gave me this: http://brandon.fuller.name/archives/hacks/nowplaying/wmp/, it looks like you need to write a plugin for WMP that exposes the information to your application. Depending on what you need/cost, the Plugin on that page might do the job!

Problems playing video with Windows Media Player in Winforms App

In order to play a .mp4 file using Windows Media Player, try the following which has been tested.

Pre-requisites: If not already installed, a "mp4" codec must be installed - such as K-Lite codec pack or another codec pack.

VS 2019:

Create a new project

  • Click File

  • Select New

  • Select Project

  • Select the following:
    Sample Image

  • Click Windows Forms App (.NET Framework)

    Sample Image

  • Click Next

  • Enter desired project name (ex: MediaPlayerTest)

  • Click Create

Add Reference

  • In VS menu, click Project
  • Select Add Reference...
  • Click COM
  • Select Windows Media Player
  • Click OK

Open Solution Explorer:

  • In VS menu, click View
  • Select Solution Explorer

Open Form1 in Designer

  • In Solution Explorer, double-click Form1.cs

Open Toolbox:

  • In VS menu, click View
  • Select Toolbox
  • Search: Windows Media Player
  • If Windows Media Player isn't found, add it to the Toolbox.

Add Windows Media Player to Toolbox (if it doesn't already exist in the Toolbox)

  • Right-click All Windows Forms
  • Select Choose Items....
  • After it's finished loading, click COM Components.
  • Check "Windows Media Player**.
    Sample Image
  • Click OK
  • Windows Media Player should now exist under "All Windows Forms" in the Toolbox.

Add Windows Media Player to Form1

  • Drag Windows Media Player from the Toolbox onto Form1.

Add Buttons to Form1

  • Drag two buttons from the Toolbox onto Form1
  • Rename button1 to btnPlay
  • Rename button2 to btnStop
  • Double-click btnPlay to create the click event handler
  • Double-click btnStop to create the click event handler

Modify/add code to Form1.cs

  • In Solution Explorer, right-click Form1.cs
  • Select View Code

Copy desired .mp4 file (ex: ocean.mp4) to the folder that your .exe file exists in.

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MediaPlayerTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.uiMode = "none";
}

private void btnPlay_Click(object sender, EventArgs e)
{
string filename = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().FullName), "ocean.mp4");
axWindowsMediaPlayer1.URL = filename;
}

private void btnStop_Click(object sender, EventArgs e)
{
//stop
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
}
}

Test Media Player:

  • Run your program
  • Click Play button

Resources:

  • Embedding the Windows Media Player Control in a C# Solution

Adding Windows Media Player to c# program

Try adding it in references, Right click your project click add reference and add select Windows Media Player, then click the OK button.



Related Topics



Leave a reply



Submit