1.添加AppConfig配置
<configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
2.添加DX的DLL
在添加引用窗口中选择浏览,然后找到以下目录: C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0
找到以上目录后,在该目录里面找到以下连个dll文件,将它们添加到工程中来 1.)Microsoft.DirectX.DirectSound.dll 2.)Microsoft.DirectX.dll
3.添加源码
//程序启动时...
private void Form1_Load(object sender, EventArgs e) { System.Reflection.Assembly assembly; System.Media.SoundPlayer sp; assembly = System.Reflection.Assembly.GetExecutingAssembly(); sp = new System.Media.SoundPlayer(global::TestCSharp.Properties.Resources.bkMusic); sp.PlayLooping(); } //按钮【播放音效】 private void btnPlay_Click(object sender, EventArgs e) { SecondaryBuffer secBuffer;//缓冲区对象 Device secDev;//设备对象 secDev = new Device(); secDev.SetCooperativeLevel(this, CooperativeLevel.Normal);//设置设备协作级别 secBuffer = new SecondaryBuffer(global::TestCSharp.Properties.Resources.YinXiao, secDev);//创建辅助缓冲区 secBuffer.Play(0, BufferPlayFlags.Default);//设置缓冲区为默认播放 }