使用C#发声(整理)

   由于C#中没有发声的元件,所以要实现此目的需要调用系统的API函数,本文只收录的了如何在window下实现的例子,没有包含非window的操作系统。

     下面是代码:

                     using System.Runtime.InteropServices

                     //在C#中调用winAPI函式

                      [DllImport("winmm.dll" , EntryPoint="PlaySound")]

                     public static extern int PlaySound(string lpxzName, int hModul, int dwFlags);

                     //如何使用PlaySound()函式

                     int  SND_ASYNC = 0X1;

                      int SND_FileNAME = 0X20000;

                     int  SND_LOOP = 0X8;

                     //运行PlaySound函数

                    PlaySound("音频文件名.wav", 0,  SND_ASYNC|SND_FileName|SND_LOOP);

      如果音频文件过大,从而造成系统资源紧张的问题则需另一些方法解决。如分割文件以及建立缓冲区的方法。

        如下:

         //声明一些API函数的调用

         [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")+"/"";    //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{}
                               }

                下面的代码是在内存中建立缓冲机制的方法实例:

                              //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);

    以下是对PlaySound和mcisendstring的说明:

 winmm.dll中对PlaySound的说明:
Platforms: Win 32s, Win 95/98, Win NT PlaySound plays a waveform sound through the speakers. This sound could be a .wav file, a system event sound (such as the system startup sound), or a sound resource stored in an application. Note that when the function needs to play an application resource or a RAM-loaded sound, Visual Basic users must use the alternate declare of the function in order to pass the numeric identifier of the sound instead of a string. The function returns 0 if an error occured, or a non-zero value if successful.
对mciSendString的说明:
mciGetErrorString obtains a textual description of an error raised by another Media Control Interface (MCI) function. Typically these errors are not the fault of the program. Rather, they are caused by "problems" with the device (for example, the MIDI driver is currently being used by another program, so your program's attempt to open it failed). The messages retrieved by this function are sufficient to tell the user what caused the error. 

  ps:随手记录的一段代码,javascrip中实现定时页面刷新及执行一些操作

            可以在javascript中用window.setInterval()来定时的刷新页面,或者使用Meta Refresh来定时刷新,如

             window.setInterval() 的实现:

   

FormName为按钮等控件所在的Form   的ID

来源:

       http://community.csdn.net/Expert/topic/2697/2697981.xml?temp=.9674799

       http://community.csdn.net/Expert/topic/3249/3249704.xml?temp=2.178371E-03

       http://community.csdn.net/Expert/topic/3713/3713058.xml?temp=.8741266
       

你可能感兴趣的:(C#的学习)