如何使用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.
C#/WPF是否提供一种简便的方法?添加此功能的好方法是什么?
Does C#/WPF provide an easy way to do this? What's a good way to add this functionality?
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);
}
}
另一种选择是使用 DirectShowNet 库(有一个
Another option is to use the DirectShowNet library (there's a sample called PlayCap
).
您可能还会发现这篇 CodeProject文章很有用.
You might also find this CodeProject article useful.