在windows应用程序中如何发出声音?
给段代码好吗?谢谢了。

解决方案 »

  1.   

    就是点个按钮,然后播放个wav文件
      

  2.   

    The MessageBeep function plays a waveform sound. The waveform sound for each sound type is identified by an entry in the [sounds] section of the registry. BOOL MessageBeep(    UINT uType  // sound type  
       );
     ParametersuTypeSpecifies the sound type, as identified by an entry in the [sounds] section of the registry. This parameter can be one of the following values: Value Sound
    0xFFFFFFFF Standard beep using the computer speaker
    MB_ICONASTERISK SystemAsterisk
    MB_ICONEXCLAMATION SystemExclamation
    MB_ICONHAND SystemHand
    MB_ICONQUESTION SystemQuestion
    MB_OK SystemDefault
     Return ValuesIf the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError. ResAfter queuing the sound, the MessageBeep function returns control to the calling function and plays the sound asynchronously. 
    If it cannot play the specified alert sound, MessageBeep attempts to play the system default sound. If it cannot play the system default sound, the function produces a standard beep sound through the computer speaker. 
    The user can disable the warning beep by using the Control Panel Sound application. See AlsoFlashWindow, MessageBox
      

  3.   

    [DllImport("kernel32.dll",EntryPoint="Beep")]
    public static extern int DeclareBeep(int dwfreq,int dwduration);
      

  4.   

    找了一个别人的方法,给你参考一下用win32 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")+"\"";
    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{}
    }
    其中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. 
      

  5.   

    播放内存中的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);
      

  6.   

    m_MusicName="\""+Tool.ReadInfo("promptmusicfile")+"\"";
    这句话什么意思?
      

  7.   

    //将blip1.wav添加入工程并设置为嵌入的资源具体应该怎么做?
      

  8.   

    托管就是...
    如:
    一个湖,可以租船,租船点就是CLR,船就是托管代码,湖就是CLR的控制范围.
      

  9.   

    如果需要简单呢 极其简单
    [DllImport("winmm.dll")]
    public static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);mciSendString("play *.wav",null,0,0);
    想要停止呢
    mciSendString("stop *.wav",null,0,0);