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

发布于 2024-09-18 18:04:19 字数 219 浏览 16 评论 0原文

我有一个应用程序,我想添加直接从某种麦克风设备导入小音频片段的功能。

我已经允许导入图片,这适用于磁盘文件和相机,因为当您附加相机时,它们会神奇地变成磁盘设备,因此文件导入方法适用于两者。

然而音频略有不同。我已经允许从磁盘导入音频文件,但我想添加直接从麦克风录制到磁盘文件或内存缓冲区的功能。

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-09-25 18:04:19

可能最简单的方法是使用 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);
    }
}

:选项是使用 DirectShowNet 库(有一个 示例 称为 PlayCap)。

您可能还会发现这篇CodeProject 文章很有用。

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.

倒数 2024-09-25 18:04:19

我正在使用这个库:
http://www.codeproject.com/KB/cs/Streaming_wave_audio.aspx
主要是因为api简单。但我不太喜欢这段代码。例如,它会长时间修复内存中的缓冲区,而不是分配非托管缓冲区。

I'm using the this library:
http://www.codeproject.com/KB/cs/Streaming_wave_audio.aspx
Mainly due to the simple 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文