C#播放声音【六种方法】

您所在的位置:网站首页 an循环播放代码 C#播放声音【六种方法】

C#播放声音【六种方法】

#C#播放声音【六种方法】| 来源: 网络整理| 查看: 265

C#中声音的播放主要有六种方法: 1.播放系统事件声音  2.使用SoundPlayer 3.使用API函数播放 4.使用axWindowsMediaPlayer的COM组件来播放 5.Microsoft speech object Library6.使用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();

2. 使用SoundPlayer SoundPlayer player = new SoundPlayer(); player.SoundLocation = @"D:\test.wav"; player.Load(); //同步加载声音 player.Play(); //启用新线程播放 //player.PlayLooping(); //循环播放模式 //player.PlaySync(); //UI线程同步播放

 

3. 使用API函数播放 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用法 } }

 

5.Microsoft speech object Library ///


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3