Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the same problem like here:
Memory Leaks in Window Media Player + c#[^]

Is there a solution since the mentioned solution does not work.
The memory is never freed until the system runs out of memory an crashes
Posted
Updated 4-Feb-22 0:47am
v2

1 solution

I added a reference to the following two dlls:
C:\Windows\System32\wmp.dll
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\AxInterop.WMPLib.dll


using AxWMPLib;

C#
private void Form1_Load(object sender, EventArgs e)
{
    Console.WriteLine("Test WMP Control");
    this.Closing += new CancelEventHandler(this.Form1_Closing);  // Set EventHandler
}

AxWMPLib.AxWindowsMediaPlayer wmp = null;  // Declare wmp Control

private void InitiateWMP()
{
    string link = @"C:\Users\MM\Videos\Video\lighthouse.mpg";
    wmp = new AxWMPLib.AxWindowsMediaPlayer();
    wmp.Enabled = true;
    wmp.Location = new System.Drawing.Point(0, 0);
    wmp.Name = "wmp";
    wmp.Size = new System.Drawing.Size(800, 600);
    this.Controls.Add(wmp);
    wmp.URL = link;
}

public void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    // Dispose of the Windows Media Player as the form is closing
    //
    // Note: The Form.Closed and Form.Closing events are not raised when the
    //   Application.Exit method is called to exit an application.    
    //   If Application.Exit is to be used, you should call the Form.Close method 
    //   before calling the Application.Exit method.
    //
    if (this.Controls.ContainsKey ("wmp"))
    {
        this.Controls.RemoveByKey("wmp"); // Remove from Conrols collection
        wmp.close(); // Close the Windows Media Player control
        wmp.Dispose(); // Dispose
        wmp = null;
    }
    GC.Collect(); // Start .NET CLR Garbage Collection
    GC.WaitForPendingFinalizers(); // Wait for Garbage Collection to finish
}

private void button1_Click(object sender, EventArgs e)
{
    button1.Enabled = false;
    InitiateWMP();
}
 
Share this answer
 
v6
Comments
john00 23-Jun-13 6:13am    
Thanks for your reply.
I am using it like this:
AxWMPLib.AxWindowsMediaPlayer wmp = new AxWMPLib.AxWindowsMediaPlayer();
wmp.URL=link;

later in code:
wmp.Close();
wmp.Dispose();

When I try ReleaseObject I get an Exception that it is not an COMObject.
Mike Meinz 23-Jun-13 8:35am    
Did you add a Reference to a DLL? If so, what?
I get a "type or namespace can't be found" error message.

john00 23-Jun-13 8:51am    
using AxWMPLib; //windows Media Player
using WMPLib;//windows Media Player

Reference would be:
AxInterop.WMPLib.dll
Interop.WMPLib.dll

Here is the complete code:

AxWMPLib.AxWindowsMediaPlayer wmp = new AxWMPLib.AxWindowsMediaPlayer();
((System.ComponentModel.ISupportInitialize)(wmp)).BeginInit();
wmp.Enabled = true;
wmp.Location = new System.Drawing.Point(0, 0);
wmp.Name = "wmp";
wmp.Size = new System.Drawing.Size(800, 600);
this.Controls.Add(wmp);
((System.ComponentModel.ISupportInitialize)(wmp)).EndInit();
wmp.uiMode = "none";
wmp.URL = link;
Mike Meinz 23-Jun-13 9:37am    
I was able to find AxInterop.WMPLib.dll in C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies. I was also able to find WMPLib.dll there. I could not find Interop.WMPLib.dll on my PC.

I tried to use WMPLib.dll, but I got an error:
"Could not load file or assembly 'AxInterop.WMPLib, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified."

I am sorry that I can't help you any further.
john00 23-Jun-13 9:46am    
Thanks for looking into that.

I found AxInterop.WMPLib.dll here: C:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\multimedia\WMP

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900