C#里怎么让计时器到了指定时间发出声音来
要怎么设置?
详细点
在线等

解决方案 »

  1.   

    using System;
    using System.Threading;
    using System.Runtime.InteropServices;
    namespace Sound
    {
    class Class1
    {
    [DllImport("user32.dll")] 
    public static extern int MessageBeep(uint n);  [STAThread]
    static void Main(string[] args)
    {
    for (int i=0;i<10;i++)
    {
    MessageBeep(0xFFFFFFFF); 
    Thread.Sleep(500);
    // Console.ReadLine();
    }

    Console.WriteLine("Finished!");
    }
    }
    }
      

  2.   

    using System;
    using System.Threading;
    using System.Runtime.InteropServices;
    namespace Sound
    {
    class Class1
    {
    [DllImport("user32.dll")] 
    public static extern int MessageBeep(uint n); 
    [DllImport("kernel32.dll")]
    private static extern int Beep(int dwFreq ,int dwDuration) ;
    [DllImport("winmm.dll")]
    public static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);
    [DllImport("winmm.dll")]
    public static extern long PlaySound(String fileName,long a,long b);
    [DllImport("winmm.dll", EntryPoint="PlaySound")]
    public static extern bool PlaySound(ref Byte snd, IntPtr hmod, uint fdwSound);

    [DllImport("winmm.dll", EntryPoint="PlaySound")]
    public static extern int PlaySound(string  lpszName,int hModule,int dwFlags); public enum PlayingFlags :uint 
    {
    SND_SYNC = 0x00,
    SND_ASYNC = 0x01,
    SND_NODEFAULT = 0x02,
    SND_MEMORY = 0x04,
    SND_ALIAS = 0x010000,
    SND_FILENAME = 0x020000,
    SND_RESOURCE = 0x040004,
    SND_ALIAS_ID = 0x0110000,
    SND_ALIAS_START = 0,
    SND_LOOP = 0x08,
    SND_NOSTOP = 0x010,
    SND_VALID = 0x01F,
    SND_NOWAIT = 0x02000,
    SND_PURGE = 0x40
    }
    internal class Helpers 
    {
    [Flags]
    public enum PlaySoundFlags : int 
    {
    SND_SYNC = 0x0000,  /* play synchronously (default) */
    SND_ASYNC = 0x0001,  /* play asynchronously */
    SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
    SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
    SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
    SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
    SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
    SND_ALIAS = 0x00010000, /* name is a registry alias */
    SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
    SND_FILENAME = 0x00020000, /* name is file name */
    SND_RESOURCE = 0x00040004  /* name is resource name or atom */
    } [DllImport("winmm")]
    public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags );
    } [STAThread]
    static void Main(string[] args)
    {
    //Play_Beep_01();
    //Play_Beep_03();
    //Play_Beep_02();             //Play_wav_01();
                Play_wav_02();
    Play_wav_03();

    Console.WriteLine("Finished!");
    Console.ReadLine();
    } static void Play_Beep_01()
    {
    for (int i=0;i<3;i++)
    {
    MessageBeep(0xFFFFFFFF); 
    Thread.Sleep(1000);
    }
    } static void Play_Beep_02()
    {
    int a=0X7FF;
    int b=100;
    Beep(a,b);
    }
    static void Play_wav_01()
    {
                try
                {
                    mciSendString("chimes.wav", null, 0, 0);
                    mciSendString("chord.wav", "ccc", 0, 0);
                    mciSendString("ding.wav", null, 0, 0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());    
                }
    } static void Play_wav_02()
    {
    PlaySound("1.wav",0,0);
    } static void Play_Beep_03()
    {
    byte[] bb = new byte[2];
    bb[0] = 7;
    char b = BitConverter.ToChar(bb,0);
    for(int i = 0;i<4;i++)
    {
    Console.Write(b);
    Thread.Sleep(1000);
    }
    } static void Play_wav_03()
    {
    for (int i=0;i<10;i++)
    {
                    Helpers.PlaySound("Utopia Restore Down.WAV", IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME | Helpers.PlaySoundFlags.SND_ASYNC);
    Thread.Sleep(100);
    }
    }
    }
    }
      

  3.   

    新建一个VC#的Windows Form工程(Windows应用程序),并且定义两个菜单按钮(menuItem1,menuItem2)。
        选择菜单中的“工具”中的“自定义工具箱(添加/移除工具箱项)”,在自定义工具箱的窗口中,点击展开“COM 组件”项,选中“Window Media Player”选项。确定后在“工具箱”中便会出现“Windows Media Player”这一项,然后再将其拖至Form上,调整大小,系统在“引用”中自动加入了对此dll的引用,AxMediaPlayer就是我们使用的Namespace与class。
        在属性栏中设置好此控件的一些属性,为了方便,这里我把AutoStart设置成为true(其实默认是true),只要FileName被设置(打开了文件),则文件将会自动播放。完整代码如下:private void menuItem1_Click(object sender, System.EventArgs e)
    { OpenFileDialog ofDialog = new OpenFileDialog();
    ofDialog.AddExtension = true;
    ofDialog.CheckFileExists = true;
    ofDialog.CheckPathExists = true;//the next sentence must be in single line
    ofDialog.Filter = "VCD文件(*.dat)|*.dat|Audio文件(*.avi)|*.avi
       |WAV文件(*.wav)|*.wav|MP3文件(*.mp3)|*.mp3|所有文件 (*.*)|*.*";ofDialog.DefaultExt = "*.mp3";
    if(ofDialog.ShowDialog() == DialogResult.OK)
    {
      this.axMediaPlayer1.FileName = ofDialog.FileName;
    }
    }
      

  4.   

            int m_iTime = 60;
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Interval = 1000;
                timer1.Enabled = true;
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                if (m_iTime <= 0)
                {
                    timer1.Enabled = false;
                    // 播放声音
                    System.Media.SoundPlayer sp = new System.Media.SoundPlayer("c:\\1.wav");
                    sp.Play();
                }
                else
                {
                    m_iTime--;
                }
            }
      

  5.   

    AxMediaPlayer axMediaPlayer1 = new AxMediaPlayer();
    axMediaPlayer1.AutoStart = true在定时器触发的时候,设置this.axMediaPlayer1.FileName = FileName;