如何在不使用库的情况下在C#中录制音频! [英] How Do I Record Audio In C# Without Using Library!!!

查看:97
本文介绍了如何在不使用库的情况下在C#中录制音频!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在使用NAudio Library,但我想在32位的.wav文件中录制音频,而不使用任何库。我已经google搜索解决方案,但无法找到合适的解决方案。所有解决方案都基于NAudio,Bass等。



请帮忙。

提前谢谢!!!

Recently i am using NAudio Library, but i want to record the audio in .wav file in 32bits without using any library. I have googled for the solution but can't find the appropriate solution. All of the solution is based on NAudio, Bass etc.

Please help.
Thanks in advance!!!

推荐答案

这取决于你所谓的不使用库。 OS API不是一种库吗? :-)无论如何...



最简单的方法(而且非常过时但工作一次)是通过P / Invoke使用Windows模块winmm.dll。方法如下:

It depends on what do you call "not using library". Isn't the OS API a kind of a library? :-) Anyway…

The simplest way (and very outdated but working one) is to use the Windows module "winmm.dll" via P/Invoke. Here is how:
using System;
using System.Runtime.InteropServices;

//...

internal static class Mci {

    internal const string DllName = "winmm.dll";
    internal const string SoundRecordingDeviceId = "soundRecordingDevice"; // any valid identifier
    internal const string OpenCommandFormat = "open new type waveaudio alias {0}";
    internal const string RecordCommandFormat = "record {0}";
    internal const string PauseCommandFormat = "pause {0}";
    internal const string StopCommandFormat = "stop {0}";
    internal const string CloseCommandFormat = "close {0}";
    internal const string SaveCommandFormatFormat = @"save {0} ""{{0}}""";
    internal static readonly string OpenRecorderCommand = string.Format(OpenCommandFormat, SoundRecordingDeviceId);
    internal static readonly string RecordCommand = string.Format(RecordCommandFormat, SoundRecordingDeviceId);
    internal static readonly string PauseCommand = string.Format(PauseCommandFormat, SoundRecordingDeviceId);
    internal static readonly string StopCommand = string.Format(StopCommandFormat, SoundRecordingDeviceId);
    internal static readonly string CloseRecorderCommand = string.Format(CloseCommandFormat, SoundRecordingDeviceId);
    internal static readonly string SaveCommandFormat = string.Format(SaveCommandFormatFormat, SoundRecordingDeviceId);

    [DllImport(DllName)]
    private static extern long mciSendString(
        string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr oCallback);

    internal static void Open() {
        mciSendString(OpenRecorderCommand, null, 0, IntPtr.Zero);
    } //Open

    internal static void Record() {
        mciSendString(RecordCommand, null, 0, IntPtr.Zero);
    } //Record

    internal static void Pause() {
        mciSendString(PauseCommand, null, 0, IntPtr.Zero);
    } //Pause

    internal static void Stop() {
        mciSendString(StopCommand, null, 0, IntPtr.Zero);
    } //Stop

    internal static void Close() {
        mciSendString(CloseRecorderCommand, null, 0, IntPtr.Zero);
    } //Close

    internal static void SaveRecording(string fileName) {
        mciSendString(string.Format(SaveCommandFormat, fileName), null, 0, IntPtr.Zero);
    } //SaveRecording

}  // classs Mci



经过测试。



重要提示:在致电关闭之前,您需要致电 SaveRecording code>。首先打开设备,记录/暂停/恢复...(恢复,再次呼叫记录)然后停止,然后保存文件,然后再次打开/记录。



使用这个界面使用字符串命令有点蹩脚,但这就是我手中的东西;并且它的工作速度足够快,因此使用它不是问题。它只能从默认录制设备录制。输出文件将是一个带有标准波形参数的WAV立体声文件。



在许多情况下,您可能需要更多功能。在这种情况下,您需要使用程序集DirectD.DirectSound.dll。您可以在添加引用窗口的选项卡.NET中添加引用,因为这是放在GAC中的程序集。使用它也很容易。请从这里开始:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb318690%28v=vs.85%29.aspx [ ^ ]。



-SA


It's tested.

Important note: You need to call SaveRecording before calling Close. You first open the device, record/pause/resume… (to resume, call Record again) and stop, then you save the file and then you can open/record again.

This is kind of a lame to use string commands using this interface, but this is what I immediately have in my hands; and it works fast enough, so it's not a problem to use it. It can only record from a default recording device. The output file will be a WAV stereo file with "standard" wave parameters.

In many cases, you may need a lot more features. In this case, you would need to use the assembly "DirectD.DirectSound.dll". You can add the reference in the tab .NET of the "Add Reference" window, because this is the assembly put in the GAC. Using it is also pretty easy. Please start here:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb318690%28v=vs.85%29.aspx[^].

—SA


这篇关于如何在不使用库的情况下在C#中录制音频!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆