[System.Runtime.InteropServices.DllImport("winmm.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)] public static extern int PlaySound(string name, int hmod, int flags);protected const int SND_ASYNC = 1; protected const int SND_NODEFAULT = 2; protected const int SND_FILENAME = 131072; protected const int SND_RESOURCE = 262148; public static void PlaySoundFile(string FileName) { if (FileName != "" && (File.Exists(FileName))) { PlaySound(FileName, null, SND_FILENAME | SND_ASYNC); } else { //Beep(); } }
internal const int SND_SYNC = 0x0000 ; /* play synchronously (default) */ internal const int SND_ASYNC = 0x0001 ; /* play asynchronously */ internal const int SND_NODEFAULT = 0x0002 ; /* silence (!default) if sound not found */ internal const int SND_MEMORY = 0x0004 ;/* pszSound points to a memory file */ internal const int SND_LOOP = 0x0008 ; /* loop the sound until next sndPlaySound */ internal const int SND_NOSTOP = 0x0010 ; /* don't stop any currently playing sound */ internal const long SND_NOWAIT = 0x00002000L; /* don't wait if the driver is busy */ internal const long SND_ALIAS = 0x00010000L; /* name is a registry alias */ internal const long SND_ALIAS_ID = 0x00110000L ;/* alias is a predefined ID */ internal const long SND_FILENAME = 0x00020000L; /* name is file name */ internal const long SND_RESOURCE = 0x00040004L; /* name is resource name or atom */ [DllImport("winmm.dll")] public static extern long PlaySound(String fileName,long a,long b); [DllImport("winmm.dll")] public static extern int mciSendString(string s1,string s2,int i1,int i2); private void timer1_Tick(object sender, System.EventArgs e) { //得到当前的时、分、秒 int h = DateTime.Now .Hour ; int m = DateTime.Now .Minute ; int s = DateTime.Now .Second ; string hs,ms,ss; //调用MyDrawClock绘制图形表盘 // MyDrawClock(h,m,s); //在statusbar上显示数字时钟 // statusBar1.Text = String.Format ("{0}:{1}:{2}",h,m,s); if(m<10) ms="0"+m.ToString (); else ms=m.ToString (); if(s<10) ss="0"+s.ToString (); else ss=s.ToString (); if(h<10) hs = "0"+h.ToString (); else hs = h.ToString (); label1.Text = String.Format ("{0}:{1}:{2}",hs,ms,ss); if(ms=="00" && ss == "00") { //Common.PlaySound("ring.wav",0,0x05); ThreadSound = new Thread (new ThreadStart (PlaySoundH)); ThreadSound.Start (); } } private void PlaySoundH() { Common.PlaySound ("ring.wav",0,0x05); }
public enum PlaySoundFlags : int { SND_SYNC = 0x0, // play synchronously (default) SND_ASYNC = 0x1, // play asynchronously SND_NODEFAULT = 0x2, // silence (!default) if sound not found SND_MEMORY = 0x4, // pszSound points to a memory file SND_LOOP = 0x8, // loop the sound until next sndPlaySound SND_NOSTOP = 0x10, // don't stop any currently playing sound SND_NOWAIT = 0x2000, // don't wait if the driver is busy SND_ALIAS = 0x10000, // name is a registry alias SND_ALIAS_ID = 0x110000,// alias is a predefined ID SND_FILENAME = 0x20000, // name is file name SND_RESOURCE = 0x40004, // name is resource name or atom }; public class Sound { [DllImport("coredll.dll")] public static extern int PlaySound( string szSound, IntPtr hModule, int flags); public static void Beep() { Play(@"\Windows\Voicbeep"); } public static void Play(string fileName) { try { PlaySound(fileName, IntPtr.Zero, (int)(PlaySoundFlags.SND_FILENAME | PlaySoundFlags.SND_SYNC)); } catch (Exception ex) { MessageBox.Show("Can't play sound file. " + ex.ToString()); } } }
public static extern int PlaySound(string name, int hmod, int flags);protected const int SND_ASYNC = 1;
protected const int SND_NODEFAULT = 2;
protected const int SND_FILENAME = 131072;
protected const int SND_RESOURCE = 262148; public static void PlaySoundFile(string FileName)
{
if (FileName != "" && (File.Exists(FileName)))
{
PlaySound(FileName, null, SND_FILENAME | SND_ASYNC);
}
else
{
//Beep();
}
}
internal const int SND_ASYNC = 0x0001 ; /* play asynchronously */
internal const int SND_NODEFAULT = 0x0002 ; /* silence (!default) if sound not found */
internal const int SND_MEMORY = 0x0004 ;/* pszSound points to a memory file */
internal const int SND_LOOP = 0x0008 ; /* loop the sound until next sndPlaySound */
internal const int SND_NOSTOP = 0x0010 ; /* don't stop any currently playing sound */ internal const long SND_NOWAIT = 0x00002000L; /* don't wait if the driver is busy */
internal const long SND_ALIAS = 0x00010000L; /* name is a registry alias */
internal const long SND_ALIAS_ID = 0x00110000L ;/* alias is a predefined ID */
internal const long SND_FILENAME = 0x00020000L; /* name is file name */
internal const long SND_RESOURCE = 0x00040004L; /* name is resource name or atom */ [DllImport("winmm.dll")]
public static extern long PlaySound(String fileName,long a,long b);
[DllImport("winmm.dll")]
public static extern int mciSendString(string s1,string s2,int i1,int i2); private void timer1_Tick(object sender, System.EventArgs e)
{
//得到当前的时、分、秒
int h = DateTime.Now .Hour ;
int m = DateTime.Now .Minute ;
int s = DateTime.Now .Second ;
string hs,ms,ss; //调用MyDrawClock绘制图形表盘
// MyDrawClock(h,m,s);
//在statusbar上显示数字时钟
// statusBar1.Text = String.Format ("{0}:{1}:{2}",h,m,s);
if(m<10)
ms="0"+m.ToString ();
else
ms=m.ToString (); if(s<10)
ss="0"+s.ToString ();
else
ss=s.ToString (); if(h<10)
hs = "0"+h.ToString ();
else
hs = h.ToString (); label1.Text = String.Format ("{0}:{1}:{2}",hs,ms,ss); if(ms=="00" && ss == "00")
{
//Common.PlaySound("ring.wav",0,0x05);
ThreadSound = new Thread (new ThreadStart (PlaySoundH));
ThreadSound.Start ();
}
} private void PlaySoundH()
{
Common.PlaySound ("ring.wav",0,0x05);
}
SND_SYNC = 0x0, // play synchronously (default)
SND_ASYNC = 0x1, // play asynchronously
SND_NODEFAULT = 0x2, // silence (!default) if sound not found
SND_MEMORY = 0x4, // pszSound points to a memory file
SND_LOOP = 0x8, // loop the sound until next sndPlaySound
SND_NOSTOP = 0x10, // don't stop any currently playing sound
SND_NOWAIT = 0x2000, // don't wait if the driver is busy
SND_ALIAS = 0x10000, // name is a registry alias
SND_ALIAS_ID = 0x110000,// alias is a predefined ID
SND_FILENAME = 0x20000, // name is file name
SND_RESOURCE = 0x40004, // name is resource name or atom
}; public class Sound
{
[DllImport("coredll.dll")]
public static extern int PlaySound(
string szSound,
IntPtr hModule,
int flags); public static void Beep() {
Play(@"\Windows\Voicbeep");
} public static void Play(string fileName) {
try {
PlaySound(fileName, IntPtr.Zero, (int)(PlaySoundFlags.SND_FILENAME | PlaySoundFlags.SND_SYNC));
} catch (Exception ex) {
MessageBox.Show("Can't play sound file. " + ex.ToString());
}
}
}
你断点看一下,是不是执行到beep();?
像这样@:\文件夹\文件名.wav这样用不可以吧??Common.PlaySound ("c:\windows\ring.wav",0,0x05);