不能用控件,防止播放的時候出現延時
或者能夠提供內存方法存放聲音數據
目的就是能夠十分准確的播放聲音,不會出現聲音的延遲現象
[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);