Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I record audio with C#/WPF?

I have an application to which I want to add the ability to import small audio snippets directly from a microphone device of some sort.

I already allow importing of pictures and this works okay with disk files and cameras since cameras magically become disk devices when you attach them so a file import method works for both.

Audio however is slightly different. I've already allowed for importing audio files off the disk but I want to add the capability to directly record from a microphone to a disk file or in-memory buffer.

Does C#/WPF provide an easy way to do this? What's a good way to add this functionality?

like image 305
paxdiablo Avatar asked Sep 12 '10 08:09

paxdiablo


2 Answers

Probably the easiest is to use mciSendString function:

public class Program
{
    [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

    static void Main(string[] args)
    {
        mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
        mciSendString("record recsound", "", 0, 0);
        Console.WriteLine("recording, press Enter to stop and save ...");
        Console.ReadLine();

        mciSendString("save recsound c:\\work\\result.wav", "", 0, 0);
        mciSendString("close recsound ", "", 0, 0);
    }
}

Another option is to use the DirectShowNet library (there's a sample called PlayCap).

You might also find this CodeProject article useful.

like image 161
Darin Dimitrov Avatar answered Oct 07 '22 18:10

Darin Dimitrov


this might help. It will use open source NAuodio project...

http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder

:)

like image 45
Shamim Avatar answered Oct 07 '22 18:10

Shamim