日期:2014-05-19  浏览次数:20968 次

winForm 声音如何实现
想要实现类似於QQ一样,有消息來时,便有预设的声音发出,如何实现

注:是vs2003.

------解决方案--------------------
INVOKE API
------解决方案--------------------
using System;
using System.Runtime.InteropServices;

public class SoundPlayer
{
public SoundPlayer()
{
}
[DllImport( "winmm.dll ")]
private static extern long sndPlaySound(string lpszSoundName, long uFlags);

public static void PlaySound(string fileName)
{
sndPlaySound(fileName, 1);
}
}
------解决方案--------------------
用循环监控貌似有点笨。哈哈
------解决方案--------------------
除了API,没有更好的法子

------解决方案--------------------
API函数
[DllImport( "winmm.dll ", EntryPoint= "PlaySound ")]
public static extern int PlaySound (
string lpszName,
int hModule,
int dwFlags
);
------解决方案--------------------
using System.Media;

private void button1_Click(object sender, EventArgs e)
{
//通过文件播放
SoundPlayer vSoundPlayer = new SoundPlayer();
vSoundPlayer.Stream = new FileStream(@ "C:\WINDOWS\Media\chord.wav ",
FileMode.Open, FileAccess.Read);
vSoundPlayer.Play();
}
------解决方案--------------------
获取事件
然后调用API 播放声音文件。
------解决方案--------------------
using System.Media;

private void button1_Click(object sender, EventArgs e)
{
//通过文件播放
SoundPlayer vSoundPlayer = new SoundPlayer();
vSoundPlayer.Stream = new FileStream(@ "C:\WINDOWS\Media\chord.wav ",
FileMode.Open, FileAccess.Read);
vSoundPlayer.Play();
}


这个方法可行。你可以试试。
------解决方案--------------------
vs2005下可用
using System.Media;

private void button1_Click(object sender, EventArgs e)
{
SoundPlayer vSoundPlayer = new SoundPlayer();
vSoundPlayer.Stream = new FileStream(@ "C:\WINDOWS\Media\chord.wav ",
FileMode.Open, FileAccess.Read);
vSoundPlayer.Play();
}
vs2003下只能用api了
------解决方案--------------------
源代码如下(全部)(自己实现的播放声音类):

public sealed class wavPlayer
{
[DllImport( "Winmm ")]
public static extern bool PlaySound(string pszSound, IntPtr hmod, UInt32
fdwSound);
private const Int32 m_SND_ASYNC = 1;
private const Int32 m_SND_LOOP = 8;
private const Int32 m_SND_FILENAME = 131072;
private static string m_PathSoundFile = string.Empty;
private static bool m_State = false;

public static bool PlayState
{
set
{
m_State = value;
}
get
{
return m_State;
}
}

public static void InitSound()
{
string soundFile = ConfigurationManager.AppSettings[ "SoundFile "].ToString();

if (soundFile == " ")