This project has moved and is read-only. For the latest updates, please go here.

waveIn_DataAvailable handler is not triggered from windows service or console application !

Jun 19, 2012 at 9:52 AM

hi,

I am trying to develop windows service to record sound when computer started, so I developed class to record voice and I test everything on windows application and it works fine, then I try this class in console application and windows service, I found out waveIn_DataAvailable is not triggered ! even after waveIn.StartRecording(); so I could not grab data to save them in file path.

here is my code:

        public void BeginRecording()
        {
            BeginMonitoring();
            if ((timerIsBetweenTimes.Enabled) || (timerIsEnabled.Enabled))
            {
                timerIsBetweenTimes.Stop();
                timerIsEnabled.Stop();
            }

            if (recordingState != RecordingState.Monitoring)
            {
                //throw new InvalidOperationException("Can't begin recording while we are in this state: " + recordingState.ToString());
                return;
            }
            if (!Directory.Exists(wavPath))
            {
                Directory.CreateDirectory(wavPath);
            }
            if (!wavPath.EndsWith("\\"))
            {
                wavPath += "\\";
            }
            string recDateTimeStr = String.Format("{0:yyyyMMdd_HHmm}", DateTime.Now);
            string waveFileName = "LMRC_" + machineId + "_" + recDateTimeStr + ".wav";
            writer = new WaveFileWriter(wavPath + waveFileName, recordingFormat);
            recordingState = RecordingState.Recording;
            waveIn.StartRecording();
            eventLog1.WriteEntry("StartRecording at: " + String.Format("{0:yyyyMMdd_HHmm}", DateTime.Now), System.Diagnostics.EventLogEntryType.Information);

        }

        public bool BeginMonitoring()
        {
            if (recordingState != RecordingState.Stopped)
            {
                //throw new InvalidOperationException("Can't begin monitoring while we are in this state: " + recordingState.ToString());
                return false;
            }
            getConfigurations();
            for (int n = 0; n < WaveIn.DeviceCount; n++)
            {
                string str = WaveIn.GetCapabilities(n).ProductName.ToLower();
                if (str.Contains(deviceName))
                {
                    deviceNo = n;
                    break;
                }
            }
            RecordingFormat = new WaveFormat(sampleRate, channels);
            waveIn = new WaveIn();
            waveIn.DeviceNumber = deviceNo;
            waveIn.DataAvailable += waveIn_DataAvailable;
            waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
            waveIn.WaveFormat = recordingFormat;

            TryGetVolumeControl();
            recordingState = RecordingState.Monitoring;
            return true;
        }

        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            eventLog1.WriteEntry("waveIn_DataAvailable at: " + String.Format("{0:yyyyMMdd_HHmm}", DateTime.Now), System.Diagnostics.EventLogEntryType.Information);
            byte[] buffer = e.Buffer;
            int bytesRecorded = e.BytesRecorded;
            WriteToFile(buffer, bytesRecorded);
        }
        private void WriteToFile(byte[] buffer, int bytesRecorded)
        {
            eventLog1.WriteEntry("WriteToFile at: " + String.Format("{0:yyyyMMdd_HHmm}", DateTime.Now), System.Diagnostics.EventLogEntryType.Information);
            if (recordingState == RecordingState.Recording || recordingState == RecordingState.RequestedStop)
            {
                if (RecordedTimeMilliSeconds >= recPeriod)
                {
                    Stop();
                }
                else
                {
                    try
                    {
                        writer.WriteData(buffer, 0, bytesRecorded);
                    }
                    catch
                    {
                    }
                }
            }
        }

please help !

Jun 19, 2012 at 10:05 AM

use WaveInEvent instead of WaveIn when using a console or windows service, since there is no message queue to process the callbacks

Mark

Jun 19, 2012 at 11:41 AM
Edited Jun 19, 2012 at 11:42 AM

thanks very much I will try it !

 

Jun 19, 2012 at 11:42 AM

need to get the very latest NAudio. It's available as a preview build on nuget: http://nuget.org/packages/naudio, or you can build it yourself from the latest source

Jun 19, 2012 at 11:42 AM
markheath wrote:

use WaveInEvent instead of WaveIn when using a console or windows service, since there is no message queue to process the callbacks

Mark

hi Mark,

where I can find WaveInEvent Class? on which namespace? because its not included in NAudio.Wave

Jun 19, 2012 at 12:38 PM
markheath wrote:

need to get the very latest NAudio. It's available as a preview build on nuget: http://nuget.org/packages/naudio, or you can build it yourself from the latest source

hi, how can i get latest source code?

Jun 19, 2012 at 12:54 PM

I have downloaded version 1.5.4 using nuget but I didnt find WaveInEvent, please help

here is snapshot of version: 


 


Jun 19, 2012 at 12:58 PM

you need to get the prerelease version from nuget. Or get the latest code by clicking "source code" above this discussion.

Jun 19, 2012 at 1:08 PM

isn't version 1.5.4 is the released one?

Jun 19, 2012 at 1:15 PM

ok I get it, thank u very much, its version 1.5.8 :)

Jun 19, 2012 at 1:50 PM

glad you've found it. I increment the third part of the number every time I release an NAudio build for any reason, and so it is not necessarily related to the prerelease number.

Jul 5, 2012 at 2:02 PM

Hi Mark,

I have the same problem but i couldn't find the release 1.5.8. could you give me the link please?

 

Jul 16, 2012 at 10:54 AM

you can either build the code yourself, or use Nuget to pick up a pre-release build

http://nuget.org/packages/naudio

Mark

Aug 24, 2012 at 12:04 PM

Thanks Mark!