This project has moved. For the latest updates, please go here.

DataAvailable not firing,

Feb 1, 2015 at 1:27 PM
Edited Feb 1, 2015 at 1:28 PM
Hi, I'm new to NAudio. I've added the following code to an application of mine, but the DataAvailable event handler never fires, so I'm not able to capture any recording. I just get a broken wave file. Can anyone tell me where I'm going wrong here?

All help is appreciated.
        public static WaveIn waveIn;
        public static WaveFileWriter waveFile;
        public static void AudioStart()
        {
                    waveIn = new WaveIn();
                    waveFile = null;
                    waveIn.WaveFormat = new WaveFormat(8000, 8, 1);
                    waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
                    waveIn.RecordingStopped += new EventHandler<StoppedEventArgs>(waveIn_RecordingStopped);

                    waveFile = new WaveFileWriter(@"C:\WPF8Data\test\temp.wav", waveIn.WaveFormat);
                    waveIn.StartRecording();

                   //I've been using breakpoints to pause between start and stop

                    waveIn.StopRecording()
        }

        static void waveIn_RecordingStopped(object sender, StoppedEventArgs e)
        {
            if (waveIn != null)
            {
                waveIn.Dispose();
                waveIn = null;
            }

            if (waveFile != null)
            {
                waveFile.Dispose();
                waveFile = null;
            }
        }

        static void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (waveFile != null)
            {
                waveFile.Write(e.Buffer, 0, e.BytesRecorded);
                waveFile.Flush();
            }
        }
Coordinator
Feb 1, 2015 at 1:28 PM
Most likely you are in a console application, or simply not on a GUI thread. Try WaveInEvent instead, which makes a separate thread for reading audio.
Feb 1, 2015 at 1:36 PM
Edited Feb 1, 2015 at 1:41 PM
Thanks. Not a console application, but not being on a GUI thread is possible. I'm calling the static method (AudioStart) from one of my screens. I'm going to assume simply changing WaveIn to WaveInEvent isn't enough to do this is it? If that actually is all involved, then it didn't fix my issue.
Coordinator
Feb 1, 2015 at 1:46 PM
Strange. Why not create a very simple blank application that just attempts to record and nothing else? Does that work? Could also try recording in 16 bit, just in case your soundcard driver is being difficult. What OS are you using? You're not remote desktopping in or anything unusual are you?
Feb 1, 2015 at 1:56 PM
To answer your questions. I am running Windows 8.1. And nothing unusual.

I will post back with the results of my blank application test.

EDIT: just found the issue. It doesn't like me having called StartRecording(), and then StopRecording() within my AudioStart() method. Even though I was using a breakpoint to simulate time elapsed between them. I removed StopRecording, and let it run, the DataAvailable method fired, and my recording size started increasing.

Thanks for your help. I think I should be sorted out :)
Feb 1, 2015 at 2:03 PM
Edited Feb 1, 2015 at 2:03 PM
Sorry, 1 last minor question. Is there an easy way using NAudio to tell the state of Recording. Like, Stopped, Paused, Recording etc?
Coordinator
Feb 1, 2015 at 2:06 PM
Not really, although it's two state - either recording or not. It starts after you call StartRecording and is stopped when you get the RecordingStopped event fired
Feb 1, 2015 at 2:07 PM
Ok, perfect. Then, I'll just use a Boolean. Appreciate all your help. Have a good one!