本代码特点:不用DirectX ,对于C/S 、B/S都适用。

方法:

 

[csharp] view plaincopy
  1. //mciSendStrin.是用来播放多媒体文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等,下面介绍一下它的使用方法:  
  2. //第一个参数:要发送的命令字符串。字符串结构是:[命令][设备别名][命令参数].  
  3. //第二个参数:返回信息的缓冲区,为一指定了大小的字符串变量.  
  4. //第三个参数:缓冲区的大小,就是字符变量的长度.  
  5. //第四个参数:回调方式,一般设为零  
  6. //返回值:函数执行成功返回零,否则返回错误代码  
  7. [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]  
  8. private static extern int mciSendString(  
  9.     string lpstrCommand,  
  10.     string lpstrReturnString,  
  11.     int uReturnLength,  
  12.     int hwndCallback);  
  13.   
  14. private static void mciSendString(String cmd)  
  15. {  
  16.     mciSendString(cmd, "", 0, 0);  
  17. }  
  18.   
  19. private static void StartRecord()  
  20. {  
  21.     mciSendString("close movie");  
  22.     mciSendString("open new type WAVEAudio alias movie");  
  23.     mciSendString("record movie");  
  24. }  
  25.   
  26. private static void StopRecord(string filename)  
  27. {  
  28.     mciSendString("stop movie");  
  29.     mciSendString("save movie " + filename);  
  30.     mciSendString("close movie");  
  31. }  

 

用法举例:

 

[csharp] view plaincopy
  1.         protected void btStart_Click(object sender, EventArgs e)  
  2.         {     
  3.             //开始录音  
  4.             StartRecord();  
  5.         }  
  6.   
  7.         protected void btStop_Click(object sender, EventArgs e)  
  8.         {  
  9.             //停止录音  
  10.             StopRecord(@"C:\test.wav");  
  11.         }  
  12.   
  13.         protected void btPlay_Click(object sender, EventArgs e)  
  14.         {  
  15.             //播放录音   也可以适用window系统带的TTS(Text To Speech)播放录音  
  16.             SoundPlayer sp = new SoundPlayer(@"c:\test.wav");  
  17.             sp.PlaySync();  
  18.         }  
posted on 2014-08-22 15:17  GC2013  阅读(1775)  评论(0编辑  收藏  举报