WinForm中播放声音的三种方法

声音文件folder.wav放置在bin目录下debug下
1.通过API调用
1 using  System.Runtime.InteropServices;
2 [DllImport( " winmm.dll " )]
3          public   static   extern   bool  PlaySound( string  pszSound,  int  hmod,  int  fdwSound);
4          public   const   int  SND_FILENAME  =   0x00020000 ;
5          public   const   int  SND_ASYNC  =   0x0001
6 // 调用下面的方法
7 PlaySound( " folder.wav " 0 , SND_ASYNC  |  SND_FILENAME);
8
2.通过SoundPlayer对象
1 using  System.Media;
2
3 SoundPlayer p  =   new  SoundPlayer();
4             p.SoundLocation  =  Application.StartupPath  +   " \\folder.wav " ;        
5             p.Load();
6             p.Play();
SoundPlayer
支持同步、异步播放
支持循环播放
支持文件和流播放
同步播放:
1 System.Media.SoundPlayer player  =   new  System.Media.SoundPlayer();
2 player.SoundLocation  =  Application.StartupPath  +   " \\folder.wav " ;        
3 player.Load();
4 player.Play();
异步播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = Application.StartupPath + "\\folder.wav";
player.LoadAsync();
player.PlaySync();
循环播放
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = Application.StartupPath + "\\folder.wav";
player.Load();
player.PlayLooping();
3.使用 Audio 对象
1 using  Microsoft.VisualBasic.Devices; // Microsoft.VisualBasic.dll需要添加引用
2
3 Audio a  =   new  Audio();
4           a.Play( " folder.wav " );

你可能感兴趣的:(WinForm)