在windows应用程序中如何发出声音?
给段代码好吗?谢谢了。
给段代码好吗?谢谢了。
解决方案 »
- 大人不要删!!初学者接触的问题,困扰几天了。没辙了!
- Winform 自定义控件属性
- C# VS2010 自动ADSL拨号问题
- 读TXT文件然后写入EXCEL里
- ch=((char)a).ToString();还是想不明白,刚才那位还在吗
- 请问如何由regist得知使用者已经安装dotnet framework 1.1
- webservice中如何引用外部对象。
- 8848.com 高薪招聘C#程序员
- 用c#创建用户并开通邮箱系统提示“该服务器不愿意处理该请求”
- 使用 Visual C# 创作用户控件 为什么会有这样的问题?在线等待....
- 怎么样读格式化的文本并附值啊?
- 新手求教:一个简单的通讯录软件,如何添加记录?
);
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
public static extern int DeclareBeep(int dwfreq,int dwduration);
[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.
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);
这句话什么意思?
如:
一个湖,可以租船,租船点就是CLR,船就是托管代码,湖就是CLR的控制范围.
[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);