NAudio record never receiving a sample

天大地大妈咪最大 提交于 2019-12-11 11:08:52

问题


I'm attempting the simplest possible NAudio example to record from an input device but for some reason I can't get the DataAvailable callback function to be called.

In the example below a break point on Do Something never gets hit.

WaveIn waveIn = new WaveIn();
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
waveIn.WaveFormat = new WaveFormat(44100, 1);
waveIn.StartRecording();

private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
   Do Something
}

I've checked, re-checked and re-re-checked that the settings are exactly the same as those used by the NAudio VoiceRecorder test application which is able to record audio fine with the exact same settings.

The only difference is my test application is a console application rather than a WPF app. Would that make a difference?


回答1:


Yes, it is because it is a console app, and the WaveIn class uses Windows messages as callbacks. If you are able to download and build the very latest source code from codeplex, you can use the brand new WaveInEvent class (added 6 Mar 2012), which does not rely on a Windows message loop.

Alternatively, if you are familiar with installing pre-release packages using NuGet, you can install the latest NAudio prerelease (currently 1.5.4-beta) which has this class in.




回答2:


Just a though, have you verified your device is valid?

Try something like this;

        int waveInDevices = WaveIn.DeviceCount;
        for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
        {
            WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
            MessageBox.Show("Device " + waveInDevice + ": " + deviceInfo.ProductName +
                ", " + deviceInfo.Channels + " channels");
        } 

to verify you have a recording device.

Bernie



来源:https://stackoverflow.com/questions/9600427/naudio-record-never-receiving-a-sample

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!