How to dispose windows media player control (i.e axWindowsMediaPlayer) then use it again?

StackOverflow https://stackoverflow.com/questions/22576672

  •  19-06-2023
  •  | 
  •  

Question

I have a windows application, the main form (mainform.cs) include user controls which can be removed/added from main menu (i.e. user can close usercontrol from the main menu, usercontrols are layout within tabs in tabcontrols) When to close a usercontrol, I call dispose like this:

If (!MediaControl.IsDisposed)
     MediaControl.Dispose();

One of these usercontrols include axWindowsMediaPlayer control for playing media. When the application starts, it load that media usercontrol successfully, and able to play media without problems (add media files, change playlist items, play, pause ...etc all work) When I close that usercontrol from the main menu, media closed successfully. But when try to add it again, I get this message :

"Additional information: COM object that has been separated from its underlying RCW cannot be used."

The VS debug break at point when attempting to initialize the playlist:

    // Within media usercontrol
    protected override void OnFilesRefresh()
    {            
        foreach (string file in files)
        {
            WMPLib.IWMPMedia m1 = axWindowsMediaPlayer.newMedia(file);
            axWindowsMediaPlayer.currentPlaylist.appendItem(m1);
            .....
        }
    }

While files is array of file paths. newMedia method is causing the problem. How to dispose the media usercontrol which contain axWindowsMediaPlayer probably then initialize it again without problems ? Sorry for my English

Was it helpful?

Solution

A little search and found that the issue is described here:

Is it safe to call an RCW from a finalizer?

And here:

Release Excel Object In My Destructor

they have the same problem, Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top