Thursday, February 5, 2009

How to record voice from microphone using C#

   1:  using System;
   2:  using System.Data;
   3:  using System.Configuration;
   4:  using System.Web;
   5:  using System.Web.Security;
   6:  using System.Web.UI;
   7:  using System.Web.UI.WebControls;
   8:  using System.Web.UI.WebControls.WebParts;
   9:  using System.Web.UI.HtmlControls;
  10:   
  11:  //add below namespaces
  12:  using Microsoft.VisualBasic.Devices;
  13:  using Microsoft.VisualBasic;
  14:  using System.Runtime.InteropServices;
  15:   
  16:  //Add reference to "Microsoft.VisualBasic" which is
       available in .NET tab
  17:   
  18:  public partial class _Default : System.Web.UI.Page 
  19:  {
  20:     protected void Page_Load(object sender, EventArgs e)
  21:      {
  22:   
  23:      }
  24:      //Add the below API. winmm.dll is a module for the Windows Multimedia API, which contains low-level audio and joystick functions
  25: [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  26:      
  27:      //The mciSendString function sends a command
           string to an MCI device. 
  28:      //MCI devices are drivers that provide Windows- 
           based programs 
  29:      //device-independent capabilities for controlling
           multimedia hardware and software.
  30:      private static extern int mciSendString
  31:          (string lpstrCommand, string 
      lpstrReturnString, int uReturnLength, int hwndCallback);
  32:   
  33:      protected void  btnRecord_Click(object sender,
           EventArgs e)
  34:  {
  35:      mciSendString("open new Type waveaudio Alias
          TestSoundRecord", "", 0, 0);
  36:      mciSendString("record TestSoundRecord", "", 0, 0);
  37:  }
  38:  protected void  btnStopSave_Click(object sender,
       EventArgs e)
  39:  {
  40:      mciSendString("save TestSoundRecord
           c:\\recorded.wav", "", 0, 0);
  41:      mciSendString("close TestSoundRecord ", "", 0, 0);
  42:      Computer c = new Computer(); 
  43:      c.Audio.Stop();
  44:  }
  45: protected void btnRead_Click(object sender, EventArgs e)
  46:  {
  47:      Computer computer = new Computer(); 
  48:      computer.Audio.Play("C:\\recorded.wav",
           AudioPlayMode.Background);
  49:  }
  50:  }

recorded.wav will be a continuous file. you can create new file each time you click stop and save.

3 comments:

Anonymous said...

Does this website really expect me to remove 50 line numbers from copies code? I don't think so. Will be adding it to my blocked list on Google.

Mitesh Machhi said...

how can create new voice file each time when we start new voice mail after stop.

Harikrishna B said...
This comment has been removed by the author.