1.添加*.wav(*.bmp等同理)到解决方案,并设置其属性为“嵌入式资源”。
2.
//存放声音文件
byte[] by;
[DllImport("winmm.dll", EntryPoint = "sndPlaySoundA")]
public static extern long sndPlaySoundA(byte[] SoundName, long Flags);
public Form1()
{
...
...
//加载声音文件到内存,不必每次播放都从文件再加载,提高性能
Type ty = this.GetType();
System.Reflection.Assembly a = ty.Assembly;
System.IO.Stream stream = a.GetManifestResourceStream(ty.Namespace + ".下跌预警.wav");
by = new byte[stream.Length];
stream.Read(by, 0, by.Length);
stream.Close();
...
}
3.播放:
sndPlaySoundA(by, 4);