简体   繁体   English

如何使用C#/ WPF录制音频?

[英]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? C#/ WPF提供了一种简单的方法吗? What's a good way to add this functionality? 添加此功能的好方法是什么?

Probably the easiest is to use mciSendString function: 可能最简单的方法是使用mciSendString函数:

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 ). 另一个选择是使用DirectShowNet库(有一个名为PlayCap示例 )。

You might also find this CodeProject article useful. 您可能还会发现此CodeProject文章很有用。

this might help. 这可能有所帮助。 It will use open source NAuodio project... 它将使用开源NAuodio项目......

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

:) :)

I'm using the this library: http://www.codeproject.com/KB/cs/Streaming_wave_audio.aspx Mainly due to the simple api. 我正在使用这个库: http//www.codeproject.com/KB/cs/Streaming_wave_audio.aspx主要是由于简单的api。 But I don't like this code too much. 但我不太喜欢这段代码。 For example it fixes it's buffers in memory for a long time instead of allocating unmanaged buffers. 例如,它将内存缓冲区长时间修复,而不是分配非托管缓冲区。

mciSendString function records only microphone sound. mciSendString函数仅记录麦克风声音。 if no mic is connected it will record nothing. 如果没有连接麦克风,它将不记录任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM