简体   繁体   English

N录音

[英]NAudio recording

i have this code here : 我在这里有此代码:

 NAudio.Wave.WaveIn sourceStream = null;
    NAudio.Wave.DirectSoundOut waveOut = null;
    NAudio.Wave.WaveFileWriter waveWriter = null;
        private void button3_Click(object sender, RoutedEventArgs e) // Start
    {
        ShowImage();

        sourceStream = new NAudio.Wave.WaveIn();
        sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, 2);

        NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(sourceStream);

        waveOut = new NAudio.Wave.DirectSoundOut();
        waveOut.Init(waveIn);

        sourceStream.StartRecording();
    }

    private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
    {
        if (waveWriter == null) return;

        waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
        waveWriter.Flush();
    }

    private void btnStop_Click(object sender, RoutedEventArgs e)
    {
        if (waveOut != null)
        {
            waveOut.Stop();
            waveOut.Dispose();
            waveOut = null;
        }
        if (sourceStream != null)
        {
            sourceStream.StopRecording();
            sourceStream.Dispose();
            sourceStream = null;
        }
        if (waveWriter != null)
        {
            waveWriter.Dispose();
            waveWriter = null;
        }

        sourceStream = new NAudio.Wave.WaveIn();
        sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, 2);
        sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
        waveWriter = new NAudio.Wave.WaveFileWriter(@"../../StoryImages/", sourceStream.WaveFormat);

        sourceStream.StopRecording();
    }

Am i doing it correctly? 我做对了吗? I seen tutorials and tried but i am really weak in programming , theres a problem with the Buffer that seems to crash my whole application. 我看过教程并尝试过,但是我在编程方面确实很虚弱,Buffer似乎有一个问题,它使我的整个应用程序崩溃了。 It always says buffer is full. 总是说缓冲区已满。 Theres a problem with 有一个问题

WriteData(e.Buffer , 0,e.BytesRecorded); WriteData(e.Buffer,0,e.BytesRecorded);

i am using NAudio to do this in WPF , am using visual studio 2010. 我正在使用NAudio在WPF中执行此操作,正在使用Visual Studio 2010。

Maybe I missunderstand you, but the code you posted seems a little bit unstructured. 也许我会误解您的意思,但是您发布的代码似乎有些杂乱无章。 At the moment you start your recording with button3_Click, your WaveWriter is still null. 从button3_Click开始录制时,WaveWriter仍然为空。 Why dont you create an instance of this writer also in button3_click ? 为什么不在button3_click中也创建此writer的实例?

In total I would recommend to sort your object creation and disposing. 总的来说,我建议您对对象的创建和处理进行排序。 -> Start method should contain all object creation, as well as adding the event handler with "+=". -> Start方法应包含所有对象的创建,以及添加带有“ + =”的事件处理程序。 The stop button handler should contain all closing of the stream, and freeing the ressources. 停止按钮处理程序应包含所有关闭流,并释放资源。

I think the probleme here is that the buffer is never emptied. 我认为这里的问题是缓冲区永远不会被清空。 You are missing waveOut.Play(); 您缺少waveOut.Play(); after sourceStream.StartRecording(); 在sourceStream.StartRecording()之后; to make the buffer properly flow down to the speakers. 使缓冲区正确流到扬声器。

Also, you cannot empty the buffer direclty in the sourceStream_DataAvailable callback because it is read only. 同样,您不能在sourceStream_DataAvailable回调中清空缓冲区的重复数据,因为它是只读的。

If you don`t want audio playback, you will need another sink then DirectSoundOut. 如果您不想播放音频,则需要另一个接收器,然后是DirectSoundOut。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM