Why windows media player is not closing with player.Close() method?

4.5k views Asked by At

I am creating a media player object in a simple console application, to play some file. Though the media player is getting launched successfully, when I am using the close() method, the process still runs and media player window does not close. what needs to be done? here is the code I wrote..

WindowsMediaPlayer player= new WindowsMediaPlayer();
player.OpenPlayer("c:\\abc.wmv");
Thread.Sleep(2000);
player.controls.stop();
player.close();

Here the process doesn't exit and file still keeps running. How can I close the application?

2

There are 2 answers

0
Hans Passant On

The automation interface doesn't have a way to force the player to exit. The less than ideal approach is to kill it:

        var prc = Process.GetProcessesByName("wmplayer");
        if (prc.Length > 0) prc[prc.Length - 1].Kill();

The better mouse trap is to embed the player into your own GUI, easy to do with Winforms.

3
usr On

I think you need to close the COM object by calling Marshal.ReleaseComObject. COM does not know that you will never be using the player again, so it cannot close the process.

Do not rely on garbage collection for this because it might never happen if there is no memory pressure. Call Marshal.ReleaseComObject manually.