从麦克风输入录制现场音频并同时保存

Record live audio from mic input and save simultanously

我试图用 C# 开发录音机。我尝试了很多方法,比如NAudio、DirectX、Microsoft.Xna.Framework.Audio等

一切都给出相同的结果。停止录制后,输出文件 mp3/wav 得到保存。

mp3/wav 文件本身在开头创建(没有内容 - 0 字节)

我正在尝试创建一个可以保存音频的应用程序 live/simultaneously。

    private void StartRecording() {
        this.WaveSource = new WaveInEvent { WaveFormat = new WaveFormat(44100, 1) };

        this.WaveSource.DataAvailable += this.WaveSourceDataAvailable;
        this.WaveSource.RecordingStopped += this.WaveSourceRecordingStopped;

        this.WaveFile = new WaveFileWriter(@"C:\Sample.wav", this.WaveSource.WaveFormat);

        this.WaveSource.StartRecording();
    }

    private void StopRecording() {
        this.WaveSource.StopRecording();
    }

    void WaveSourceDataAvailable(object sender, WaveInEventArgs e) {
        if (this.WaveFile != null) {
            this.WaveFile.Write(e.Buffer, 0, e.BytesRecorded);
            this.WaveFile.Flush();
        }
    }

    void WaveSourceRecordingStopped(object sender, StoppedEventArgs e) {
        if (this.WaveSource != null) {
            this.WaveSource.Dispose();
            this.WaveSource = null;
        }

        if (this.WaveFile != null) {
            this.WaveFile.Dispose();
            this.WaveFile = null;
        }
    }

想试试 BASS NET 吗? 有一个完整的代码给你任务How to record sound and encode it to mp3? (C#!)

您可以使用 DirectShow 执行此操作。 查看 Microsoft's documentation and the project's code samples 以了解根据您的需要配置它的最佳方法。

我已经解决了 NAudio 库本身的问题。 对现有代码进行少量修改。

public class Recorder {

    WaveIn sourceStream;
    WaveFileWriter waveWriter;
    readonly String FilePath;
    readonly String FileName;
    readonly int InputDeviceIndex;

    public Recorder(int inputDeviceIndex, String filePath, String fileName) {
        InitializeComponent();
        this.InputDeviceIndex = inputDeviceIndex;
        this.FileName = fileName;
        this.FilePath = filePath;
    }

    public void StartRecording(object sender, EventArgs e) {
        sourceStream = new WaveIn {
            DeviceNumber = this.InputDeviceIndex,
            WaveFormat =
                new WaveFormat(44100, WaveIn.GetCapabilities(this.InputDeviceIndex).Channels)
        };

        sourceStream.DataAvailable += this.SourceStreamDataAvailable;

        if (!Directory.Exists(FilePath)) {
            Directory.CreateDirectory(FilePath);
        }

        waveWriter = new WaveFileWriter(FilePath + FileName, sourceStream.WaveFormat);
        sourceStream.StartRecording();
    }

    public void SourceStreamDataAvailable(object sender, WaveInEventArgs e) {
        if (waveWriter == null) return;
        waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
        waveWriter.Flush();
    }

    private void RecordEnd(object sender, EventArgs e) {
        if (sourceStream != null) {
            sourceStream.StopRecording();
            sourceStream.Dispose();
            sourceStream = null;
        }
        if (this.waveWriter == null) {
            return;
        }
        this.waveWriter.Dispose();
        this.waveWriter = null;
        recordEndButton.Enabled = false;
        Application.Exit();
        Environment.Exit(0);
    }
}