2

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?

satya
  • 2,537
  • 9
  • 33
  • 43
  • 1
    Yeah .close() won't do it. According to the docs, "This method closes the current digital media file, not Windows Media Player itself.". http://msdn.microsoft.com/en-us/library/windows/desktop/dd562399%28v=vs.85%29.aspx I can't seem to find anything else on exiting the process. – bschultz Jan 27 '12 at 22:04
  • I'm not sure at all if this will work, but try Environment.Exit(exitCode). Make sure you have SecurityPermissionFlag.UnmanagedCode permission. Like I said, I don't know if it'll work but it's worth a shot lol. – bschultz Jan 27 '12 at 22:23

2 Answers2

1

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

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.

usr
  • 168,620
  • 35
  • 240
  • 369
  • I have tried that too. still It didn't exiting the process. The media player window still runs the file. Not sure why the stop() method also doesn't work in the above scenario.. However, I tried with other approach where stop() works. like Player.URL = "c:\\abc.wmv"; player.controls.play(); player.controls.stop(); But the problem here is there is no display window. I could hear only audio – satya Jan 27 '12 at 22:03
  • You need to dispose _all_ COM objects. maybe you have a subobject like player.playlist lying around? Anything that you ever accessed would be a cause of this problem. Try calling GC.Collect() and GC.WaitForPendingFinalizer() and see if the problem goes away. – usr Jan 27 '12 at 22:06
  • Try this: Marshal.ReleaseComObject(new WindowsMediaPlayer()). Does the process still not exit? – usr Jan 27 '12 at 22:06