6.使用directX
1.播放系统事件声音
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"D:\test.wav";
player.Load(); //同步加载声音
player.Play(); //启用新线程播放
//player.PlayLooping(); //循环播放模式
//player.PlaySync(); //UI线程同步播放
using System.Runtime.InteropServices;
public static class WavPlayer
{
[DllImport("winmm.dll", SetLastError = true)]
static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound);
[DllImport("winmm.dll", SetLastError = true)]
static extern long mciSendString(string strCommand,
StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
[DllImport("winmm.dll")]
private static extern long sndPlaySound(string lpszSoundName, long uFlags);
[Flags]
public enum SoundFlags
{
/// play synchronously (default)
SND_SYNC = 0x0000,
/// play asynchronously
SND_ASYNC = 0x0001,
/// silence (!default) if sound not found
SND_NODEFAULT = 0x0002,
/// pszSound points to a memory file
SND_MEMORY = 0x0004,
/// loop the sound until next sndPlaySound
SND_LOOP = 0x0008,
/// don’t stop any currently playing sound
SND_NOSTOP = 0x0010,
/// Stop Playing Wave
SND_PURGE = 0x40,
/// don’t wait if the driver is busy
SND_NOWAIT = 0x00002000,
/// name is a registry alias
SND_ALIAS = 0x00010000,
/// alias is a predefined id
SND_ALIAS_ID = 0x00110000,
/// name is file name
SND_FILENAME = 0x00020000,
/// name is resource name or atom
SND_RESOURCE = 0x00040004
}
public static void Play(string strFileName)
{
PlaySound(strFileName, UIntPtr.Zero,
(uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_SYNC | SoundFlags.SND_NOSTOP));
}
public static void mciPlay(string strFileName)
{
string playCommand = "open " + strFileName + " type WAVEAudio alias MyWav";
mciSendString(playCommand, null, 0, IntPtr.Zero);
mciSendString("play MyWav", null, 0, IntPtr.Zero);
}
public static void sndPlay(string strFileName)
{
sndPlaySound(strFileName, (long)SoundFlags.SND_SYNC);
}
}
关于mciSendString的详细参数说明,请参见MSDN,或是http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx
4.使用axWindowsMediaPlayer的COM组件来播放
选择菜单中的“工具”中的“自定义工具箱(添加/移除工具箱项)”,在自定义工具箱的窗口中,点击展开“COM 组件”项,选中“WindowMedia Player”选项。确定后在“工具箱”中便会出现“Windows Media Player”这一项,然后再将其拖至Form上,调整大小,系统在“引用”中自动加入了对此dll的引用,AxMediaPlayer就是我们使用的 Namespace与class。
把Windows Media Player控件拖放到Winform窗体中,把axWindowsMediaPlayer1中URL属性设置为MP3或是AVI的文件路径。
private voidmenuItem1_Click(object sender, System.EventArgs e)
{
OpenFileDialogofDialog = new OpenFileDialog();
ofDialog.AddExtension= true;
ofDialog.CheckFileExists= true;
ofDialog.CheckPathExists= true;
//the nextsentence 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)
{
// 2003一下版本 方法this.axMediaPlayer1.FileName = ofDialog.FileName;
this.axMediaPlayer1.URL=ofDialog.FileName;//2005用法
}
}
///
/// 文件全名
public voidPlaySound(string FileName)
{//要加载COM组件:Microsoft speech object Library
if (!System.IO.File.Exists(FileName))
{
return;
}
SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();
SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();
spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead,true);
SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;
pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);
spFs.Close();
}
准备工作:
1.安装了DirectX SDK(有9个DLL文件)。这里我们只用到MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll
2.一个W***文件。(这样的文件比较好找,在QQ的目录里就不少啊。这里就不多说了。)名字叫SND.W***,放在最后目标程序的同个目录下面
开始写程序啦。随便用个UltraEdit就好了。
1.引入DirectX 的DLL文件的名字空间:
using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; |
Device dv=new Device(); |
dv.SetCooperativeLevel((new UF()),CooperativeLevel.Priority); |
SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv); |
buf.Play(0,BufferPlayFlags.Looping); |
Console.Read(); |
class UF:Form{} |
csc /r:directX/MicroSoft.DirectX.dll;directX/Microsoft.Directx.DirectSound.dll dxsnd.cs |
//dxsnd.cs using System; using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; using System.Windows.Forms; namespace test1 { class test { public static void Main(string [] args) { Device dv=new Device(); dv.SetCooperativeLevel((new UF()),CooperativeLevel.Priority); SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv); buf.Play(0,BufferPlayFlags.Looping); Console.ReadLine(); } class UF:Form{} } } |
http://www.cnblogs.com/net-study/archive/2013/07/10/3181674.html
http://www.sufeinet.com/thread-459-1-1.html