因此,我遇到了一些问题,我编写了一个拍手传感器,听到有人拍手并执行某个命令时会听到。窗口刷新音频
//CLAP
private float bigValue;
WaveIn waveIn;
private double MaxValue;
private void button1_Loaded(object sender, RoutedEventArgs e)
{
if (Convert.ToInt16(textBox1.Text) > 100)
{
MessageBox.Show("Invalid Value");
return;
}
else
MaxValue = Convert.ToDouble(textBox1.Text)/100;
bigValue = 0;
waveIn = new WaveIn();
int waveInDevices = waveIn.DeviceNumber;
//Get Device Count
for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
{
WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
}
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
int sampleRate = 8000;
int channels = 1;
waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
waveIn.StartRecording();
}
//CLAP
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
float sample32 = sample/32768f;
label1.Content = sample32.ToString();
if (bigValue < sample32)
{
bigValue = sample32;
label2.Content = bigValue.ToString();
if (bigValue > MaxValue)
{
waveIn.StopRecording();
SendMessage(MONITOR_ON, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
MessageBox.Show("Did you Clap?");
}
}
}
}
代码本身按原样工作,但我需要它能够根据需要多次重置自身。这个程序基本上听一个拍手,唤醒显示器并启动它。该程序打破了我添加另一个“waveIn.StartRecording();”的任何时间
关于如何刷新页面或让它永远听音乐的任何想法?
可能需要将'WaveIn'代码移动到后端线程([BackgroundWorker](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx)),它只是在检测到拍手时在主线上触发事件,但始终保持收听。 – wdosanjos
你可以给我一个例子,如何做到这一点 –
我会使用WasapiCapture而不是WaveIn。 WaveIn已经过时了。 Wasapi是一个非常新的API,自Windows Vista以来,它在所有Windows系统上都得到支持。 –