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

时间:2015-05-07 11:13:27

标签: c# xna directx audio-recording naudio

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

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

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

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

    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;
        }
    }

3 个答案:

答案 0 :(得分:5)

我已经解决了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);
    }
}

答案 1 :(得分:1)

您可以使用DirectShow执行此操作。 请查看Microsoft's documentationproject's code samples,了解根据您的需求进行配置的最佳方式。

答案 2 :(得分:0)

想尝试BASS NET吗? 任务How to record sound and encode it to mp3? (C#!)

有完整的代码