我使用关闭按钮创建了这个简单的示例表单。
不使用 Interop.WMPLib.dll 时,一切都按预期工作
我见过其他应用程序使用它没有问题,但为什么当我添加以下行时表单进程没有关闭:
SoundPlayer myPlayer = new SoundPlayer();
当然还有处理它:
if (myPlayer != null)
{
myPlayer.Dispose();
myPlayer = null;
}
窗体关闭,但调试器 VS2008 仍处于活动状态。 Form 项目和 dll 仍然处于活动状态。
如果您给我发送电子邮件至[电子邮件受保护],我可以将压缩的项目发送给您。
下面是 dll 的类:
使用系统;
使用 System.Collections.Generic;
使用系统文本;
使用系统线程;
使用 System.Runtime.InteropServices;
使用 WMPLib;
命名空间 WindowsMobile.Utilities
{
公共委托 void SoundPlayerStateChanged(SoundPlayer 发送者, SoundPlayerState newState);
public enum SoundPlayerState
{
Stopped,
Playing,
Paused,
}
public class SoundPlayer : IDisposable
{
[DllImport("coredll")]
public extern static int waveOutSetVolume(int hwo, uint dwVolume);
[DllImport("coredll")]
public extern static int waveOutGetVolume(int hwo, out uint dwVolume);
WindowsMediaPlayer myPlayer = new WindowsMediaPlayer();
public SoundPlayer()
{
myPlayer.uiMode = "invisible";
myPlayer.settings.volume = 100;
}
string mySoundLocation = string.Empty;
public string SoundLocation
{
get { return mySoundLocation; }
set { mySoundLocation = value; }
}
public void Pause()
{
myPlayer.controls.pause();
}
public void PlayLooping()
{
Stop();
myPlayer.URL = mySoundLocation;
myPlayer.settings.setMode("loop", true);
}
public int Volume
{
get { return myPlayer.settings.volume; }
set { myPlayer.settings.volume = value; }
}
public void Play()
{
Stop();
myPlayer.URL = mySoundLocation;
myPlayer.controls.play();
}
public void Stop()
{
myPlayer.controls.stop();
myPlayer.close();
}
#region IDisposable Members
public void Dispose()
{
try
{
Stop();
}
catch (Exception)
{
}
// need this otherwise the process won't exit?!
try
{
int ret = Marshal.FinalReleaseComObject(myPlayer);
}
catch (Exception)
{
}
myPlayer = null;
GC.Collect();
}
#endregion
}
}