How do I record audio with C#/WPF?

前端 未结 4 1489
醉话见心
醉话见心 2020-12-08 12:32

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

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 13:01

    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.

提交回复
热议问题