C#声音控制

不能用控件,防止播放的时候出现延时
或者能够提供内存方法存放声音数据
目的就是能够十分准确的播放声音,不会出现声音的延迟现象

[DllImport("Winmm.dll")]
public static extern long PlaySound(string name,long  module,long flag);
[DllImport("winmm.dll")]
private static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);
private string m_MusicName="";
private void PlayMusic()
{
 m_MusicName="/""+Tool.ReadInfo("promptmusicfile")+"/"";
 if(m_MusicName.Length==0)
 return;
 try
 {
 mciSendString(@"close " + m_MusicName,"",0,0);
 mciSendString(@"open " + m_MusicName,"",0,0);
 mciSendString(@"play " + m_MusicName ,"",0,0);
 }
 catch
 {
 }

}

private void StopMusic()
{
 try
 {
 mciSendString(@"close " + m_MusicName,"",0,0);
 }
 catch{}
}

播放内存中的WAV文件可以这样:

//API定义
private const int SND_ASYNC  = 0x1;
private const int SND_MEMORY = 0x4;

[DllImport("winmm.dll")]
private static extern int sndPlaySoundA(byte[] lpszSoundName, int uFlags);

//将blip1.wav添加入工程并设置为嵌入的资源
//现在是将它读入内存备用
Type t=this.GetType();
System.Reflection.Assembly a=t.Assembly;
System.IO.Stream stream=a.GetManifestResourceStream(t.Namespace+".blip1.wav");
byte[] ba=new byte[stream.Length];
stream.Read(ba,0, ba.Length);
stream.Close();

//播放缓存
sndPlaySoundA(ba, SND_MEMORY);


你可能感兴趣的:(.NET)